Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/uniq Fix the bug introduced in the previous commit.



details:   https://anonhg.NetBSD.org/src/rev/43b6cae3555e
branches:  trunk
changeset: 348369:43b6cae3555e
user:      abhinav <abhinav%NetBSD.org@localhost>
date:      Sun Oct 16 06:17:51 2016 +0000

description:
Fix the bug introduced in the previous commit.

The lengths of the lines being compared were not correct. Essentially, we
were comparing the length of the buffers instead of the length of the strings.

This patch also avoids calling skip() twice for the same line.

Thanks to Rin Okuyama for the patch.

diffstat:

 usr.bin/uniq/uniq.c |  28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diffs (87 lines):

diff -r cf8f63eafd7a -r 43b6cae3555e usr.bin/uniq/uniq.c
--- a/usr.bin/uniq/uniq.c       Sun Oct 16 05:32:30 2016 +0000
+++ b/usr.bin/uniq/uniq.c       Sun Oct 16 06:17:51 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uniq.c,v 1.19 2016/10/14 19:43:59 abhinav Exp $        */
+/*     $NetBSD: uniq.c,v 1.20 2016/10/16 06:17:51 abhinav Exp $        */
 
 /*
  * Copyright (c) 1989, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)uniq.c     8.3 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: uniq.c,v 1.19 2016/10/14 19:43:59 abhinav Exp $");
+__RCSID("$NetBSD: uniq.c,v 1.20 2016/10/16 06:17:51 abhinav Exp $");
 #endif /* not lint */
 
 #include <err.h>
@@ -65,12 +65,12 @@
 int
 main (int argc, char *argv[])
 {
-       const char *t1, *t2;
+       const char *prevp, *thisp;
        FILE *ifp, *ofp;
        int ch;
        char *prevline, *thisline, *p;
        size_t prevlinesize, thislinesize, psize;
-       size_t prevlinecompsize, thislinecompsize;
+       size_t prevlen, thislen;
 
        setprogname(argv[0]);
        ifp = ofp = NULL;
@@ -127,11 +127,16 @@
 
        if ((p = fgetln(ifp, &psize)) == NULL)
                return 0;
-       prevlinesize = psize;
+       prevlinesize = prevlen = psize;
        if ((prevline = malloc(prevlinesize + 1)) == NULL)
                err(1, "malloc");
        (void)memcpy(prevline, p, prevlinesize);
        prevline[prevlinesize] = '\0';
+       
+       if (numfields || numchars)
+               prevp = skip(prevline, &prevlen);
+       else
+               prevp = prevline;
 
        thislinesize = psize;
        if ((thisline = malloc(thislinesize + 1)) == NULL)
@@ -143,22 +148,19 @@
                                err(1, "realloc");
                        thislinesize = psize;
                }
+               thislen = psize;
                (void)memcpy(thisline, p, psize);
                thisline[psize] = '\0';
-               thislinecompsize = thislinesize;
-               prevlinecompsize = prevlinesize;
 
                /* If requested get the chosen fields + character offsets. */
                if (numfields || numchars) {
-                       t1 = skip(thisline, &thislinecompsize);
-                       t2 = skip(prevline, &prevlinecompsize);
+                       thisp = skip(thisline, &thislen);
                } else {
-                       t1 = thisline;
-                       t2 = prevline;
+                       thisp = thisline;
                }
 
                /* If different, print; set previous to new value. */
-               if (thislinecompsize != prevlinecompsize || strcmp(t1, t2)) {
+               if (thislen != prevlen || strcmp(thisp, prevp)) {
                        char *t;
                        size_t ts;
 
@@ -169,6 +171,8 @@
                        ts = prevlinesize;
                        prevlinesize = thislinesize;
                        thislinesize = ts;
+                       prevp = thisp;
+                       prevlen = thislen;
                        repeats = 0;
                } else
                        ++repeats;



Home | Main Index | Thread Index | Old Index