Skip to content

Commit 9de4dd8

Browse files
committed
Improve error handling when reading extra fields.
Based on Issue #509.
1 parent c30a302 commit 9de4dd8

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

THANKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ David Demelier <demelier.david@gmail.com>
4646
Dean Ellis <dellis1972@googlemail.com>
4747
Declan Moran
4848
Del Merritt <del@alum.mit.edu>
49+
Deng XingJing
4950
Devin Davila <daviladsoftware@gmail.com>
5051
Dmytro Rybachenko <atmoliton@gmail.com>
5152
Dylan T. <dktapps@pmmp.io>

src/diff_output.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,25 @@ diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data, zip_u
9191
return;
9292
}
9393

94-
offset = 0;
95-
for (i = 0; i < data_length; i++) {
96-
hexdata[offset++] = (i == 0 ? '<' : ' ');
97-
98-
if (i >= MAX_BYTES) {
99-
snprintf(hexdata + offset, sizeof(hexdata) - offset, "...");
100-
break;
101-
}
102-
snprintf(hexdata + offset, sizeof(hexdata) - offset, "%02x", data[i]);
103-
offset += 2;
94+
if (data == NULL) {
95+
strcpy(hexdata, "<no data>");
10496
}
97+
else {
98+
offset = 0;
99+
for (i = 0; i < data_length; i++) {
100+
hexdata[offset++] = (i == 0 ? '<' : ' ');
101+
102+
if (i >= MAX_BYTES) {
103+
snprintf(hexdata + offset, sizeof(hexdata) - offset, "...");
104+
break;
105+
}
106+
snprintf(hexdata + offset, sizeof(hexdata) - offset, "%02x", data[i]);
107+
offset += 2;
108+
}
105109

106-
hexdata[offset++] = '>';
107-
hexdata[offset] = '\0';
110+
hexdata[offset++] = '>';
111+
hexdata[offset] = '\0';
112+
}
108113

109114
va_start(ap, fmt);
110115
vsnprintf(prefix, sizeof(prefix), fmt, ap);

src/zipcmp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,21 +672,19 @@ ef_read(zip_t *za, zip_uint64_t idx, struct entry *e) {
672672

673673
e->n_extra_fields = (zip_uint16_t)(n_local + n_central);
674674

675-
if ((e->extra_fields = (struct ef *)malloc(sizeof(e->extra_fields[0]) * e->n_extra_fields)) == NULL)
676-
return -1;
675+
if ((e->extra_fields = (struct ef *)malloc(sizeof(e->extra_fields[0]) * e->n_extra_fields)) == NULL) {
676+
fprintf(stderr, "%s: malloc failure\n", progname);
677+
exit(1);
678+
}
677679

678680
for (i = 0; i < n_local; i++) {
679681
e->extra_fields[i].name = e->name;
680682
e->extra_fields[i].data = zip_file_extra_field_get(za, idx, i, &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_LOCAL);
681-
if (e->extra_fields[i].data == NULL)
682-
return -1;
683683
e->extra_fields[i].flags = ZIP_FL_LOCAL;
684684
}
685685
for (; i < e->n_extra_fields; i++) {
686686
e->extra_fields[i].name = e->name;
687687
e->extra_fields[i].data = zip_file_extra_field_get(za, idx, (zip_uint16_t)(i - n_local), &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_CENTRAL);
688-
if (e->extra_fields[i].data == NULL)
689-
return -1;
690688
e->extra_fields[i].flags = ZIP_FL_CENTRAL;
691689
}
692690

@@ -723,6 +721,8 @@ ef_order(const void *ap, const void *bp) {
723721
return a->id - b->id;
724722
if (a->size != b->size)
725723
return a->size - b->size;
724+
if (a->data == NULL || b->data == NULL)
725+
return a->data == b->data ? 0 : (a->data == NULL ? -1 : 1);
726726
return memcmp(a->data, b->data, a->size);
727727
}
728728

0 commit comments

Comments
 (0)