From ak03@gte.com  Wed Oct 11 12:42:21 2000
Return-Path: <ak03@gte.com>
Received: from h132-197-97-45.gte.com (h132-197-97-45.gte.com [132.197.97.45])
	by hub.freebsd.org (Postfix) with ESMTP
	id 8263737B787; Wed, 11 Oct 2000 12:17:39 -0700 (PDT)
Received: (from ak03@localhost)
	by h132-197-97-45.gte.com (8.11.0/8.11.0) id e9BJHOb80947;
	Wed, 11 Oct 2000 15:17:24 -0400 (EDT)
	(envelope-from ak03)
Message-Id: <200010111917.e9BJHOb80947@h132-197-97-45.gte.com>
Date: Wed, 11 Oct 2000 15:17:24 -0400 (EDT)
From: ak03@gte.com
Reply-To: ak03@gte.com
To: FreeBSD-gnats-submit@freebsd.org
Cc: des@freebsd.org
Subject: Unjustified basename code removal and subsequent breakage
X-Send-Pr-Version: 3.2

>Number:         21918
>Category:       bin
>Synopsis:       Revision 1.5 provides incomplete fix for 1.4 breakage by DES
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 11 12:50:01 PDT 2000
>Closed-Date:    Fri Jun 22 22:57:41 PDT 2001
>Last-Modified:  Fri Jun 22 22:57:53 PDT 2001
>Originator:     Alexander Kabaev
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
Verizon Laboratories Inc.
>Environment:

	FreeBSD-CURRENT PRE_SMPNG and later

>Description:

	The comment on the old (correct) revision states that:
	  If the suffix operand is present, is not identical to the
          characters remaining in string, and is identical to a suffix
          of the characters remaining in string, the suffix suffix
          shall be removed from string.	
	The new version removes suffix even when suffix is identical 
	to the remaining string itself. That is clearly not the 
	behaviour both comment and basename man page describe.

>How-To-Repeat:

	in sh: 
		% basename filename.ext filename.ext
                
		%

	in ksh:
		% basename filename.ext filename.ext
		filename.ext
		%
>Fix:


Index: basename.c
===================================================================
RCS file: /usr/ncvs/src/usr.bin/basename/basename.c,v
retrieving revision 1.5
diff -u -r1.5 basename.c
--- basename.c	2000/09/06 07:28:02	1.5
+++ basename.c	2000/10/11 19:00:43
@@ -56,7 +56,7 @@
 	int argc;
 	char **argv;
 {
-	char *p, *q;
+	char *p;
 	int ch;
 
 	while ((ch = getopt(argc, argv, "")) != -1)
@@ -73,8 +73,25 @@
 
 	if ((p = basename(argv[0])) == NULL)
 		err(1, "%s", argv[0]);
-	if (*++argv && (q = strstr(p, *argv)) && strcmp(q, *argv) == 0)
-		*q = '\0';
+
+	/*
+	 *     If the suffix operand is present, is not identical to the
+         *     characters remaining in string, and is identical to a suffix
+         *     of the characters remaining in string, the suffix
+	 *     shall be removed from string.
+	 */
+	if (*++argv) {
+		int suffixlen, stringlen, off;
+
+		suffixlen = strlen(*argv);
+		stringlen = strlen(p);
+
+		if (suffixlen < stringlen) {
+			off = stringlen - suffixlen;
+			if (!strcmp(p + off, *argv))
+				p[off] = '\0';
+		}
+	}
 	(void)printf("%s\n", p);
 	exit(0);
 }

>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: ak03@gte.com
Cc: FreeBSD-gnats-submit@FreeBSD.ORG, des@FreeBSD.ORG
Subject: Re: bin/21918: Unjustified basename code removal and subsequent
 breakage
Date: Thu, 12 Oct 2000 16:43:14 +1100 (EST)

 On Wed, 11 Oct 2000 ak03@gte.com wrote:
 
 > >Description:
 > 
 > 	The comment on the old (correct) revision states that:
 > 	  If the suffix operand is present, is not identical to the
 >           characters remaining in string, and is identical to a suffix
 >           of the characters remaining in string, the suffix suffix
 >           shall be removed from string.	
 > 	The new version removes suffix even when suffix is identical 
 > 	to the remaining string itself. That is clearly not the 
 > 	behaviour both comment and basename man page describe.
 
 basename(3) is remarkably feeble compared with basename(1).  Another case
 that has changed is `basename ""`.  basename(3) is specified to bogotify
 this case by changing "" to ".".
 
 Bruce
 
 

From: Dag-Erling Smorgrav <des@ofug.org>
To: ak03@gte.com
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/21918: Unjustified basename code removal and subsequent breakage
Date: 12 Oct 2000 10:27:39 +0200

 I will commit the following patch later today:
 
 Index: basename.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/basename/basename.c,v
 retrieving revision 1.5
 diff -u -r1.5 basename.c
 --- basename.c	2000/09/06 07:28:02	1.5
 +++ basename.c	2000/10/12 08:23:25
 @@ -73,8 +73,9 @@
  
  	if ((p = basename(argv[0])) == NULL)
  		err(1, "%s", argv[0]);
 -	if (*++argv && (q = strstr(p, *argv)) && strcmp(q, *argv) == 0)
 -		*q = '\0';
 +	if (*++argv && (q = strchr(p, '\0') - strlen(*argv)) > p &&
 +	    strcmp(*argv, q) == 0)
 +			*q = '\0';
  	(void)printf("%s\n", p);
  	exit(0);
  }
 
 
 DES
 -- 
 Dag-Erling Smorgrav - des@ofug.org
 

From: ak03@gte.com (Alexander N. Kabaev)
To: Dag-Erling Smorgrav <des@ofug.org>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/21918: Unjustified basename code removal and subsequent breakage
Date: 12 Oct 2000 10:33:41 -0400

 > Why do you use 16 lines of code to fix this when all you need is to
 > add p != q to the if test?
 
   I just restored the code you removed in revision 1.4. It is of
 course the matter of personal preference, but the old code IMO 
 is much more readable than the && chain you added in recently.
 Feel free to apply your patch if you really like it better. 
 6 out of 16 lines of my patch are the comment anyway, so it is not
 _that_ bad :)
 
 >(And you didn't spot the real bug, either - but it can still be
 > fixed by just rewording the if test)
 
 Do you mean the basename(1) and basename(3) inconsistency described
 in Bruse Evans message? Or there is something else I am missing?
 
 Should we add something like the following to make FreeBSD basename 
 consistent with historic behaviour again?
 
 /*
  * Do not call basename(3) with an empty string because
  * basename(3) is specified to bogotify
  * this case by changing "" to "." and that is
  * not consistent with what basename(1) is supposed to do
  */
 if ( argv[0][0]) == '\0') {
 	(void)printf("\n");
 	exit(0);
 }
 
 
 P.S. All English grammar errors in the above text are mine and
      mine only.
 

From: Dag-Erling Smorgrav <des@ofug.org>
To: ak03@gte.com (Alexander N. Kabaev)
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/21918: Unjustified basename code removal and subsequent breakage
Date: 12 Oct 2000 18:10:44 +0200

 ak03@gte.com (Alexander N. Kabaev) writes:
 > >(And you didn't spot the real bug, either - but it can still be
 > > fixed by just rewording the if test)
 > Do you mean the basename(1) and basename(3) inconsistency described
 > in Bruse Evans message? Or there is something else I am missing?
 
 Something else - 'basename filename.ext.ext .ext' doesn't work the way
 it should.
 
 > Should we add something like the following to make FreeBSD basename 
 > consistent with historic behaviour again?
 
 Yes.
 
 DES
 -- 
 Dag-Erling Smorgrav - des@ofug.org
 

From: "Alexander N. Kabaev" <ak03@gte.com>
To: Dag-Erling Smorgrav <des@ofug.org>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/21918: Unjustified basename code removal and subsequent 
Date: Thu, 12 Oct 2000 12:31:23 -0400 (EDT)

 > 
 > Something else - 'basename filename.ext.ext .ext' doesn't work the way
 > it should.
 > 
 Ah, I see it now. My patch fixed that bug as well despite the fact that I wasn't
 even aware it's presence.
 
 Thanks for prompt response!
 ----------------------------------
 E-Mail: Alexander N. Kabaev <ak03@gte.com>
 Date: 12-Oct-00
 Time: 12:27:41
 ----------------------------------
 
State-Changed-From-To: open->closed 
State-Changed-By: mikeh 
State-Changed-When: Fri Jun 22 22:57:41 PDT 2001 
State-Changed-Why:  
Fixed. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=21918 
>Unformatted:
 Alexander N. Kabaev
