All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
@ 2021-07-09 14:01 Petr Vorel
  2021-07-12  2:00 ` Li Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Vorel @ 2021-07-09 14:01 UTC (permalink / raw)
  To: ltp

From: Petr Vorel <petr.vorel@gmail.com>

in tst_ipaddr_un(). This is required to fix problem on dash, which
(unlike bash and busybox ash implementation) takes value of previously
defined variable, i.e.  guarding with local does not work:

$ foo=ee; bar() { local foo; echo "foo: '$foo'"; }; bar
foo: 'ee'

It requires declare it as empty:
$ foo=ee; bar() { local foo=; echo "foo: '$foo'"; }; bar
foo: ''

This problem seems to be on various dash versions from 0.5.8 to
0.5.11+git20200708+dd9ef66-5 (on Debian oldstable, stable, testing).
It'd require to set all variables, but now fix just prefix, which is
used in build.sh and it's quite common name.

Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/lib/tst_net.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index db1db4790..52975f37b 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -408,7 +408,7 @@ tst_ipaddr_un()
 	local max_net_id=$default_max
 	local min_net_id=0
 
-	local counter host_id host_range is_counter max_host_id min_host_id net_id prefix tmp type
+	local counter host_id host_range is_counter max_host_id min_host_id net_id prefix= tmp type
 
 	local OPTIND
 	while getopts "c:h:n:p" opt; do
-- 
2.26.2


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

* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
  2021-07-09 14:01 [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty Petr Vorel
@ 2021-07-12  2:00 ` Li Wang
  2021-07-12  5:49   ` Petr Vorel
  0 siblings, 1 reply; 6+ messages in thread
From: Li Wang @ 2021-07-12  2:00 UTC (permalink / raw)
  To: ltp

On Fri, Jul 9, 2021 at 10:01 PM Petr Vorel <pvorel@suse.cz> wrote:

> From: Petr Vorel <petr.vorel@gmail.com>
>
> in tst_ipaddr_un(). This is required to fix problem on dash, which
> (unlike bash and busybox ash implementation) takes value of previously
> defined variable, i.e.  guarding with local does not work:
>
> $ foo=ee; bar() { local foo; echo "foo: '$foo'"; }; bar
> foo: 'ee'
>

I'm thinking maybe we should avoid defining the same name
variables like that. But anyway, your fix should be worked.

Reviewed-by: Li Wang <liwang@redhat.com>



>
> It requires declare it as empty:
> $ foo=ee; bar() { local foo=; echo "foo: '$foo'"; }; bar
> foo: ''
>
> This problem seems to be on various dash versions from 0.5.8 to
> 0.5.11+git20200708+dd9ef66-5 (on Debian oldstable, stable, testing).
> It'd require to set all variables, but now fix just prefix, which is
> used in build.sh and it's quite common name.
>
> Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>  testcases/lib/tst_net.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
> index db1db4790..52975f37b 100644
> --- a/testcases/lib/tst_net.sh
> +++ b/testcases/lib/tst_net.sh
> @@ -408,7 +408,7 @@ tst_ipaddr_un()
>         local max_net_id=$default_max
>         local min_net_id=0
>
> -       local counter host_id host_range is_counter max_host_id
> min_host_id net_id prefix tmp type
> +       local counter host_id host_range is_counter max_host_id
> min_host_id net_id prefix= tmp type
>
>         local OPTIND
>         while getopts "c:h:n:p" opt; do
> --
> 2.26.2
>
>

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210712/436494f8/attachment.htm>

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

* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
  2021-07-12  2:00 ` Li Wang
@ 2021-07-12  5:49   ` Petr Vorel
  2021-07-12  5:57     ` Joerg Vehlow
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Vorel @ 2021-07-12  5:49 UTC (permalink / raw)
  To: ltp

Hi Li,
> On Fri, Jul 9, 2021 at 10:01 PM Petr Vorel <pvorel@suse.cz> wrote:

> > From: Petr Vorel <petr.vorel@gmail.com>

> > in tst_ipaddr_un(). This is required to fix problem on dash, which
> > (unlike bash and busybox ash implementation) takes value of previously
> > defined variable, i.e.  guarding with local does not work:

> > $ foo=ee; bar() { local foo; echo "foo: '$foo'"; }; bar
> > foo: 'ee'


> I'm thinking maybe we should avoid defining the same name
> variables like that. But anyway, your fix should be worked.

> Reviewed-by: Li Wang <liwang@redhat.com>

Thanks for a review!
It's a bit strange to add '=' only single variable, maybe I should have rename
the variable.

Paranoid approach would be to add '=' to any variable which is assigned only on
some circumstance or even any variable which is not assigned to any value.
But we probably don't want to do it.

Kind regards,
Petr

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

* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
  2021-07-12  5:49   ` Petr Vorel
@ 2021-07-12  5:57     ` Joerg Vehlow
  2021-07-12  6:40       ` Petr Vorel
  2021-07-12 16:12       ` Petr Vorel
  0 siblings, 2 replies; 6+ messages in thread
From: Joerg Vehlow @ 2021-07-12  5:57 UTC (permalink / raw)
  To: ltp

Hi,

On 7/12/2021 7:49 AM, Petr Vorel wrote:
> Thanks for a review!
> It's a bit strange to add '=' only single variable, maybe I should have rename
> the variable.
>
> Paranoid approach would be to add '=' to any variable which is assigned only on
> some circumstance or even any variable which is not assigned to any value.
> But we probably don't want to do it.
I think good common practice would be to add it to all variables, where 
it matters.
There are probably only very few places, where a local variable is not 
assigned before being accessed.
Maybe there is some kind of shell code linter, that can find 
uninitialized variables?
A good linter could also find variables, that should be local, but are 
not. But I have never looked into shell code linting.


In fs_bind_lib.sh, OPTIND in fs_bind_check probably also requires 
setting to empty, or better 0.
But maybe dash's implementation of getopts does not require it. I only 
had to add local OPTIND for busybox sh iirc...

Yet another case of: Shellcode is unpredictable :)


Joerg

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

* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
  2021-07-12  5:57     ` Joerg Vehlow
@ 2021-07-12  6:40       ` Petr Vorel
  2021-07-12 16:12       ` Petr Vorel
  1 sibling, 0 replies; 6+ messages in thread
From: Petr Vorel @ 2021-07-12  6:40 UTC (permalink / raw)
  To: ltp

Hi Joerg,

> Hi,

> On 7/12/2021 7:49 AM, Petr Vorel wrote:
> > Thanks for a review!
> > It's a bit strange to add '=' only single variable, maybe I should have rename
> > the variable.

> > Paranoid approach would be to add '=' to any variable which is assigned only on
> > some circumstance or even any variable which is not assigned to any value.
> > But we probably don't want to do it.
> I think good common practice would be to add it to all variables, where it
> matters.
> There are probably only very few places, where a local variable is not
> assigned before being accessed.
> Maybe there is some kind of shell code linter, that can find uninitialized
> variables?
> A good linter could also find variables, that should be local, but are not.
> But I have never looked into shell code linting.

Thanks for your input.

I'm aware only of shellcheck [1], which I'm not a big fan of (false positives,
useless hints). And yes, in this case it warns about 'local' not being POSIX
=> useless :(.

In testcases/lib/tst_net.sh line 411:
	local counter host_id host_range is_counter max_host_id min_host_id net_id prefix tmp type
        ^-- SC3043: In POSIX sh, 'local' is undefined.


> In fs_bind_lib.sh, OPTIND in fs_bind_check probably also requires setting to
> empty, or better 0.
We use tst_test.sh in OPTIND=1 ("The index of the next argument to be processed
by the getopts builtin command")

> But maybe dash's implementation of getopts does not require it. I only had
> to add local OPTIND for busybox sh iirc...
I used to set local OPTIND (in tst_net.sh), which is obviously not working on
dash.

> Yet another case of: Shellcode is unpredictable :)
Yep, I love shell (understand why everything is slowly rewritten into C).

Kind regards,
Petr

> Joerg

[1] https://github.com/koalaman/shellcheck

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

* [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty
  2021-07-12  5:57     ` Joerg Vehlow
  2021-07-12  6:40       ` Petr Vorel
@ 2021-07-12 16:12       ` Petr Vorel
  1 sibling, 0 replies; 6+ messages in thread
From: Petr Vorel @ 2021-07-12 16:12 UTC (permalink / raw)
  To: ltp

Hi all,

> in tst_ipaddr_un(). This is required to fix problem on dash, which
> (unlike bash and busybox ash implementation) takes value of previously
> defined variable, i.e.  guarding with local does not work:
> 
> $ foo=ee; bar() { local foo; echo "foo: '$foo'"; }; bar
> foo: 'ee'
> 
> It requires declare it as empty:
> $ foo=ee; bar() { local foo=; echo "foo: '$foo'"; }; bar
> foo: ''
> 
> This problem seems to be on various dash versions from 0.5.8 to
> 0.5.11+git20200708+dd9ef66-5 (on Debian oldstable, stable, testing).
> It'd require to set all variables, but now fix just prefix, which is
> used in build.sh and it's quite common name.

FYI this behavior is expected on dash, quoting man dash(1):
"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."

Kind regards,
Petr

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

end of thread, other threads:[~2021-07-12 16:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-09 14:01 [LTP] [PATCH 1/1] tst_net.sh: Declare prefix variable as empty Petr Vorel
2021-07-12  2:00 ` Li Wang
2021-07-12  5:49   ` Petr Vorel
2021-07-12  5:57     ` Joerg Vehlow
2021-07-12  6:40       ` Petr Vorel
2021-07-12 16:12       ` Petr Vorel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.