[hackers] [sbase] linecmp: Handle NUL bytes properly || Michael Forney

From: <git_AT_suckless.org>
Date: Sat, 9 Jul 2016 11:09:58 +0200 (CEST)

commit 8ca79a2993e572beb7be56be1b0b95482c3a2431
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Sat May 14 18:56:55 2016 -0700
Commit: sin <sin_AT_2f30.org>
CommitDate: Sat Jul 9 10:09:50 2016 +0100

    linecmp: Handle NUL bytes properly
    
    Test case:
    
    if [ "$(printf 'a\na\0b' | ./sort -u)" = "$(printf 'a\na\0b')" ] ; then
            echo pass
    else
            echo fail
    fi

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;
 }
Received on Sat Jul 09 2016 - 11:09:58 CEST

This archive was generated by hypermail 2.3.0 : Sat Jul 09 2016 - 11:13:43 CEST