]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: trace: fix warning on null dereference
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 24 May 2024 12:17:11 +0000 (14:17 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 24 May 2024 12:36:03 +0000 (14:36 +0200)
Since a recent change on trace, the following compilation warning may
occur :
  src/trace.c: In function ‘trace_parse_cmd’:
  src/trace.c:865:33: error: potential null pointer dereference [-Werror=null-dereference]
    865 |                         for (nd = src->decoding; nd->name && nd->desc; nd++)
        |                              ~~~^~~~~~~~~~~~~~~

Fix this by rearranging code path to better highlight that only "quiet"
verbosity is allowed if no trace source is specified.

This was detected with GCC 14.1.

src/trace.c

index 861ec9b013a38735a7f864169c1c839ee0792371..fcf557bb7b31cad79d2fcbc88c50aa96bdd5902b 100644 (file)
@@ -376,15 +376,16 @@ static int trace_source_parse_verbosity(struct trace_source *src,
        const struct name_desc *nd;
        int ret;
 
+       /* Only "quiet" is defined for all sources. Other identifiers are
+        * specific to trace source.
+        */
        if (strcmp(verbosity, "quiet") == 0) {
                ret = 0;
                goto end;
        }
 
-       /* Only "quiet" is defined for all sources. Other identifiers are
-        * specific to trace source.
-        */
-       BUG_ON(!src);
+       if (!src)
+               return -1;
 
        if (!src->decoding || !src->decoding[0].name) {
                if (strcmp(verbosity, "default") != 0)
@@ -852,18 +853,18 @@ int trace_parse_cmd(char *arg, char **errmsg)
                        return 1;
                }
 
-               if (!src && strcmp(field, "quiet") != 0) {
-                       memprintf(errmsg, "trace source must be specified for verbosity other than 'quiet'");
-                       return 1;
-               }
-
                verbosity = trace_source_parse_verbosity(src, field);
                if (verbosity < 0) {
                        const struct name_desc *nd;
 
-                       memprintf(errmsg, "no such trace verbosity '%s' for source '%s', available verbosities for this source are: 'quiet'", field, name);
-                       for (nd = src->decoding; nd->name && nd->desc; nd++)
-                               memprintf(errmsg, "%s, %s'%s'", *errmsg, (nd + 1)->name ? "" : "and ", nd->name);
+                       if (!src) {
+                               memprintf(errmsg, "trace source must be specified for verbosity other than 'quiet'");
+                       }
+                       else {
+                               memprintf(errmsg, "no such trace verbosity '%s' for source '%s', available verbosities for this source are: 'quiet'", field, name);
+                               for (nd = src->decoding; nd->name && nd->desc; nd++)
+                                       memprintf(errmsg, "%s, %s'%s'", *errmsg, (nd + 1)->name ? "" : "and ", nd->name);
+                       }
 
                        return 1;
                }