This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.

Attachment: fix38699.diff.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]