[PATCH] warn on uninitialized accesses by function calls (PR 10138)

Martin Sebor msebor@gmail.com
Fri May 15 23:31:24 GMT 2020


Besides better buffer overflow checking, the new GCC 10 attribute
access also provides an opportunity to detect other kinds of bugs,
including uninitialized accesses by user-defined functions.
The attached patch implements this enhancement.

In addition, the closely related PR 10138 requests that GCC warn when
passing the address of an uninitialized variable to a const-qualified
pointer function argument.  Const pointers almost always imply a read
access of the object, so the patch also enables the warning in these
cases.  (There are situations when a const pointer doesn't imply it
and the warning takes care not to trigger overly enthusiastically.)
Since pointers often point to allocated objects it seemed natural
(and was surprisingly easy) to also detect uninitialized reads from
those.

For optimum results I slightly enhanced the detection of the referenced
decls and allocations.  In the process, I also noticed and fixed a small
bug in the existing code.   This helps both find more uninitialized
variables and reduce the rate of false positives in existing warnings.

Besides the usual GCC bootstrap/regtest I validated the changes by
building a number of packages, including Binutils/GDB, Glibc, and
the Linux kernel.  It found a decent number of likely bugs (about
half a doze by my count) but also triggered a few false positives.
One class of such problems was due to the kernel's function

   __check_object_size (const void*, unsigned, bool)

used to validate the sizes of objects without ever accessing them.
To accommodate this idiom the patch adds a new  mode to attribute
access: none.

Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc-10138.diff
Type: text/x-patch
Size: 72290 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20200515/941d46ba/attachment-0001.bin>


More information about the Gcc-patches mailing list