Skip to content

Commit

Permalink
Fix normalize entry path (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- authored Dec 18, 2020
1 parent 91a2e2c commit 903beb7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ static char *normalize(char *name, char *const nname, size_t len) {

for (; offn < len; offn++) {
if (ISSLASH(name[offn])) {
if (ncpy > 0 && strncmp(&nname[offnn], ".", 1) &&
strncmp(&nname[offnn], "..", 2)) {
if (ncpy > 0 && strcmp(&nname[offnn], ".\0") &&
strcmp(&nname[offnn], "..\0")) {
offnn += ncpy;
nname[offnn++] = name[offn]; // append '/'
}
Expand All @@ -894,8 +894,8 @@ static char *normalize(char *name, char *const nname, size_t len) {
}

// at the end, extra check what we've already copied
if (ncpy == 0 || !strncmp(&nname[offnn], ".", 1) ||
!strncmp(&nname[offnn], "..", 2)) {
if (ncpy == 0 || !strcmp(&nname[offnn], ".\0") ||
!strcmp(&nname[offnn], "..\0")) {
nname[offnn] = 0;
}
return nname;
Expand Down
42 changes: 40 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ static void test_append(void) {
++total_entries;
assert(0 == zip_entry_close(zip));

assert(0 == zip_entry_open(zip, "dotfiles/.test"));
assert(0 == strcmp(zip_entry_name(zip), "dotfiles/.test"));
assert(0 == zip_entry_size(zip));
assert(0 == zip_entry_crc32(zip));
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
assert(strlen(TESTDATA2) == zip_entry_size(zip));
assert(CRC32DATA2 == zip_entry_crc32(zip));

assert(total_entries == zip_entry_index(zip));
++total_entries;
assert(0 == zip_entry_close(zip));

zip_close(zip);
}

Expand Down Expand Up @@ -145,6 +157,17 @@ static void test_read(void) {
free(buf);
buf = NULL;

buftmp = strlen(TESTDATA2);
buf = calloc(buftmp, sizeof(char));
assert(0 == zip_entry_open(zip, "dotfiles/.test"));

bufsize = zip_entry_noallocread(zip, (void *)buf, buftmp);
assert(buftmp == (size_t)bufsize);
assert(0 == strncmp(buf, TESTDATA2, buftmp));
assert(0 == zip_entry_close(zip));
free(buf);
buf = NULL;

zip_close(zip);
}

Expand Down Expand Up @@ -173,18 +196,27 @@ static void test_extract(void) {

struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
assert(zip != NULL);
memset((void *)&buf, 0, sizeof(struct buffer_t));

memset((void *)&buf, 0, sizeof(struct buffer_t));
assert(0 == zip_entry_open(zip, "test/test-1.txt"));
assert(0 == zip_entry_extract(zip, on_extract, &buf));

assert(buf.size == strlen(TESTDATA1));
assert(0 == strncmp(buf.data, TESTDATA1, buf.size));
assert(0 == zip_entry_close(zip));
free(buf.data);
buf.data = NULL;
buf.size = 0;

memset((void *)&buf, 0, sizeof(struct buffer_t));
assert(0 == zip_entry_open(zip, "dotfiles/.test"));
assert(0 == zip_entry_extract(zip, on_extract, &buf));
assert(buf.size == strlen(TESTDATA2));
assert(0 == strncmp(buf.data, TESTDATA2, buf.size));
assert(0 == zip_entry_close(zip));
free(buf.data);
buf.data = NULL;
buf.size = 0;

zip_close(zip);
}

Expand Down Expand Up @@ -476,9 +508,14 @@ static void test_extract_stream(void) {
assert(0 == zip_entry_write(zip, TESTDATA1, strlen(TESTDATA1)));
assert(0 == zip_entry_close(zip));

assert(0 == zip_entry_open(zip, "dotfiles/.test\0"));
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
assert(0 == zip_entry_close(zip));

zip_close(zip);

remove(RFILE);
remove("dotfiles/.test\0");

FILE *fp = NULL;
fp = fopen(ZIPNAME, "rb+");
Expand All @@ -497,6 +534,7 @@ static void test_extract_stream(void) {

fclose(fp);
remove(RFILE);
remove("dotfiles/.test\0");
remove(ZIPNAME);
#endif
}
Expand Down

0 comments on commit 903beb7

Please sign in to comment.