|
|
|
|
|
by ranger_danger
33 days ago
|
|
> For example, this only works because the string foobarbazboo is the same length as admin_id 12 != 8 I don't get it... > As you can guess, this is an example of undefined behavior causing unintuitive results. No I couldn't... and what undefined behavior where? They don't explain anything. |
|
As for the rest of the bug, my guess is:
1. Construct a temporary std::string for "foobarbazbug". The length of this string is 12 so it's stored on the stack due to the short/small string optimization.
2. Call Sanitize, passing a pointer to the temporary
3. Store the return value of Sanitize. Note that this return value is basically a pointer to the entire contents of the temporary, which due to SSO means the return value is actually a pointer to a specific bit of stack memory
4. Destroy the temporary.
5. Construct a std::string for the admin_id, reusing the stack slot for the temporary. This data also ends up on the stack due to SSO.
6. username == admin_id. username points to the stack memory formerly occupied by the argument passed to Sanitize(), which is now occupied by admin_id. As a result this comparison is effectively admin_id == admin_id, which is obviously true.
I suspect (but can't check for now) that due to the dependence on SSO lengths up to 16 should work in general, up to 22 (or 23? Can't remember) should work for libc++, and beyond that is not very likely to work at all.