pengutronix / genimage

tool to generate multiple filesystem and flash images from a tree
GNU General Public License v2.0
305 stars 110 forks source link

warn_unused_result build warnings #164

Closed lbmeng closed 2 years ago

lbmeng commented 3 years ago

Configure result:

        genimage 14
        =====

        prefix:                 /usr/local

        compiler:               gcc
        cflags:                 -g -O2  -pipe -Wall -Wextra -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wsign-compare -Wchar-subscripts -Wstrict-prototypes -Wshadow -Wformat-security -Wtype-limits -Wno-unused-parameter -ffunction-sections -fdata-sections -fvisibility=hidden
        ldflags:                  -Wl,--as-needed -Wl,--gc-sections

        debug:                  no
        hide symbols:           yes
        libconfuse:             -lconfuse

When building with gcc 9.3.0 on Ubuntu 20.04, 2 build warnings were seen:

util.c: In function ‘uuid_random’:
util.c:728:2: warning: ignoring return value of ‘asprintf’, declared with attribute warn_unused_result [-Wunused-result]
  728 |  asprintf(&uuid, "%04lx%04lx-%04lx-%04lx-%04lx-%04lx%04lx%04lx",
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  729 |    random() & 0xffff, random() & 0xffff,
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  730 |    random() & 0xffff,
      |    ~~~~~~~~~~~~~~~~~~
  731 |    (random() & 0x0fff) | 0x4000,
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  732 |    (random() & 0x3fff) | 0x8000,
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  733 |    random() & 0xffff, random() & 0xffff, random() & 0xffff);
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

image-flash.c: In function ‘flash_generate’:
image-flash.c:37:8: warning: ignoring return value of ‘truncate’, declared with attribute warn_unused_result [-Wunused-result]
   37 |  (void)truncate(imageoutfile(image), 0);
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If compiling with "-O0", these warnings were gone.

As you can see, the call to truncate() in image-flash.c is casted by (void), which seems to suppress exact this build warning, but it does not work, at least for GCC (haven't tried clang yet).

Actually casting the function call with (void) does not suppress this warning is a deliberate "feature" of GCC "-Wunused-result" attribute. There have been a lot of arguments in the community to persuade the GCC people to change the behavior. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425.