Subject: bin/26058: /bin/sh parameter expansion / word splitting bug
To: None <gnats-bugs@gnats.netbsd.org>
From: Darrin B. Jewell <dbj@netbsd.org>
List: netbsd-bugs
Date: 06/25/2004 13:27:50
>Number:         26058
>Category:       bin
>Synopsis:       /bin/sh parameter expansion / word splitting bug
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jun 25 17:33:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Darrin B. Jewell
>Release:        2.0_BETA as of mid may 2004 and earlier
>Organization:
>Environment:
$  ident /bin/sh
/bin/sh:
     $NetBSD: crt0.c,v 1.13 2003/07/26 19:24:27 salo Exp $
     $NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $
     $NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $
     $NetBSD: error.c,v 1.31 2003/08/07 09:05:30 agc Exp $
     $NetBSD: eval.c,v 1.75 2003/11/14 10:27:10 dsl Exp $
     $NetBSD: exec.c,v 1.37 2003/08/07 09:05:31 agc Exp $
     $NetBSD: expand.c,v 1.60 2003/12/21 08:32:39 jdolecek Exp $
     $NetBSD: histedit.c,v 1.34 2003/10/27 06:19:29 lukem Exp $
     $NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $
     $NetBSD: jobs.c,v 1.62 2003/12/18 00:56:05 christos Exp $
     $NetBSD: mail.c,v 1.16 2003/08/07 09:05:33 agc Exp $
     $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $
     $NetBSD: memalloc.c,v 1.28 2003/08/07 09:05:34 agc Exp $
     $NetBSD: miscbltin.c,v 1.32 2003/08/07 09:05:35 agc Exp $
     $NetBSD: mystring.c,v 1.16 2003/08/07 09:05:35 agc Exp $
     $NetBSD: options.c,v 1.36 2004/01/05 23:23:32 jmmv Exp $
     $NetBSD: parser.c,v 1.55 2003/08/07 09:05:37 agc Exp $
     $NetBSD: redir.c,v 1.28 2003/08/07 09:05:37 agc Exp $
     $NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $
     $NetBSD: trap.c,v 1.30 2003/08/26 18:13:25 jmmv Exp $
     $NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $
     $NetBSD: var.c,v 1.34 2003/08/26 18:14:24 jmmv Exp $
     $NetBSD: test.c,v 1.25 2002/05/25 23:12:16 wiz Exp $
     $NetBSD: printf.c,v 1.29 2003/08/07 11:15:33 agc Exp $
     $NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $
     $NetBSD: skeleton.c,v 1.25 2003/08/07 11:17:54 agc Exp $
     $NetBSD: arith.y,v 1.17 2003/09/17 17:33:36 jmmv Exp $
     $NetBSD: arith_lex.l,v 1.12 2003/08/07 09:05:30 agc Exp $

>Description:
/bin/sh fails to split words after the following parameter expansion
  ${foo:+bar baz}

Both ksh and bash appear to do it correctly as I read the
spec available at opengroup.org

>How-To-Repeat:

  $ touch bar
  $ touch baz
  $ touch 'bar baz'
  $ sh -c 'foo=x; ls -l ${foo:+bar baz}'
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:24 bar baz
  $ bash -c 'foo=x; ls -l ${foo:+bar baz}'
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:23 bar
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:23 baz
  $ ksh -c 'foo=x; ls -l ${foo:+bar baz}'
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:23 bar
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:23 baz

Also please make sure that the fix also does the
correct thing for this case:

  $ sh -c 'foo="bar baz"; ls ${foo:+-l "${foo}"}'
  ls: unknown option --  
  usage: ls [-AaBbCcdFfgikLlmnopqRrSsTtuWwx1] [file ...]
  $ bash -c 'foo="bar baz"; ls ${foo:+-l "${foo}"}'
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:24 bar baz
  $ ksh -c 'foo="bar baz"; ls ${foo:+-l "${foo}"}'
  -rw-r--r--  1 dbj  dbj  0 Jun 25 13:24 bar baz

Since it more closely represents the problem I had
when I first discovered this bug.

>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: