Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 6 Feb 2002 23:06:12 +1100 (EST)
From:      "Tim J. Robbins" <tim@robbins.dropbear.id.au>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/34663: [PATCH] vintage tr(1) -c "" bug
Message-ID:  <200202061206.g16C6CB38865@descent.robbins.dropbear.id.au>

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

>Number:         34663
>Category:       bin
>Synopsis:       [PATCH] vintage tr(1) -c "" bug
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Feb 06 04:10:02 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Tim J. Robbins
>Release:        FreeBSD 4.5-STABLE i386
>Organization:
>Environment:
System: FreeBSD descent.robbins.dropbear.id.au 4.5-STABLE FreeBSD 4.5-STABLE #1: Mon Jan 28 23:17:03 EST 2002 tim@descent.robbins.dropbear.id.au:/usr/obj/usr/src/sys/DESCENT i386

>Description:

tr's -c option gives incorrect results when the first string is empty.

>How-To-Repeat:

tim@descent$ echo "test" | tr -c "" "A" | hd
00000000  ff ff ff ff ff                                    |.....|
00000005

These results are incorrect. The complement of `no characters' is
`all characters', so all characters should be turned into A's.

Using 4.3BSD tr.c ("@(#)tr.c 4.2 (Berkeley) 4/22/85"):
tim@descent$ echo "test" | /tmp/tr -c "" "A" | hd
00000000  41 41 41 41 41                                    |AAAAA|
00000005

>Fix:

ch, which is used to hold the last character from string2, needs to be
initialised outside the loop. The 0xff's in the incorrect output are
(unsigned char)-1's, left over from when ch was being used to hold the
return value of getopt().

Index: tr/tr.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/tr/tr.c,v
retrieving revision 1.9
diff -u -r1.9 tr.c
--- tr/tr.c	2001/12/11 23:36:25	1.9
+++ tr/tr.c	2002/02/06 11:57:19
@@ -213,6 +213,7 @@
 		errx(1, "empty string2");
 
 	/* If string2 runs out of characters, use the last one specified. */
+	ch = s2.lastch;
 	if (sflag)
 		while (next(&s1)) {
 			string1[s1.lastch] = ch = s2.lastch;


After applying this patch, the results match those of 4.3BSD tr:
tim@descent$ echo 'test' | tr -c "" "A" | hd
00000000  41 41 41 41 41                                    |AAAAA|
00000005
>Release-Note:
>Audit-Trail:
>Unformatted:

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?200202061206.g16C6CB38865>