[Bug c/124230] bound sanitizer does not issue error for out-of-bound access through pointer array annotated with counted_by

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Mar 6 20:39:18 GMT 2026


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124230

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:9b3c24577e75356efabcdfe0beaec2a4f0bc6686

commit r16-7934-g9b3c24577e75356efabcdfe0beaec2a4f0bc6686
Author: Qing Zhao <qing.zhao@oracle.com>
Date:   Fri Mar 6 18:58:41 2026 +0000

    Fix [PR124230]

    For a pointer array reference that is annotated with counted_by attribute,
    such as:

      struct annotated {
        int *c __attribute__ ((counted_by (b)));
        int b;
      };

      struct annotated *p = setup (10);
      p->c[12] = 2; //out of bound access

    the IR for p->c[12] is:
      (.ACCESS_WITH_SIZE (p->c, &p->b, 0B, 4) + 48) = 2;

    The current routine get_index_from_offset in c-family/c-ubsan.cc cannot
    handle the integer constant offset "48" correctly.

    The fix is to enhance "get_index_from_offset" to correctly handle the
constant
    offset.

            PR c/124230

    gcc/c-family/ChangeLog:

            * c-ubsan.cc (get_index_from_offset): Handle the special case when
            the offset is an integer constant.

    gcc/testsuite/ChangeLog:

            * gcc.dg/ubsan/pointer-counted-by-bounds-124230-char.c: New test.
            * gcc.dg/ubsan/pointer-counted-by-bounds-124230-float.c: New test.
            * gcc.dg/ubsan/pointer-counted-by-bounds-124230-struct.c: New test.
            * gcc.dg/ubsan/pointer-counted-by-bounds-124230-union.c: New test.
            * gcc.dg/ubsan/pointer-counted-by-bounds-124230.c: New test.


More information about the Gcc-bugs mailing list