GNU bug report logs - #6124
basename/dirname are not POSIX compliant on arg starting with a dash

Previous Next

Package: coreutils;

Reported by: Vincent Lefevre <vincent <at> vinc17.net>

Date: Thu, 6 May 2010 11:36:02 UTC

Severity: normal

Tags: notabug

Merged with 6123

Done: Jim Meyering <jim <at> meyering.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 6124 in the body.
You can then email your comments to 6124 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 11:36:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Vincent Lefevre <vincent <at> vinc17.net>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Thu, 06 May 2010 11:36:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Vincent Lefevre <vincent <at> vinc17.net>
To: bug-coreutils <at> gnu.org
Cc: 580492 <at> bugs.debian.org
Subject: basename/dirname are not POSIX compliant on arg starting with a dash
Date: Thu, 6 May 2010 13:15:02 +0200
Hi,

I've reported the following bug in the Debian BTS:

  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=580492

With the basename and dirname utilities from GNU Coreutils, one has:

ypig% POSIXLY_CORRECT=1 basename --
basename: missing operand
Try `basename --help' for more information.
ypig% POSIXLY_CORRECT=1 dirname --
dirname: missing operand
Try `dirname --help' for more information.

instead of:

ypig% busybox basename --
--
ypig% busybox dirname --
.

According to POSIX[*], basename and dirname take no options (examples
in POSIX also assume that), so that this would make BusyBox's behavior
correct.

[*] http://www.opengroup.org/onlinepubs/9699919799/utilities/basename.html
    http://www.opengroup.org/onlinepubs/9699919799/utilities/dirname.html

-- 
Vincent Lefèvre <vincent <at> vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)





Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 12:53:01 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Vincent Lefevre <vincent <at> vinc17.net>, bug-coreutils <at> gnu.org, 
	580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 13:50:27 +0100
On 06/05/10 12:15, Vincent Lefevre wrote:
> Hi,
> 
> I've reported the following bug in the Debian BTS:
> 
>   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=580492
> 
> With the basename and dirname utilities from GNU Coreutils, one has:
> 
> ypig% POSIXLY_CORRECT=1 basename --
> basename: missing operand
> Try `basename --help' for more information.
> ypig% POSIXLY_CORRECT=1 dirname --
> dirname: missing operand
> Try `dirname --help' for more information.
> 
> instead of:
> 
> ypig% busybox basename --
> --
> ypig% busybox dirname --
> .
> 
> According to POSIX[*], basename and dirname take no options (examples
> in POSIX also assume that), so that this would make BusyBox's behavior
> correct.
> 
> [*] http://www.opengroup.org/onlinepubs/9699919799/utilities/basename.html
>     http://www.opengroup.org/onlinepubs/9699919799/utilities/dirname.html
> 

The above busybox behavior implies that
if you write a robust script like this for coreutils,
that it is not portable to busybox:

base=$(basename -- "$path")

That also implies that we can't change the behavior.
Because there is an optional suffix parameter, one can't
know which behavior is required for the above example.
I suppose one could choose based on POSIXLY_CORRECT.

Note solaris behaves like busybox and openbsd behaves like coreutils.

cheers,
Pádraig.





Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 13:25:03 GMT) Full text and rfc822 format available.

Message #11 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Vincent Lefevre <vincent <at> vinc17.net>
To: Pádraig Brady <P <at> draigBrady.com>
Cc: bug-coreutils <at> gnu.org, 580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg
	starting with a dash
Date: Thu, 6 May 2010 15:23:38 +0200
On 2010-05-06 13:50:27 +0100, Pádraig Brady wrote:
> The above busybox behavior implies that
> if you write a robust script like this for coreutils,
> that it is not portable to busybox:
> 
> base=$(basename -- "$path")

If one follows POSIX, the correct way to do it is:

  base=$(basename "$path")

and this is robust because the first argument is necessarily the path
(it cannot be an option, even "--").

> That also implies that we can't change the behavior.

According to

  grep 'basename --' /usr/bin/*
  grep 'basename \$' /usr/bin/*

on my Debian machine, almost all scripts use basename without "--".
The only exceptions are autoconf (but I'm not sure this counts, as
there's a test for "basename --" first, and I don't know what it
really expects) and savelog (from the debianutils package, thus
specific to Debian, but savelog also uses `basename $0`, so that
it is already inconsistent). Therefore I assume that changing
coreutils would be easier.

> Because there is an optional suffix parameter, one can't
> know which behavior is required for the above example.
> I suppose one could choose based on POSIXLY_CORRECT.

I think that it should really behave correctly if POSIXLY_CORRECT
is set, but also if it isn't set, given what appears to be the
current practice.

> Note solaris behaves like busybox and openbsd behaves like coreutils.

Perhaps they're copying coreutils without looking at what POSIX says.

-- 
Vincent Lefèvre <vincent <at> vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)





Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 13:43:01 GMT) Full text and rfc822 format available.

Message #14 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Vincent Lefevre <vincent <at> vinc17.net>, bug-coreutils <at> gnu.org, 
	580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 07:41:59 -0600
[Message part 1 (text/plain, inline)]
On 05/06/2010 05:15 AM, Vincent Lefevre wrote:
> Hi,
> 
> I've reported the following bug in the Debian BTS:
> 
>   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=580492

That bug should be reassigned to busybox.

> 
> With the basename and dirname utilities from GNU Coreutils, one has:
> 
> ypig% POSIXLY_CORRECT=1 basename --
> basename: missing operand

Correct.

> 
> ypig% busybox basename --
> --

Buggy.

> According to POSIX[*], basename and dirname take no options (examples
> in POSIX also assume that), so that this would make BusyBox's behavior
> correct.

No, you're missing one other key point of POSIX:

http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html
Section 1.4, OPTIONS:

    Default Behavior: When this section is listed as "None.", it means
that the implementation need not support any options. Standard utilities
that do not accept options, but that do accept operands, shall recognize
"--" as a first argument to be discarded.

    The requirement for recognizing "--" is because conforming
applications need a way to shield their operands from any arbitrary
options that the implementation may provide as an extension. For
example, if the standard utility foo is listed as taking no options, and
the application needed to give it a pathname with a leading <hyphen>, it
could safely do it as:

    foo -- -myfile

    and avoid any problems with -m used as an extension.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 13:51:02 GMT) Full text and rfc822 format available.

Message #17 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
Cc: bug-coreutils <at> gnu.org, Vincent Lefevre <vincent <at> vinc17.net>,
	580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 07:49:31 -0600
[Message part 1 (text/plain, inline)]
On 05/06/2010 07:41 AM, Eric Blake wrote:
>> According to POSIX[*], basename and dirname take no options (examples
>> in POSIX also assume that), so that this would make BusyBox's behavior
>> correct.
> 
> No, you're missing one other key point of POSIX:
> 
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html
> Section 1.4, OPTIONS:

If you want more evidence from POSIX that 'basename -- foo' is mandated
behavior, read up on this correction to POSIX:
http://austingroupbugs.net/view.php?id=192

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 14:10:02 GMT) Full text and rfc822 format available.

Message #20 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Vincent Lefevre <vincent <at> vinc17.net>, Pádraig Brady <P <at> draigBrady.com>,
	bug-coreutils <at> gnu.org, 580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 08:02:04 -0600
[Message part 1 (text/plain, inline)]
On 05/06/2010 07:23 AM, Vincent Lefevre wrote:
> If one follows POSIX, the correct way to do it is:
> 
>   base=$(basename "$path")
> 
> and this is robust because the first argument is necessarily the path
> (it cannot be an option, even "--").

POSIX does not forbid applications from adding extension options.  And
BSD basename has done just that:

SYNOPSIS
     basename string [suffix]
     basename [-a] [-s suffix] string [...]
     dirname string [...]

And since POSIX allows FreeBSD's extensions to exist (although a
portable script will not use them), it must also require that a portable
app be able to guarantee a way to bypass those extensions (ergo
base=$(basename -- "$path") is a POSIX requirement).

> According to
> 
>   grep 'basename --' /usr/bin/*
>   grep 'basename \$' /usr/bin/*
> 
> on my Debian machine, almost all scripts use basename without "--".
> The only exceptions are autoconf (but I'm not sure this counts, as
> there's a test for "basename --" first,

Can you find an actual example of this?  I think you read it
incorrectly, as really, autoconf first tests for 'basename -- /', but if
you indeed found an instance of autoconf testing for 'basename --', that
would be a bug in autoconf that needs to be reported to that list.

>> Note solaris behaves like busybox and openbsd behaves like coreutils.
> 
> Perhaps they're copying coreutils without looking at what POSIX says.

Or, more likely, perhaps they are independently obeying what POSIX says,
without reference to either coreutils or busybox.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 14:28:01 GMT) Full text and rfc822 format available.

Message #23 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Vincent Lefevre <vincent <at> vinc17.net>
To: Eric Blake <eblake <at> redhat.com>
Cc: bug-coreutils <at> gnu.org, 580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg
	starting with a dash
Date: Thu, 6 May 2010 16:24:10 +0200
On 2010-05-06 07:41:59 -0600, Eric Blake wrote:
> No, you're missing one other key point of POSIX:
> 
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html
> Section 1.4, OPTIONS:
> 
>     Default Behavior: When this section is listed as "None.", it means
> that the implementation need not support any options. Standard utilities
> that do not accept options, but that do accept operands, shall recognize
> "--" as a first argument to be discarded.

OK, an alternative way would be that an exception is added to POSIX,
just like for "echo".

>     The requirement for recognizing "--" is because conforming
> applications need a way to shield their operands from any arbitrary
> options that the implementation may provide as an extension. For
> example, if the standard utility foo is listed as taking no options, and
> the application needed to give it a pathname with a leading <hyphen>, it
> could safely do it as:
> 
>     foo -- -myfile
> 
>     and avoid any problems with -m used as an extension.

However, if extensions are allowed, this would mean that many scripts
would have to be fixed.

-- 
Vincent Lefèvre <vincent <at> vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)





Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 14:31:02 GMT) Full text and rfc822 format available.

Message #26 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Vincent Lefevre <vincent <at> vinc17.net>, bug-coreutils <at> gnu.org,
	580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 08:29:57 -0600
[Message part 1 (text/plain, inline)]
On 05/06/2010 08:24 AM, Vincent Lefevre wrote:
>>     Default Behavior: When this section is listed as "None.", it means
>> that the implementation need not support any options. Standard utilities
>> that do not accept options, but that do accept operands, shall recognize
>> "--" as a first argument to be discarded.
> 
> OK, an alternative way would be that an exception is added to POSIX,
> just like for "echo".

Please no.  echo has an exception precisely because it has historical
baggage, and that is what makes echo so pitiful that POSIX itself states
that printf(1) is more portable than echo(1).  Relatively newer
applications, like basename and dirname, that were first written in an
age when consistency was more important, should not be burdened with the
historical baggage of ancient echo(1).  And changing it now is too late;
just as you argue about the number of broken scripts that are not robust
because they failed to use --, you have to also consider the number of
correct scripts that have been written with the correct assumption that
basename and dirname accept --: it has been 9 years since POSIX 2001
first codified that behavior.

Besides, we would much prefer to keep 'basename --help' as a GNU
extension.  Not having 'POSIXLY_CORRECT=1 /bin/echo --help' give useful
help is an annoyance that we would rather avoid repeating.

> 
> However, if extensions are allowed, this would mean that many scripts
> would have to be fixed.

Yes, many broken scripts need to be fixed.  But that is the fault of
those broken scripts, not coreutils.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 15:40:03 GMT) Full text and rfc822 format available.

Message #29 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Vincent Lefevre <vincent <at> vinc17.net>
To: Eric Blake <eblake <at> redhat.com>
Cc: bug-coreutils <at> gnu.org, Pádraig Brady <P <at> draigBrady.com>,
	580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg
	starting with a dash
Date: Thu, 6 May 2010 17:38:51 +0200
On 2010-05-06 08:02:04 -0600, Eric Blake wrote:
> On 05/06/2010 07:23 AM, Vincent Lefevre wrote:
> > According to
> > 
> >   grep 'basename --' /usr/bin/*
> >   grep 'basename \$' /usr/bin/*
> > 
> > on my Debian machine, almost all scripts use basename without "--".
> > The only exceptions are autoconf (but I'm not sure this counts, as
> > there's a test for "basename --" first,
> 
> Can you find an actual example of this?  I think you read it
> incorrectly, as really, autoconf first tests for 'basename -- /', but if
> you indeed found an instance of autoconf testing for 'basename --', that
> would be a bug in autoconf that needs to be reported to that list.

Sorry, I meant it was testing basename with "--" as the first
argument while in this case it is useless. So, I wondered whether
the goal of this test was to test whether this form was supported.

> >> Note solaris behaves like busybox and openbsd behaves like coreutils.
> > 
> > Perhaps they're copying coreutils without looking at what POSIX says.
> 
> Or, more likely, perhaps they are independently obeying what POSIX says,
> without reference to either coreutils or busybox.

So, I think I should reassign the bug to busybox?

-- 
Vincent Lefèvre <vincent <at> vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)





Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6124; Package coreutils. (Thu, 06 May 2010 16:54:01 GMT) Full text and rfc822 format available.

Message #32 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Vincent Lefevre <vincent <at> vinc17.net>, Pádraig Brady <P <at> draigBrady.com>,
	bug-coreutils <at> gnu.org, 580492 <at> bugs.debian.org
Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting
	with a dash
Date: Thu, 06 May 2010 10:52:55 -0600
[Message part 1 (text/plain, inline)]
On 05/06/2010 09:38 AM, Vincent Lefevre wrote:
>>> there's a test for "basename --" first,
>>
>> Can you find an actual example of this?  I think you read it
>> incorrectly, as really, autoconf first tests for 'basename -- /', but if
>> you indeed found an instance of autoconf testing for 'basename --', that
>> would be a bug in autoconf that needs to be reported to that list.
> 
> Sorry, I meant it was testing basename with "--" as the first
> argument while in this case it is useless. So, I wondered whether
> the goal of this test was to test whether this form was supported.

Yes, the goal of that autoconf test is to determine whether basename(1)
supports leading --, as required by POSIX.

>> Or, more likely, perhaps they are independently obeying what POSIX says,
>> without reference to either coreutils or busybox.
> 
> So, I think I should reassign the bug to busybox?

Yes.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Merged 6123 6124. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 07 May 2010 17:23:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 6123 <at> debbugs.gnu.org and Vincent Lefevre <vincent <at> vinc17.net> Request was from Jim Meyering <jim <at> meyering.net> to control <at> debbugs.gnu.org. (Sun, 17 Apr 2011 08:43:02 GMT) Full text and rfc822 format available.

Added tag(s) notabug. Request was from Jim Meyering <jim <at> meyering.net> to control <at> debbugs.gnu.org. (Sun, 17 Apr 2011 08:43:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 15 May 2011 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 14 years and 117 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.