RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 1317303 - zsh -n does not detect incorrect associative array assignment
Summary: zsh -n does not detect incorrect associative array assignment
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: zsh
Version: 7.2
Hardware: All
OS: Linux
medium
high
Target Milestone: rc
: ---
Assignee: Kamil Dudka
QA Contact: BaseOS QE - Apps
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-03-13 22:40 UTC by Paul Wayper
Modified: 2019-11-14 07:35 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-03-24 09:31:15 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description Paul Wayper 2016-03-13 22:40:16 UTC
Description of problem:

zsh has the -n option to do a syntax test of the script and not actually execute it.  This test does not detect and warn on incorrect associative array declaration, whereas execution of the script breaks at that point.

Version-Release number of selected component (if applicable):

zsh 5.0.2

How reproducible:

Always

Steps to Reproduce:

###1. Create the following scripts:
$ cat zsh_array.zsh 
#!/usr/bin/zsh

typeset -A fn
fn=(foo_key foo_val bar_key bar_val)

printf %s\\n ${fn[foo_key]} ${fn[bar_key]}

$ cat zsh_bad_array.zsh 
#!/usr/bin/zsh

typeset -A fn
fn=(foo_key foo_val bar_key)

printf %s\\n $fn[foo_key] $fn[bar_key]

$ cat ksh_array.ksh 
#!/usr/bin/ksh

typeset -A fn
fn=([foo_key]=foo_val [bar_key]=bar_val)

printf %s\\n ${fn[foo_key]} ${fn[bar_key]}

###2. Test:
ksh -n ksh_array.ksh && echo result = $?
ksh ksh_array.ksh

###3. Test:
ksh -n ksh_bad_array.ksh && echo result = $?
ksh ksh_bad_array.ksh

###4. Test:
ksh -n zsh_array.zsh && echo result = $?
ksh zsh_array.zsh


Actual results:

###2. Test passes and execution passes:
$ ksh -n ksh_array.ksh && echo result = $?
result = 0
$ ksh ksh_array.ksh
foo_val
bar_val

###3. Test passes but execution fails:
$ ksh -n ksh_bad_array.ksh && echo result = $?
result = 0
$ ksh ksh_bad_array.ksh
ksh_bad_array.ksh[4]: [bar_key]: not found [No such file or directory]

###4. Test passes but execution fails:
$ ksh -n zsh_array.zsh && echo result = $?
result = 0
$ ksh zsh_array.zsh
zsh_array.zsh: line 4: cannot append index array to associative array fn

Expected results:

###2. Test passes and execution passes:
$ ksh -n ksh_array.ksh && echo result = $?
result = 0
$ ksh ksh_array.ksh
foo_val
bar_val

###3. Test passes but execution fails:
$ ksh -n ksh_bad_array.ksh && echo result = $?
ksh_bad_array.ksh[4]: [bar_key]: not found [No such file or directory]
result = 1
$ ksh ksh_bad_array.ksh
ksh_bad_array.ksh[4]: [bar_key]: not found [No such file or directory]

###4. Test passes but execution fails:
$ ksh -n zsh_array.zsh && echo result = $?
zsh_array.zsh: line 4: cannot append index array to associative array fn
result = 1
$ ksh zsh_array.zsh
zsh_array.zsh: line 4: cannot append index array to associative array fn

Additional info:

As zsh is designed to be mostly identical to ksh in execution but has a different associative array declaration syntax, zsh -n should work as a test of whether we are trying to execute a ksh or zsh shell script.

Comment 2 Kamil Dudka 2016-03-14 15:40:23 UTC
I find this bug report very difficult to understand.  The bug summary says that you want to make zsh detect incorrect uses of associative arrays ... but in the expected output you are saying that the syntax check should pass in all three cases?

Why don't you simply use the syntax that works in both ksh and zsh?

typeset -A fn
fn[foo_key]=foo_val 
fn[bar_key]=bar_val

Comment 3 Paul Wayper 2016-03-16 03:00:34 UTC
Hi Kamil,

Yes, you can use the syntax that works in both.  But they also support, and document, the multiple assignment method - the zsh wiki at:

http://zshwiki.org/home/scripting/array

gives the first example of associative array assignment as:

typeset -A buffer
buffer=( key1 val1 key2 val2 )

The syntax check should not pass in all three cases - only in test 2 (the correct multiple assignment syntax) should it pass.  Indeed, the problem is that at the moment the syntax check passes but the code is syntactically incorrect.

Hope this helps,

Paul

Comment 4 Kamil Dudka 2016-03-16 17:08:59 UTC
(In reply to Paul Wayper from comment #3)
> Yes, you can use the syntax that works in both.  But they also support, and
> document, the multiple assignment method - the zsh wiki at:
> 
> http://zshwiki.org/home/scripting/array
> 
> gives the first example of associative array assignment as:
> 
> typeset -A buffer
> buffer=( key1 val1 key2 val2 )

... and it works as documented, does not it?

> The syntax check should not pass in all three cases - only in test 2 (the
> correct multiple assignment syntax) should it pass. Indeed, the problem is
> that at the moment the syntax check passes but the code is syntactically
> incorrect.

Since shell is an interpreted language, not all "syntactical" errors could be detected without actually executing the code.

In any case, the described behavior of RHEL-7 zsh matches the behavior of upstream zsh.  Neither ksh is able detect such kinds of programming mistakes while using the -n option.  I do not think we can easily improve anything in this regard.

Comment 5 Paul Wayper 2016-03-21 03:05:37 UTC
Hi Kamil,

I'm not sure how you go from "shell is an interpreted language" to "can't detect some syntax errors".  The '-n' option specifically says in the documentation that it does a syntax check on the script.  It doesn't mention any exceptions.

I believe what you're saying is "we must go upstream to fix this bug"; would you like to start that conversation upstream or shall I?

Thanks in advance,

Paul

Comment 6 Kamil Dudka 2016-03-21 06:31:02 UTC
(In reply to Paul Wayper from comment #5)
> I'm not sure how you go from "shell is an interpreted language" to "can't
> detect some syntax errors".

zsh can detect pretty much all syntax errors.  What I am saying is that it cannot detect some syntax errors *without* actually executing the code.  Obviously, all languages that support 'eval' and the like have to have some limitations in this regard.

> The '-n' option specifically says in the
> documentation that it does a syntax check on the script.  It doesn't mention
> any exceptions.

Which documentation?

If it says that all syntax errors are guaranteed to be reported, we just need to reword the documentation.

> I believe what you're saying is "we must go upstream to fix this bug"; would
> you like to start that conversation upstream or shall I?

Please do.

Comment 7 Paul Wayper 2016-03-22 22:15:17 UTC
OK, this is what I've written to the zsh-workers address:

Hi there!

Sourceforge has pointed me to this list as a way to get a bug fixed in zsh.

I've discovered that an incorrect associative array declaration in zsh isn't detected via 'zsh -n script.zsh', even though it does get flagged when the script is executed.  For example:

$ cat zsh_array.zsh 
#!/usr/bin/zsh

typeset -A fn
fn=(foo_key foo_val bar_key bar_val)

printf %s\\n ${fn[foo_key]} ${fn[bar_key]}

$ cat zsh_bad_array.zsh 
#!/usr/bin/zsh

typeset -A fn
fn=(foo_key foo_val bar_key)

printf %s\\n $fn[foo_key] $fn[bar_key]

$ zsh -n zsh_array.zsh && echo $?
0
$ zsh zsh_array.zsh
foo_val bar_val

# And yet:
$ zsh -n zsh_bad_array.zsh && echo $?
0
$ zsh zsh_bad_array.zsh
/tmp/zsh_bad_array.zsh:4: bad set of key/value pairs for associative array


This syntax for associative array declaration is the one documented as correct:

http://zshwiki.org/home/scripting/array

gives the first example of associative array assignment as:

typeset -A buffer
buffer=( key1 val1 key2 val2 )

Ideally what I'd like is for zsh -n to give the same error message on an incorrect associative array declaration as zsh gives when executing the script.

Thanks in advance,

Paul

Comment 8 Kamil Dudka 2016-03-23 07:25:46 UTC
(In reply to Paul Wayper from comment #7)
> OK, this is what I've written to the zsh-workers at zsh dot org address:

... and this is the response from upstream developers (not a bug):

http://www.zsh.org/mla/workers/2016/msg00742.html

Comment 9 Kamil Dudka 2016-03-24 09:31:15 UTC
zsh (as well as other shells) is not designed to detect this kind of programming mistakes without actually executing the code.  Please suggest the customer to use the solution proposed in comment #2.


Note You need to log in before you can comment on or make changes to this bug.