Avoid malloc(0).

--HG--
branch : HEAD
diff --git a/lib/zip_close.c b/lib/zip_close.c
index f4ae2a4..d86a9b2 100644
--- a/lib/zip_close.c
+++ b/lib/zip_close.c
@@ -225,11 +225,15 @@
 
 	if (za->entry[i].ch_extra_len != -1) {
 	    free(de.extrafield);
-	    if ((de.extrafield=malloc(za->entry[i].ch_extra_len)) == NULL) {
-		error = 1;
-		break;
-	    }
-	    memcpy(de.extrafield, za->entry[i].ch_extra, za->entry[i].ch_extra_len);
+	    if (za->entry[i].ch_extra_len > 0) {
+	        if ((de.extrafield=malloc(za->entry[i].ch_extra_len)) == NULL) {
+		    error = 1;
+		    break;
+	        }
+	        memcpy(de.extrafield, za->entry[i].ch_extra, za->entry[i].ch_extra_len);
+            }
+	    else
+		de.extrafield = NULL;
 	    de.extrafield_len = za->entry[i].ch_extra_len;
 	    /* as the rest of cd entries, its malloc/free is done by za */
 	    /* TODO unsure if this should also be set in the CD --
@@ -548,13 +552,18 @@
 _zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
 {
     if (src->ch_comment_len != -1) {
-	dest->comment = _zip_memdup(src->ch_comment,
-				    src->ch_comment_len, &src->error);
-	if (dest->comment == NULL)
-	    return -1;
 	dest->comment_len = src->ch_comment_len;
-    } else {
-	if (src->cdir && src->cdir->comment) {
+	if (src->ch_comment_len == 0)
+	    dest->comment = NULL;
+	else {
+	    dest->comment = _zip_memdup(src->ch_comment,
+				        src->ch_comment_len, &src->error);
+	    if (dest->comment == NULL)
+	        return -1;
+	}
+    }
+    else {
+	if (src->cdir && src->cdir->comment_len > 0) {
 	    dest->comment = _zip_memdup(src->cdir->comment,
 					src->cdir->comment_len, &src->error);
 	    if (dest->comment == NULL)
diff --git a/lib/zip_dirent.c b/lib/zip_dirent.c
index b5b9d27..14ed2b6 100644
--- a/lib/zip_dirent.c
+++ b/lib/zip_dirent.c
@@ -77,6 +77,9 @@
 	return -1;
     }
 
+    if (nentry == cd->nentry)
+	return 0;
+
     if ((entry=((struct zip_dirent *)
 		realloc(cd->entry, sizeof(*(cd->entry))*nentry))) == NULL) {
 	_zip_error_set(error, ZIP_ER_MEMORY, 0);
@@ -101,7 +104,9 @@
 	return NULL;
     }
 
-    if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry))
+    if (nentry == 0)
+	cd->entry = NULL;
+    else if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry))
 	== NULL) {
 	_zip_error_set(error, ZIP_ER_MEMORY, 0);
 	free(cd);
@@ -523,6 +528,9 @@
 {
     char *r, *o;
 
+    if (len == 0 && nulp == 0)
+	return NULL;
+
     r = (char *)malloc(nulp ? len+1 : len);
     if (!r) {
 	_zip_error_set(error, ZIP_ER_MEMORY, 0);
@@ -553,6 +561,9 @@
 {
     char *r, *o;
 
+    if (len == 0 && nulp == 0)
+	return NULL;
+
     r = (char *)malloc(nulp ? len+1 : len);
     if (!r) {
 	_zip_error_set(error, ZIP_ER_MEMORY, 0);
diff --git a/lib/zip_entry_new.c b/lib/zip_entry_new.c
index ad5d599..5b7d4ae 100644
--- a/lib/zip_entry_new.c
+++ b/lib/zip_entry_new.c
@@ -45,9 +45,8 @@
     struct zip_entry *ze;
     if (!za) {
 	ze = (struct zip_entry *)malloc(sizeof(struct zip_entry));
-	if (!ze) {
+	if (!ze)
 	    return NULL;
-	}
     }
     else {
 	if (za->nentry+1 >= za->nentry_alloc) {
diff --git a/lib/zip_memdup.c b/lib/zip_memdup.c
index 641125e..69a8da0 100644
--- a/lib/zip_memdup.c
+++ b/lib/zip_memdup.c
@@ -43,6 +43,9 @@
 {
     void *ret;
 
+    if (len == 0)
+	return NULL;
+
     ret = malloc(len);
     if (!ret) {
 	_zip_error_set(error, ZIP_ER_MEMORY, 0);
diff --git a/lib/zip_open.c b/lib/zip_open.c
index 5fcf8e7..c13157a 100644
--- a/lib/zip_open.c
+++ b/lib/zip_open.c
@@ -119,8 +119,10 @@
     za->cdir = cdir;
     za->zp = fp;
 
-    if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
-					      * cdir->nentry)) == NULL) {
+    if (cdir->nentry == 0)
+	za->entry = NULL;
+    else if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
+						   * cdir->nentry)) == NULL) {
 	set_error(zep, NULL, ZIP_ER_MEMORY);
 	_zip_free(za);
 	return NULL;
diff --git a/lib/zip_utf-8.c b/lib/zip_utf-8.c
index 5924e3e..f9a771a 100644
--- a/lib/zip_utf-8.c
+++ b/lib/zip_utf-8.c
@@ -202,6 +202,9 @@
     zip_uint8_t *utf8buf;
     zip_uint32_t buflen, i, offset;
 
+    if (len == 0)
+	return NULL;
+
     buflen = 0;
     for (i=0; i<len; i++)
 	buflen += _zip_unicode_to_utf8_len(_cp437_to_unicode[cp437buf[i]]);
diff --git a/src/zipcmp.c b/src/zipcmp.c
index 75a3e4e..d017841 100644
--- a/src/zipcmp.c
+++ b/src/zipcmp.c
@@ -171,23 +171,27 @@
 
 	n[i] = zip_get_num_files(za);
 
-	if ((e[i]=malloc(sizeof(*e[i]) * n[i])) == NULL) {
-	    fprintf(stderr, "%s: malloc failure\n", prg);
-	    exit(1);
-	}
+	if (n[i] == 0)
+	    e[i] = NULL;
+        else {
+	    if ((e[i]=malloc(sizeof(*e[i]) * n[i])) == NULL) {
+	        fprintf(stderr, "%s: malloc failure\n", prg);
+	        exit(1);
+	    }
 
-	for (j=0; j<n[i]; j++) {
-	    zip_stat_index(za, j, 0, &st);
-	    e[i][j].name = strdup(st.name);
-	    e[i][j].size = st.size;
-	    e[i][j].crc = st.crc;
-	    if (test_files)
-		test_file(za, j, st.size, st.crc);
-	}
+	    for (j=0; j<n[i]; j++) {
+	        zip_stat_index(za, j, 0, &st);
+	        e[i][j].name = strdup(st.name);
+	        e[i][j].size = st.size;
+	        e[i][j].crc = st.crc;
+	        if (test_files)
+		    test_file(za, j, st.size, st.crc);
+	    }
+	    qsort(e[i], n[i], sizeof(e[i][0]), entry_cmp);
+        }
 
 	zip_close(za);
 
-	qsort(e[i], n[i], sizeof(e[i][0]), entry_cmp);
     }
 
     switch (compare_list(zn, verbose,