Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Oct 2000 15:17:24 -0400 (EDT)
From:      ak03@gte.com
To:        FreeBSD-gnats-submit@freebsd.org
Cc:        des@freebsd.org
Subject:   bin/21918: Unjustified basename code removal and subsequent breakage
Message-ID:  <200010111917.e9BJHOb80947@h132-197-97-45.gte.com>

next in thread | raw e-mail | index | archive | help

>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:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 11 12:50:01 PDT 2000
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:
 Alexander N. Kabaev


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200010111917.e9BJHOb80947>