[hackers] [sbase] [PATCH 6/6] linecmp: Handle NUL bytes properly

From: Michael Forney <mforney_AT_mforney.org>
Date: Sat, 14 May 2016 18:56:55 -0700

Test case:

if [ "$(printf 'a\na\0b' | ./sort -u)" = "$(printf 'a\na\0b')" ] ; then
        echo pass
else
        echo fail
fi
---
 libutil/linecmp.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
diff --git a/libutil/linecmp.c b/libutil/linecmp.c
index f5a6cf9..08fc0e3 100644
--- a/libutil/linecmp.c
+++ b/libutil/linecmp.c
_AT_@ -10,15 +10,8 @@ linecmp(struct line *a, struct line *b)
 {
 	int res = 0;
 
-	if (!(res = memcmp(a->data, b->data, MIN(a->len, b->len)))) {
-		if (a->len > b->len) {
-			res = a->data[b->len];
-		} else if (b->len > a->len) {
-			res = -b->data[a->len];
-		} else {
-			res = 0;
-		}
-	}
+	if (!(res = memcmp(a->data, b->data, MIN(a->len, b->len))))
+		res = a->len - b->len;
 
 	return res;
 }
-- 
2.6.2
Received on Sun May 15 2016 - 03:56:55 CEST

This archive was generated by hypermail 2.3.0 : Sun May 15 2016 - 04:03:15 CEST