[PATCH v2 3/3] libsubcmd: Silence compiler warning

Eder Zulian posted 3 patches 1 month ago
[PATCH v2 3/3] libsubcmd: Silence compiler warning
Posted by Eder Zulian 1 month ago
Initialize the pointer 'o' in options__order to NULL to prevent a
compiler warning/error which is observed when compiling with the '-Og'
option, but is not emitted by the compiler with the current default
compilation options.

For example, when compiling libsubcmd with

 $ make "EXTRA_CFLAGS=-Og" -C tools/lib/subcmd/ clean all

Clang version 17.0.6 and GCC 13.3.1 fail to compile parse-options.c due
to following error:

  parse-options.c: In function ‘options__order’:
  parse-options.c:832:9: error: ‘o’ may be used uninitialized [-Werror=maybe-uninitialized]
    832 |         memcpy(&ordered[nr_opts], o, sizeof(*o));
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  parse-options.c:810:30: note: ‘o’ was declared here
    810 |         const struct option *o, *p = opts;
        |                              ^
  cc1: all warnings being treated as errors

Signed-off-by: Eder Zulian <ezulian@redhat.com>
---
 tools/lib/subcmd/parse-options.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
index eb896d30545b..555d617c1f50 100644
--- a/tools/lib/subcmd/parse-options.c
+++ b/tools/lib/subcmd/parse-options.c
@@ -807,7 +807,7 @@ static int option__cmp(const void *va, const void *vb)
 static struct option *options__order(const struct option *opts)
 {
 	int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
-	const struct option *o, *p = opts;
+	const struct option *o = NULL, *p = opts;
 	struct option *opt, *ordered = NULL, *group;
 
 	/* flatten the options that have parents */
-- 
2.46.2

Re: [PATCH v2 3/3] libsubcmd: Silence compiler warning
Posted by Andrii Nakryiko 1 month ago
On Tue, Oct 22, 2024 at 10:24 AM Eder Zulian <ezulian@redhat.com> wrote:
>
> Initialize the pointer 'o' in options__order to NULL to prevent a
> compiler warning/error which is observed when compiling with the '-Og'
> option, but is not emitted by the compiler with the current default
> compilation options.
>
> For example, when compiling libsubcmd with
>
>  $ make "EXTRA_CFLAGS=-Og" -C tools/lib/subcmd/ clean all
>
> Clang version 17.0.6 and GCC 13.3.1 fail to compile parse-options.c due
> to following error:
>
>   parse-options.c: In function ‘options__order’:
>   parse-options.c:832:9: error: ‘o’ may be used uninitialized [-Werror=maybe-uninitialized]
>     832 |         memcpy(&ordered[nr_opts], o, sizeof(*o));
>         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   parse-options.c:810:30: note: ‘o’ was declared here
>     810 |         const struct option *o, *p = opts;
>         |                              ^
>   cc1: all warnings being treated as errors
>
> Signed-off-by: Eder Zulian <ezulian@redhat.com>
> ---
>  tools/lib/subcmd/parse-options.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>

First two patches look good, we can take them through bpf-next. What
do we do with this one? Arnaldo, would you like us to take it through
bpf-next as well (if yes, please give your ack), or you'd like to take
it through your tree?

> diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> index eb896d30545b..555d617c1f50 100644
> --- a/tools/lib/subcmd/parse-options.c
> +++ b/tools/lib/subcmd/parse-options.c
> @@ -807,7 +807,7 @@ static int option__cmp(const void *va, const void *vb)
>  static struct option *options__order(const struct option *opts)
>  {
>         int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
> -       const struct option *o, *p = opts;
> +       const struct option *o = NULL, *p = opts;
>         struct option *opt, *ordered = NULL, *group;
>
>         /* flatten the options that have parents */
> --
> 2.46.2
>
Re: [PATCH v2 3/3] libsubcmd: Silence compiler warning
Posted by Arnaldo Carvalho de Melo 1 month ago
On Tue, Oct 22, 2024 at 04:18:15PM -0700, Andrii Nakryiko wrote:
> On Tue, Oct 22, 2024 at 10:24 AM Eder Zulian <ezulian@redhat.com> wrote:
> >
> > Initialize the pointer 'o' in options__order to NULL to prevent a
> > compiler warning/error which is observed when compiling with the '-Og'
> > option, but is not emitted by the compiler with the current default
> > compilation options.
> >
> > For example, when compiling libsubcmd with
> >
> >  $ make "EXTRA_CFLAGS=-Og" -C tools/lib/subcmd/ clean all
> >
> > Clang version 17.0.6 and GCC 13.3.1 fail to compile parse-options.c due
> > to following error:
> >
> >   parse-options.c: In function ‘options__order’:
> >   parse-options.c:832:9: error: ‘o’ may be used uninitialized [-Werror=maybe-uninitialized]
> >     832 |         memcpy(&ordered[nr_opts], o, sizeof(*o));
> >         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   parse-options.c:810:30: note: ‘o’ was declared here
> >     810 |         const struct option *o, *p = opts;
> >         |                              ^
> >   cc1: all warnings being treated as errors
> >
> > Signed-off-by: Eder Zulian <ezulian@redhat.com>
> > ---
> >  tools/lib/subcmd/parse-options.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> 
> First two patches look good, we can take them through bpf-next. What
> do we do with this one? Arnaldo, would you like us to take it through
> bpf-next as well (if yes, please give your ack), or you'd like to take

Yes, please take it thru bpf-next

Acked-by: Arnaldo Carvalho de Melo <acme@redhat.com>

- Arnaldo

> it through your tree?
 
> > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> > index eb896d30545b..555d617c1f50 100644
> > --- a/tools/lib/subcmd/parse-options.c
> > +++ b/tools/lib/subcmd/parse-options.c
> > @@ -807,7 +807,7 @@ static int option__cmp(const void *va, const void *vb)
> >  static struct option *options__order(const struct option *opts)
> >  {
> >         int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
> > -       const struct option *o, *p = opts;
> > +       const struct option *o = NULL, *p = opts;
> >         struct option *opt, *ordered = NULL, *group;
> >
> >         /* flatten the options that have parents */
> > --
> > 2.46.2