bug-gnulib
[Top][All Lists]
Advanced

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

Fix trim2 uninitialized GCC warning


From: Georg Sauthoff
Subject: Fix trim2 uninitialized GCC warning
Date: Sun, 2 Apr 2023 18:11:09 +0200

Hello,

on Fedora Rawhide, gnulib compilation yields the following warning:

gcc -DLOCALEDIR=\"/usr/share/locale\" -DHAVE_CONFIG_H -I.  -Ilib -I./lib -Isrc 
-I./src   -O2 -flto=auto -ffat-lto-objects -fexceptions -g 
-grecord-gcc-switches -pipe -Wall -Werror=format-security 
-Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS 
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong 
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -m64 -mcpu=power8 -mtune=power8 
-fasynchronous-unwind-tables -fstack-clash-protection  -c -o lib/trim.o 
lib/trim.c
make[2]: Leaving directory '/builddir/build/BUILD/datamash-1.8'
lib/trim.c: In function 'trim2':
lib/trim.c:103:16: warning: 'r' may be used uninitialized 
[-Wmaybe-uninitialized]
  103 |             *r = '\0';
      |             ~~~^~~~~~
lib/trim.c:69:17: note: 'r' was declared here
   69 |           char *r IF_LINT (= NULL); /* used only while state = 2 */
      |                 ^


Looking at the code [1], it seems that there was an attempt to fix this
warning in GCC or in another checker.

However, to eliminate the warning, the code also can be simplified like this:

```
diff --git a/lib/trim.c b/lib/trim.c
index 162c43e1de..bfd743cf0f 100644
--- a/lib/trim.c
+++ b/lib/trim.c
@@ -30,12 +30,6 @@
 #include "mbiter.h"
 #include "xalloc.h"
 
-/* Use this to suppress gcc's "...may be used before initialized" warnings. */
-#if defined GCC_LINT || defined lint
-# define IF_LINT(Code) Code
-#else
-# define IF_LINT(Code) /* empty */
-#endif
 
 char *
 trim2 (const char *s, int how)
@@ -66,7 +60,6 @@ trim2 (const char *s, int how)
       if (how != TRIM_LEADING)
         {
           unsigned int state = 0;
-          char *r IF_LINT (= NULL); /* used only while state = 2 */
 
           mbi_init (i, d, strlen (d));
 
@@ -87,7 +80,7 @@ trim2 (const char *s, int how)
               if (state == 1 && mb_isspace (mbi_cur (i)))
                 {
                   state = 2;
-                  r = (char *) mbi_cur_ptr (i);
+                  *(char *) mbi_cur_ptr (i) = 0;
                 }
               else if (state == 2 && mb_isspace (mbi_cur (i)))
                 {
@@ -98,9 +91,6 @@ trim2 (const char *s, int how)
                   state = 1;
                 }
             }
-
-          if (state == 2)
-            *r = '\0';
         }
     }
   else
```

In that way the warning is reliably eliminated, there is no need anymore
for clever macro tricks and arguably the code is now easier to grasp.


Best regards,
Georg

[1]: 
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/trim.c;h=162c43e1dec073db4147287d71526f2d44ee1705;hb=HEAD#l69
-- 
'Setting environment variables from .travis.yml
 Broadcast message from root@testing-gce-259341e4-fe5b-44da-9a00-6941f50689b5
 The system is going down for power off NOW!'
  2016, Travis CI log of a 'failed' build



reply via email to

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