Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



As 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 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.



Exactly, 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/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: