[PATCH] Fix PR C/C++/38699, ICE using offsetof with pointer and array accesses

Andrew Pinski pinskia@gmail.com
Sat Jan 3 06:55:00 GMT 2009


Hi,
  The problem here is that fold_offsetof_1 does not error out when
using [] on a pointer.  In some cases we accept this invalid offsetof
and in other cases we would ICE when we have a POINTER_PLUS_EXPR.
This fixes the problem by changing the way fold_offsetof_1 handles
INDIRECT_REF and NOP_EXPR and errors out if the inner most INDIRECT
reference is not an INTEGER_CST.  I added extra code to give a better
error message on what the field is being used incorrectly.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

I added a bunch of testcases for where it is valid to use offsetof and
where it is invalid to use it. The C++ ones are to make sure we catch
invalid cases with dependent types.

Thanks,
Andrew Pinski

* c-common.c (fold_offsetof_1): Handle the cases where INDIRECT_REF's
operand is not a zero constant.

* gcc.dg/offsetof-1.c: New testcase.
* gcc.dg/offsetof-2.c: New test.
* gcc.dg/offsetof-3.c: New test.
* g++.dg/parse/offsetof9.C: New test.
* g++.dg/parse/offsetof10.C: New test.
* g++.dg/parse/offsetof11.C: New test.
* g++.dg/parse/offsetof12.C: New test.
* g++.dg/parse/offsetof13.C: New test.
* g++.dg/parse/offsetof14.C: New test.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: fix38699.diff.txt
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20090103/d740bc4c/attachment.txt>


More information about the Gcc-patches mailing list