by makomk · 10y ago · 0 comments
struct foo {
int bar;
}
void myfunc(struct foo *p) {
int *t = &p->bar;
if(p == NULL) return;
*t = 5;
}
Is this undefined? Probably. Absent compiler optimizations, it won't cause a crash on any system you're ever likely to encounter because it's not actually dereferencing the pointer, but some versions of gcc have caused security issues in Linux by optimising away NULL checks like this. ...
void myfunc(struct foo *p) {
int *t = (int *)((char *)p + offsetof(p->bar));
...
}
right? In the C abstract machine taking the address of an offset of a pointer is always defined, I think.