3 ms·
The code is wrong, and the compiler has no correct option to use. Whatever it decides to do is ok. This is completely different from the program: int f (c
by marcosdumay 7y ago
The code is wrong, and the compiler has no correct option to use. Whatever it decides to do is ok.
This is completely different from the program:
int f (char* a, size_t len_a, char* b, size_t len_b) {
size_t i = 0;
int eq = 1;
while (i < len_a && i < len_b) {
if (a[i] != b[i]) eq = 0;
i++;
}
return eq && i == len_a && i == len_b;
}
If you pass two strings that differ on the first character, do you expect it to run through all the characters or return at the first? Both will have exactly the same result, and compilers know how to convert one into the other. If you use code that returns early on a function that verifies user password hashes on a web site, you'll just leak all of your users passwords.
- zrm 7y agoThe first function is wrong because it leaves the compiler the discretion to do something dangerous. But isn't that the same problem as the second function? The compiler has much more discretion in the first case, all the way to nasal demons and flying monkeys, but that's little consolation when the discretion it does have in the second case is to do exactly the thing that makes the function insecure. "Whatever it decides to do is ok," but some of the things it can decide to do will leak your password hashes. Throw in Spectre and it's a huge mess, because it's not just the password hashing function leaking the password hashes, it's anything with an exploitable bounds check that can reach the memory where they're stored.