scripts/setlocalversion: clear local varaible to make it work for sh

Message ID 20191001121724.23886-1-yamada.masahiro@socionext.com
State New
Headers show
Series
  • scripts/setlocalversion: clear local varaible to make it work for sh
Related show

Commit Message

Masahiro Yamada Oct. 1, 2019, 12:17 p.m.
Geert Uytterhoeven reports a strange side-effect of commit 858805b336be
("kbuild: add $(BASH) to run scripts with bash-extension"), which
inserts the contents of a localversion file in the build directory twice.

[Steps to Reproduce]
  $ echo bar > localversion
  $ mkdir build
  $ cd build/
  $ echo foo > localversion
  $ make -s -f ../Makefile defconfig include/config/kernel.release
  $ cat include/config/kernel.release
  5.4.0-rc1foofoobar

This comes down to the behavior change of 'local' variables.

The 'man sh' on my Ubuntu machine, where sh is an alias to dash,
explains as follows:
  When a variable is made local, it inherits the initial value and
  exported and readonly flags from the variable with the same name
  in the surrounding scope, if there is one. Otherwise, the variable
  is initially unset.

[Test Code]

  foo ()
  {
          local res
          echo "res: $res"
  }

  res=1
  foo

[Result]

  $ sh test.sh
  res: 1
  $ bash test.sh
  res:

So, scripts/setlocalversion correctly works only for bash in spite of
its hashbang being #!/bin/sh. Nobody had noticed it before because
CONFIG_SHELL was previously set to sh only when bash is missing, which
is very unlikely to happen.

The benefit of commit 858805b336be is to make people write portable and
correct code. I gave it the Fixes tag since it uncovered the issue for
most of people.

Clear the variable 'res' in collect_files() to make it work for sh
(and it also works on distributions where sh is an alias to bash).

Fixes: commit 858805b336be ("kbuild: add $(BASH) to run scripts with bash-extension")
Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

---

 scripts/setlocalversion | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-- 
2.17.1

Comments

Geert Uytterhoeven Oct. 1, 2019, 1:20 p.m. | #1
Hi Yamada-san,

s/varaible/variable/ in subject.

On Tue, Oct 1, 2019 at 2:17 PM Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:
> Geert Uytterhoeven reports a strange side-effect of commit 858805b336be

> ("kbuild: add $(BASH) to run scripts with bash-extension"), which

> inserts the contents of a localversion file in the build directory twice.

>

> [Steps to Reproduce]

>   $ echo bar > localversion

>   $ mkdir build

>   $ cd build/

>   $ echo foo > localversion

>   $ make -s -f ../Makefile defconfig include/config/kernel.release

>   $ cat include/config/kernel.release

>   5.4.0-rc1foofoobar

>

> This comes down to the behavior change of 'local' variables.

>

> The 'man sh' on my Ubuntu machine, where sh is an alias to dash,

> explains as follows:

>   When a variable is made local, it inherits the initial value and

>   exported and readonly flags from the variable with the same name

>   in the surrounding scope, if there is one. Otherwise, the variable

>   is initially unset.

>

> [Test Code]

>

>   foo ()

>   {

>           local res

>           echo "res: $res"

>   }

>

>   res=1

>   foo

>

> [Result]

>

>   $ sh test.sh

>   res: 1

>   $ bash test.sh

>   res:

>

> So, scripts/setlocalversion correctly works only for bash in spite of

> its hashbang being #!/bin/sh. Nobody had noticed it before because

> CONFIG_SHELL was previously set to sh only when bash is missing, which

> is very unlikely to happen.

>

> The benefit of commit 858805b336be is to make people write portable and

> correct code. I gave it the Fixes tag since it uncovered the issue for

> most of people.

>

> Clear the variable 'res' in collect_files() to make it work for sh

> (and it also works on distributions where sh is an alias to bash).

>

> Fixes: commit 858805b336be ("kbuild: add $(BASH) to run scripts with bash-extension")

> Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>


Can you please use

    Reported-by: Geert Uytterhoeven <geert+renesas@glider.be>

instead?

> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>


Thanks, that fixes the issue for me!

Tested-by: Geert Uytterhoeven <geert+renesas@glider.be>


Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Masahiro Yamada Oct. 1, 2019, 2:45 p.m. | #2
Hi Geert,

On Tue, Oct 1, 2019 at 10:20 PM Geert Uytterhoeven <geert@linux-m68k.org> wrote:
>

> Hi Yamada-san,

>

> s/varaible/variable/ in subject.

>

> On Tue, Oct 1, 2019 at 2:17 PM Masahiro Yamada

> <yamada.masahiro@socionext.com> wrote:

> > Geert Uytterhoeven reports a strange side-effect of commit 858805b336be

> > ("kbuild: add $(BASH) to run scripts with bash-extension"), which

> > inserts the contents of a localversion file in the build directory twice.

> >

> > [Steps to Reproduce]

> >   $ echo bar > localversion

> >   $ mkdir build

> >   $ cd build/

> >   $ echo foo > localversion

> >   $ make -s -f ../Makefile defconfig include/config/kernel.release

> >   $ cat include/config/kernel.release

> >   5.4.0-rc1foofoobar

> >

> > This comes down to the behavior change of 'local' variables.

> >

> > The 'man sh' on my Ubuntu machine, where sh is an alias to dash,

> > explains as follows:

> >   When a variable is made local, it inherits the initial value and

> >   exported and readonly flags from the variable with the same name

> >   in the surrounding scope, if there is one. Otherwise, the variable

> >   is initially unset.

> >

> > [Test Code]

> >

> >   foo ()

> >   {

> >           local res

> >           echo "res: $res"

> >   }

> >

> >   res=1

> >   foo

> >

> > [Result]

> >

> >   $ sh test.sh

> >   res: 1

> >   $ bash test.sh

> >   res:

> >

> > So, scripts/setlocalversion correctly works only for bash in spite of

> > its hashbang being #!/bin/sh. Nobody had noticed it before because

> > CONFIG_SHELL was previously set to sh only when bash is missing, which

> > is very unlikely to happen.

> >

> > The benefit of commit 858805b336be is to make people write portable and

> > correct code. I gave it the Fixes tag since it uncovered the issue for

> > most of people.

> >

> > Clear the variable 'res' in collect_files() to make it work for sh

> > (and it also works on distributions where sh is an alias to bash).

> >

> > Fixes: commit 858805b336be ("kbuild: add $(BASH) to run scripts with bash-extension")

> > Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>

>

> Can you please use

>

>     Reported-by: Geert Uytterhoeven <geert+renesas@glider.be>

>

> instead?


OK, I will.

Thanks.



> > Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

>

> Thanks, that fixes the issue for me!

>

> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be>

>

> Gr{oetje,eeting}s,

>

>                         Geert

>

> --

> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

>

> In personal conversations with technical people, I call myself a hacker. But

> when I'm talking to journalists I just say "programmer" or something like that.

>                                 -- Linus Torvalds




-- 
Best Regards
Masahiro Yamada

Patch

diff --git a/scripts/setlocalversion b/scripts/setlocalversion
index 365b3c2b8f43..220dae0db3f1 100755
--- a/scripts/setlocalversion
+++ b/scripts/setlocalversion
@@ -126,7 +126,7 @@  scm_version()
 
 collect_files()
 {
-	local file res
+	local file res=
 
 	for file; do
 		case "$file" in