bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Correct but unhelpful VLA warning vs. gnulib's gettext.h; can we elimina


From: James Youngman
Subject: Correct but unhelpful VLA warning vs. gnulib's gettext.h; can we eliminate the false positive?
Date: Sat, 9 Jul 2011 23:32:40 +0100

To be clear before we start, gnulib is doing the right thing here.  It
contains this code in lib/gettext.h:-

static const char *
dcpgettext_expr (const char *domain,
                 const char *msgctxt, const char *msgid,
                 int category)
{
  size_t msgctxt_len = strlen (msgctxt) + 1;
  size_t msgid_len = strlen (msgid) + 1;
  const char *translation;
#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  char msg_ctxt_id[msgctxt_len + msgid_len];
#else
  char buf[1024];
  char *msg_ctxt_id =
    (msgctxt_len + msgid_len <= sizeof (buf)
     ? buf
     : (char *) malloc (msgctxt_len + msgid_len));
  if (msg_ctxt_id != NULL)
#endif


tl;dr: it uses a variable-length array if we determined that the
compiler supports those.   All well and good.   But, if we compile the
code with more GCC warnings turned on via the manywarnings module, we
get this result:

gcc -std=gnu99 -DHAVE_CONFIG_H -I.
-I/home/james/source/GNU/findutils/git/gnu/findutils/find -I..
-I../gl/lib -I/home/james/source/GNU/findutils/git/gnu/findutils/lib
-I/home/james/source/GNU/findutils/git/gnu/findutils/gl/lib -I../intl
-DLOCALEDIR=\"/usr/local/share/locale\"   -Wall -W -Wformat-y2k
-Wformat-security -Winit-self -Wmissing-include-dirs -Wswitch-enum
-Wunused -Wunknown-pragmas -Wstrict-aliasing -Wstrict-overflow
-Wfloat-equal -Wdeclaration-after-statement -Wshadow
-Wunsafe-loop-optimizations -Wpointer-arith -Wbad-function-cast
-Wcast-qual -Wcast-align -Wwrite-strings -Wlogical-op
-Waggregate-return -Wstrict-prototypes -Wold-style-definition
-Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn
-Wmissing-format-attribute -Wpacked -Wredundant-decls -Wnested-externs
-Winline -Winvalid-pch -Wlong-long -Wvla -Wvolatile-register-var
-Wdisabled-optimization -Wstack-protector -Woverlength-strings
-Wbuiltin-macro-redefined -Wmudflap -Wpacked-bitfield-compat
-Wsync-nand -Wattributes -Wcoverage-mismatch -Wmultichar
-Wunused-macros -Wno-missing-field-initializers -g -O2 -MT fstype.o
-MD -MP -MF .deps/fstype.Tpo -c -o fstype.o
/home/james/source/GNU/findutils/git/gnu/findutils/find/fstype.c
cc1: warning: ../intl: No such file or directory
In file included from
/home/james/source/GNU/findutils/git/gnu/findutils/find/fstype.c:56:
/home/james/source/GNU/findutils/git/gnu/findutils/gl/lib/gettext.h:
In function 'dcpgettext_expr':
/home/james/source/GNU/findutils/git/gnu/findutils/gl/lib/gettext.h:216:
warning: variable length array 'msg_ctxt_id' is used
/home/james/source/GNU/findutils/git/gnu/findutils/gl/lib/gettext.h:
In function 'dcnpgettext_expr':
/home/james/source/GNU/findutils/git/gnu/findutils/gl/lib/gettext.h:262:
warning: variable length array 'msg_ctxt_id' is used

In other words, "gcc -Wvla" is issuing a warning for a construct we
know is safe.   However, I can't be sure I won't accidentally write
code in the future which is not protected by something similar to
_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS.   So I think that -Wvla is a
useful warning flag.

Is there a way of eliminating this false positive which doesn't force
me to give up -Wvla?   I mean, apart from giving up the use of VLAs in
gnulib even when it's safe to use them.

James.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]