3 ms·
Doing anything with invalid pointers is UB in C++. It might be fine now, but it might not work in 5 years, when compiled with a newer compiler. E.g. a sufficie
by pkolaczk 14y ago
Doing anything with invalid pointers is UB in C++. It might be fine now, but it might not work in 5 years, when compiled with a newer compiler.
E.g. a sufficiently clever, malicious compiler could reason that because you are doing pointer arithmetic involving PolicyAccountDomainInfo->, the pointer must be valid and then it might get rid of the null check and branch in the second line. I remember not so long ago there was a vulnerability in Linux kernel because of exactly this kind of "malicious" optimisation performed by gcc.
- Someone 14y agohttps://isc.sans.edu/diary/A+new+fascinating+Linux+kernel+vulnerability/6820 https://isc.sans.edu/diary/A+new+fascinating+Linux+kernel+vu...
- twoodfin 14y agoExactly, though in the case I bet you're remembering, the pointer was dereferenced before the null check so the latter was optimized out. http://lwn.net/Articles/342330/ http://lwn.net/Articles/342330/
- microtherion 14y agoAs others have commented, the Linux incident involved an actual dereferencing of the pointer. The way I read http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf top of page 89 and footnote 102 on that page is that this is NOT undefined behavior in C, and though I haven’t checked the corresponding language in the C++ standard, I would think it’s not undefined in C++ either.