printf does not return error on readonly assignment

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

printf does not return error on readonly assignment

Arnaud Gaillard
Hello,

Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: cygwin
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash.exe' -DCONF_HOSTTYPE='i686'
-DCONF_OSTYPE='cygwin' -DCONF_MACHTYPE='i686-pc-cygwin'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H -DRECYCLES_PIDS   -I.
-I/usr/src/bash-4.4.12-3.i686/src/bash-4.4
-I/usr/src/bash-4.4.12-3.i686/src/bash-4.4/include
-I/usr/src/bash-4.4.12-3.i686/src/bash-4.4/lib  -DWORDEXP_OPTION -ggdb
-O2 -pipe -Wimplicit-function-declaration
-fdebug-prefix-map=/usr/src/bash-4.4.12-3.i686/build=/usr/src/debug/bash-4.4.12-3
-fdebug-prefix-map=/usr/src/bash-4.4.12-3.i686/src/bash-4.4=/usr/src/debug/bash-4.4.12-3
-Wno-parentheses -Wno-format-security
uname output: CYGWIN_NT-6.1-WOW Arnaud-PC 2.8.1(0.312/5/3) 2017-07-03
14:06 i686 Cygwin
Machine Type: i686-pc-cygwin

Bash Version: 4.4
Patch Level: 12
Release Status: release

Description:

            According to the help page of the `printf` builtin, it
should not return success when an error has occurred during the
assignment:

> Exit Status:
> Returns success unless an invalid option is given or a write or assignment
> error occurs.

Assigning a value to a `readonly` variable returns an error code:

> bash$ readonly a=2
> bash$ a=3
> -bash: a: readonly variable
> bash$ echo $?
> 1

However, assigning the value to a `readonly` variable via `printf -v`
returns success:

> bash$ readonly b=2
> bash$ printf -v b 3
> -bash: b: readonly variable
> bash$ echo $?
> 0

I don't know whether this is an error coming from `help` or a bug of *bash*,
but it seems more logical to me that this is an error coming from *bash*.
Indeed, trying to use `printf` to assign a value to `$1` for example,
does return a error:

> bash$ printf -v 1 2
> -bash: `1': not a valid identifier
> bash$ echo $?
> 2

Repeat-By:

> bash$ readonly var=1
> bash$ var=2
> -bash: var: readonly variable
> bash$ echo $?
> 1
> bash$ printf -v var 2
> -bash: var: readonly variable
> echo $?
> 0


Sincerely yours,

--
Arnaud GAILLARD

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: printf does not return error on readonly assignment

Chet Ramey
On 7/6/17 12:41 AM, Arnaud Gaillard wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
>
> Description:
>
>             According to the help page of the `printf` builtin, it
> should not return success when an error has occurred during the
> assignment:
>
>> Exit Status:
>> Returns success unless an invalid option is given or a write or assignment
>> error occurs.
>
> Assigning a value to a `readonly` variable returns an error code:
>
>> bash$ readonly a=2
>> bash$ a=3
>> -bash: a: readonly variable
>> bash$ echo $?
>> 1
>
> However, assigning the value to a `readonly` variable via `printf -v`
> returns success:

Thanks for the report.  This is a reasonable improvement to make.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://cnswww.cns.cwru.edu/~chet/

Loading...