List Archive

Thread

Thread Index

Message

From: Hanno Böck <hanno%hboeck.de@localhost>
To: libzip-discuss%nih.at@localhost
Subject: [patch] zipcmp: fix call of qsort with null pointer
Date: Tue, 18 Oct 2016 16:47:17 +0200

Hi,

qsort() requires to be called with a valid pointer. For an empty zip
file zipcmp will try to sort an empty list with 0 elements.
undefined behavior sanitizer throws a warning about this
(-fsanitize=undefined in cflags), test with
zipcmp testempty.zip testempty.zip (from the regress dir)

error message:
zipcmp.c:228:8: runtime error: null pointer passed as argument 1, which is 
declared to never be null
/usr/include/stdlib.h:765:30: note: nonnull attribute specified here
SUMMARY: AddressSanitizer: undefined-behavior zipcmp.c:228:8 in 


Attached patch checks for this and fixes it.

-- 
Hanno Böck
https://hboeck.de/

mail/jabber: hanno%hboeck.de@localhost
GPG: FE73757FA60E4E21B937579FA5880072BBB51E42
--- a/src/zipcmp.c      2016-10-18 13:31:23.000000000 +0200
+++ b/src/zipcmp.c      2016-10-18 16:44:35.744444493 +0200
@@ -225,7 +225,8 @@
            if (list_zip(zn[i], a+i) < 0)
                exit(2);
        }
-       qsort(a[i].entry, a[i].nentry, sizeof(a[i].entry[0]), entry_cmp);
+       if (a[i].nentry > 0)
+           qsort(a[i].entry, a[i].nentry, sizeof(a[i].entry[0]), entry_cmp);
     }
 
     header_done = 0;

Attachment: pgpH2FWGaE6dZ.pgp
Description: OpenPGP digital signature

Made by MHonArc.