PDA

Bekijk Volledige Versie : RE: gcc 4.1 bug miscompiles pointer range checks, may place you at risk



Michael Wojcik
18/04/06, 18:20
> From: Felix von Leitner [mailto:felix-bugtraq@fefe.de]=20
> Sent: Monday, 17 April, 2006 16:04
>=20
> static inline int range_ptrinbuf(const void* buf,unsigned=20
> long len,const void* ptr) {
> register const char* c=3D(const char*)buf; /* no pointer=20
> arithmetic on void* */
> return (c && c+len>c && (const char*)ptr-c<len);
> }
>=20
> ...
> assert(range_ptrinbuf(buf,(unsigned long)-1,buf+1)=3D=3D0);
>=20
> Imagine my surprise when this assertion failed.

As far as the C language is concerned, this isn't a compiler "bug".
You've created an invalid pointer in "c+len" (the result neither points
within the object that includes the location c points to, nor one past
it), which invokes Undefined Behavior (ISO 9899:1990 6.5.6 #8). And
you've compared two pointers which do not point within the same object
("c+len>c"), which also invokes Undefined Behavior (6.5.8 #5).

The behavior of gcc 4.1 in this case might be infelicitous, but it is
not, properly speaking, a bug.

--=20
Michael Wojcik
Principal Software Systems Developer, Micro Focus