#2324 Issue closed: In general we must use local variables in functions (and also in scripts)

Labels: bug, cleanup, no-issue-activity

jsmeix opened issue at 2020-01-30 13:57:

In current ReaR code most variables are non-local
which means most variables are global.

In particular variables that are set in functions
must be in general local variables to avoid
that a variable with same name that exists
outside of the function (e.g. a variable in a script)
gets set to an unintended new value by the function.

Because often variables in ReaR have very short
generic names like dev the problem happens by accident
more often as it would with more specific variable names,
cf. "Code must be easy to read" in
https://github.com/rear/rear/wiki/Coding-Style

For example think about something like

# function showdev () { dev=$1 ; echo $dev ; }

# dev=sda1

# showdev sdb2
sdb2

# echo $dev
sdb2

versus

# function showdev () { local dev=$1 ; echo $dev ; }

# dev=sda1

# showdev sdb2
sdb2

# echo $dev
sda1

The dev example is intentional because
I know we do have a case like that in ReaR, but
right now I do not remember where I had noticed it...

jsmeix commented at 2020-01-30 15:44:

...perhaps the actual case in ReaR was not about dev but
about disk or something similar - as far as I vaguely remember
the issue was in the layout code or related to the layout code...

github-actions commented at 2020-06-26 01:39:

Stale issue message


[Export of Github issue for rear/rear.]