BUILD: trace: fix warning on null dereference

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.
This commit is contained in:
Amaury Denoyelle 2024-05-24 14:17:11 +02:00
parent 77c228f04f
commit 4e632545f7

View 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;
}