Linux-Modules Archive on lore.kernel.org
 help / Atom feed
* [PATCH] libkmod: fix uninitialized variable usage warnings
@ 2017-11-06 21:57 Eugene Syromiatnikov
  2017-11-07  3:24 ` Lucas De Marchi
  0 siblings, 1 reply; 4+ messages in thread
From: Eugene Syromiatnikov @ 2017-11-06 21:57 UTC (permalink / raw)
  To: linux-modules; +Cc: Yauheni Kaliuta

There are two places where _cleanup_free_ variables are not initialised
by the time function exits that have been caught by gcc:

	In file included from libkmod/libkmod.c:35:0:
	libkmod/libkmod.c: In function 'kmod_lookup_alias_is_builtin':
	./shared/util.h:73:13: warning: 'line' may be used uninitialized in this function [-Wmaybe-uninitialized]
		 free(*(void**) p);
		     ^
	libkmod/libkmod.c:551:23: note: 'line' was declared here
	  _cleanup_free_ char *line;
			       ^
	In file included from libkmod/libkmod-module.c:42:0:
	libkmod/libkmod-module.c: In function 'kmod_module_probe_insert_module':
	./shared/util.h:73:13: warning: 'cmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
		 free(*(void**) p);
		     ^
	libkmod/libkmod-module.c:996:23: note: 'cmd' was declared here
	  _cleanup_free_ char *cmd;
                       ^

This patch initializes them to NULL so free become no-op in these cases.
---
 libkmod/libkmod-module.c | 2 +-
 libkmod/libkmod.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 0a3ef11..6f23c1a 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -995,7 +995,7 @@ static int module_do_install_commands(struct kmod_module *mod,
 {
 	const char *command = kmod_module_get_install_commands(mod);
 	char *p;
-	_cleanup_free_ char *cmd;
+	_cleanup_free_ char *cmd = NULL;
 	int err;
 	size_t cmdlen, options_len, varlen;
 
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 69fe431..964772d 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -556,7 +556,7 @@ finish:
 
 bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
 {
-	_cleanup_free_ char *line;
+	_cleanup_free_ char *line = NULL;
 
 	line = lookup_builtin_file(ctx, name);
 
-- 
2.1.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libkmod: fix uninitialized variable usage warnings
  2017-11-06 21:57 [PATCH] libkmod: fix uninitialized variable usage warnings Eugene Syromiatnikov
@ 2017-11-07  3:24 ` Lucas De Marchi
  2017-11-07 11:59   ` Eugene Syromiatnikov
  0 siblings, 1 reply; 4+ messages in thread
From: Lucas De Marchi @ 2017-11-07  3:24 UTC (permalink / raw)
  To: Eugene Syromiatnikov; +Cc: linux-modules, Yauheni Kaliuta

On Mon, Nov 6, 2017 at 1:57 PM, Eugene Syromiatnikov <esyr@redhat.com> wrote:
> There are two places where _cleanup_free_ variables are not initialised
> by the time function exits that have been caught by gcc:
>
>         In file included from libkmod/libkmod.c:35:0:
>         libkmod/libkmod.c: In function 'kmod_lookup_alias_is_builtin':
>         ./shared/util.h:73:13: warning: 'line' may be used uninitialized in this function [-Wmaybe-uninitialized]
>                  free(*(void**) p);
>                      ^
>         libkmod/libkmod.c:551:23: note: 'line' was declared here
>           _cleanup_free_ char *line;
>                                ^
>         In file included from libkmod/libkmod-module.c:42:0:
>         libkmod/libkmod-module.c: In function 'kmod_module_probe_insert_module':
>         ./shared/util.h:73:13: warning: 'cmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
>                  free(*(void**) p);
>                      ^
>         libkmod/libkmod-module.c:996:23: note: 'cmd' was declared here
>           _cleanup_free_ char *cmd;
>                        ^
>
> This patch initializes them to NULL so free become no-op in these cases.
> ---
>  libkmod/libkmod-module.c | 2 +-
>  libkmod/libkmod.c        | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
> index 0a3ef11..6f23c1a 100644
> --- a/libkmod/libkmod-module.c
> +++ b/libkmod/libkmod-module.c
> @@ -995,7 +995,7 @@ static int module_do_install_commands(struct kmod_module *mod,
>  {
>         const char *command = kmod_module_get_install_commands(mod);
>         char *p;
> -       _cleanup_free_ char *cmd;
> +       _cleanup_free_ char *cmd = NULL;
>         int err;
>         size_t cmdlen, options_len, varlen;
>
> diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> index 69fe431..964772d 100644
> --- a/libkmod/libkmod.c
> +++ b/libkmod/libkmod.c
> @@ -556,7 +556,7 @@ finish:
>
>  bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
>  {
> -       _cleanup_free_ char *line;
> +       _cleanup_free_ char *line = NULL;

This seems to be a bogus warning. See the line just below... there's
no way to exit this function without first  assigning 'line'.

Lucas De Marchi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libkmod: fix uninitialized variable usage warnings
  2017-11-07  3:24 ` Lucas De Marchi
@ 2017-11-07 11:59   ` Eugene Syromiatnikov
  2017-11-07 16:45     ` Lucas De Marchi
  0 siblings, 1 reply; 4+ messages in thread
From: Eugene Syromiatnikov @ 2017-11-07 11:59 UTC (permalink / raw)
  To: Lucas De Marchi; +Cc: linux-modules, Yauheni Kaliuta

On Mon, Nov 06, 2017 at 07:24:55PM -0800, Lucas De Marchi wrote:
> On Mon, Nov 6, 2017 at 1:57 PM, Eugene Syromiatnikov <esyr@redhat.com> wrote:
> > There are two places where _cleanup_free_ variables are not initialised
> > by the time function exits that have been caught by gcc:
> >
> >         In file included from libkmod/libkmod.c:35:0:
> >         libkmod/libkmod.c: In function 'kmod_lookup_alias_is_builtin':
> >         ./shared/util.h:73:13: warning: 'line' may be used uninitialized in this function [-Wmaybe-uninitialized]
> >                  free(*(void**) p);
> >                      ^
> >         libkmod/libkmod.c:551:23: note: 'line' was declared here
> >           _cleanup_free_ char *line;
> >                                ^
> >         In file included from libkmod/libkmod-module.c:42:0:
> >         libkmod/libkmod-module.c: In function 'kmod_module_probe_insert_module':
> >         ./shared/util.h:73:13: warning: 'cmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
> >                  free(*(void**) p);
> >                      ^
> >         libkmod/libkmod-module.c:996:23: note: 'cmd' was declared here
> >           _cleanup_free_ char *cmd;
> >                        ^
> >
> > This patch initializes them to NULL so free become no-op in these cases.
> > ---
> >  libkmod/libkmod-module.c | 2 +-
> >  libkmod/libkmod.c        | 2 +-
> >  2 files changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
> > index 0a3ef11..6f23c1a 100644
> > --- a/libkmod/libkmod-module.c
> > +++ b/libkmod/libkmod-module.c
> > @@ -995,7 +995,7 @@ static int module_do_install_commands(struct kmod_module *mod,
> >  {
> >         const char *command = kmod_module_get_install_commands(mod);
> >         char *p;
> > -       _cleanup_free_ char *cmd;
> > +       _cleanup_free_ char *cmd = NULL;
> >         int err;
> >         size_t cmdlen, options_len, varlen;
> >
> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> > index 69fe431..964772d 100644
> > --- a/libkmod/libkmod.c
> > +++ b/libkmod/libkmod.c
> > @@ -556,7 +556,7 @@ finish:
> >
> >  bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
> >  {
> > -       _cleanup_free_ char *line;
> > +       _cleanup_free_ char *line = NULL;
> 
> This seems to be a bogus warning. See the line just below... there's
> no way to exit this function without first  assigning 'line'.

In this instance, I initially thought that gcc is smart and derives
"uninitialised variable" from the lookup_builtin_file call, but nope, it
was happy only after line was initialised on declaration. On the other
hand, documentation[1] mentions that cleanup function is also called
during stack unwinding in case -fexceptions is enabled, and RHEL's RPM
package indeed has this flag, at least, so this GCC's complaint still
seems relevant.

[1] https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html

> Lucas De Marchi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libkmod: fix uninitialized variable usage warnings
  2017-11-07 11:59   ` Eugene Syromiatnikov
@ 2017-11-07 16:45     ` Lucas De Marchi
  0 siblings, 0 replies; 4+ messages in thread
From: Lucas De Marchi @ 2017-11-07 16:45 UTC (permalink / raw)
  To: Eugene Syromiatnikov; +Cc: linux-modules, Yauheni Kaliuta

On Tue, Nov 7, 2017 at 3:59 AM, Eugene Syromiatnikov <esyr@redhat.com> wrote:
> On Mon, Nov 06, 2017 at 07:24:55PM -0800, Lucas De Marchi wrote:
>> On Mon, Nov 6, 2017 at 1:57 PM, Eugene Syromiatnikov <esyr@redhat.com> wrote:
>> > There are two places where _cleanup_free_ variables are not initialised
>> > by the time function exits that have been caught by gcc:
>> >
>> >         In file included from libkmod/libkmod.c:35:0:
>> >         libkmod/libkmod.c: In function 'kmod_lookup_alias_is_builtin':
>> >         ./shared/util.h:73:13: warning: 'line' may be used uninitialized in this function [-Wmaybe-uninitialized]
>> >                  free(*(void**) p);
>> >                      ^
>> >         libkmod/libkmod.c:551:23: note: 'line' was declared here
>> >           _cleanup_free_ char *line;
>> >                                ^
>> >         In file included from libkmod/libkmod-module.c:42:0:
>> >         libkmod/libkmod-module.c: In function 'kmod_module_probe_insert_module':
>> >         ./shared/util.h:73:13: warning: 'cmd' may be used uninitialized in this function [-Wmaybe-uninitialized]
>> >                  free(*(void**) p);
>> >                      ^
>> >         libkmod/libkmod-module.c:996:23: note: 'cmd' was declared here
>> >           _cleanup_free_ char *cmd;
>> >                        ^
>> >
>> > This patch initializes them to NULL so free become no-op in these cases.
>> > ---
>> >  libkmod/libkmod-module.c | 2 +-
>> >  libkmod/libkmod.c        | 2 +-
>> >  2 files changed, 2 insertions(+), 2 deletions(-)
>> >
>> > diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
>> > index 0a3ef11..6f23c1a 100644
>> > --- a/libkmod/libkmod-module.c
>> > +++ b/libkmod/libkmod-module.c
>> > @@ -995,7 +995,7 @@ static int module_do_install_commands(struct kmod_module *mod,
>> >  {
>> >         const char *command = kmod_module_get_install_commands(mod);
>> >         char *p;
>> > -       _cleanup_free_ char *cmd;
>> > +       _cleanup_free_ char *cmd = NULL;
>> >         int err;
>> >         size_t cmdlen, options_len, varlen;
>> >
>> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
>> > index 69fe431..964772d 100644
>> > --- a/libkmod/libkmod.c
>> > +++ b/libkmod/libkmod.c
>> > @@ -556,7 +556,7 @@ finish:
>> >
>> >  bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
>> >  {
>> > -       _cleanup_free_ char *line;
>> > +       _cleanup_free_ char *line = NULL;
>>
>> This seems to be a bogus warning. See the line just below... there's
>> no way to exit this function without first  assigning 'line'.
>
> In this instance, I initially thought that gcc is smart and derives
> "uninitialised variable" from the lookup_builtin_file call, but nope, it
> was happy only after line was initialised on declaration. On the other
> hand, documentation[1] mentions that cleanup function is also called
> during stack unwinding in case -fexceptions is enabled, and RHEL's RPM
> package indeed has this flag, at least, so this GCC's complaint still
> seems relevant.

This is likely to trigger "double assignment" warnings on other compilers.
A good question would be.... why is -fexceptions being used in a C-only library?
If it was a binary or a library that links to a C++-library, I would
understand, but in
this case it seems nonsense. /me lost

An acceptable solution would be to just merge the 2 lines so we
initialize line on
declaration.

I added -fexceptions here to reproduce the issue, but it didn't
reproduce using gcc 7.2.1
on Fedora 27.  I also remember not having the issue with gcc 6. So I
still think it's
a compiler bug, although I don't mind if the solution proposed above
is applied (but mention
on commit message the exact compiler version).


Lucas De Marchi

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-06 21:57 [PATCH] libkmod: fix uninitialized variable usage warnings Eugene Syromiatnikov
2017-11-07  3:24 ` Lucas De Marchi
2017-11-07 11:59   ` Eugene Syromiatnikov
2017-11-07 16:45     ` Lucas De Marchi

Linux-Modules Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-modules/0 linux-modules/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-modules linux-modules/ https://lore.kernel.org/linux-modules \
		linux-modules@vger.kernel.org linux-modules@archiver.kernel.org
	public-inbox-index linux-modules


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-modules


AGPL code for this site: git clone https://public-inbox.org/ public-inbox