3 ms·
> Some compilers will optimize out operations they deem useless. A compiler translates a certain language to machine code. If it is optimizing it will use that
by nsajko 7y ago
> Some compilers will optimize out operations they deem useless.
A compiler translates a certain language to machine code. If it is optimizing it will use that language's semantics for optimization. That is necessary, otherwise we would be writing almost everything in assembly.
> Know what optimizations your compiler can do, and carefully consider the effect of each one on security programming patterns. In particular, be careful of optimizations that can remove code or branches, and code that prevents errors which "should be impossible" if the rest of the program is correct.
A priority is to understand the language one is writing in.
> When possible, consider disabling compiler optimizations that can eliminate or weaken security checks.
A better solution is to use assembly language and thus exactly specify your program's semantics within the program; that compiler flag could be removed by whoever builds the program, or something like link-time optimization.
- karl_schlagenfu 7y ago> A priority is to understand the language one is writing in. That's all well and good but you can't expect a developer to know all the optimisations different compilers will do, especially when it relates to undefined behaviuor.
- charleslmunger 7y agoI think the point of the OP is that you shouldn't think about the optimizations a compiler might do, but what the language spec guarantees. If the language spec guarantees that your code behaves the way you want, no correct compiler will break it.
- zokier 7y agoThe problem arises when the language simply does not provide any guarantees on the sorts side-effects you are interested in, so you can not simply rely on specified behavior.
- zaarn 7y agoAnd next thing you know, 1000 people with compilers that subtle do not behave to spec knock on your door.