Skip to content

Commit fd3118f

Browse files
committed
Fix #78641: addGlob can modify given remove_path value
`remove_path` points to the given string, so we must not modify it. Instead we use a duplicate, if we need the modification. We may want to switch to `zend_string`s in master.
1 parent 19e6abe commit fd3118f

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ PHP NEWS
3737
. Fixed bug #76859 (stream_get_line skips data if used with data-generating
3838
filter). (kkopachev)
3939

40+
- Zip:
41+
. Fixed bug #78641 (addGlob can modify given remove_path value). (cmb)
42+
4043
26 Sep 2019, PHP 7.2.23
4144

4245
- Core:

ext/zip/php_zip.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16671667
struct zip *intern;
16681668
zval *self = getThis();
16691669
char *path = ".";
1670-
char *remove_path = NULL;
1670+
char *remove_path = NULL, *save_remove_path;
16711671
char *add_path = NULL;
16721672
size_t add_path_len, remove_path_len = 0, path_len = 1;
16731673
zend_long remove_all_path = 0;
@@ -1703,10 +1703,11 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17031703
RETURN_FALSE;
17041704
}
17051705

1706+
save_remove_path = remove_path;
17061707
if (remove_path && remove_path_len > 1) {
17071708
size_t real_len = strlen(remove_path);
17081709
if ((real_len > 1) && ((remove_path[real_len - 1] == '/') || (remove_path[real_len - 1] == '\\'))) {
1709-
remove_path[real_len - 1] = '\0';
1710+
remove_path = estrndup(remove_path, real_len - 1);
17101711
}
17111712
}
17121713

@@ -1766,6 +1767,9 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17661767
}
17671768
}
17681769
}
1770+
if (remove_path != save_remove_path) {
1771+
efree(remove_path);
1772+
}
17691773
}
17701774
/* }}} */
17711775

ext/zip/tests/bug78641.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug #78641 (addGlob can modify given remove_path value)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zip')) die('skip zip extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
define("TMPDIR", __DIR__ . "/");
10+
11+
$file = TMPDIR . 'bug78641';
12+
touch($file);
13+
14+
$zip = new ZipArchive();
15+
$zip->open(TMPDIR . "bug78641.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
16+
var_dump(basename(TMPDIR));
17+
$zip->addGlob($file, 0, ["remove_path" => TMPDIR]);
18+
var_dump(basename(TMPDIR));
19+
$zip->close();
20+
?>
21+
--EXPECT--
22+
string(5) "tests"
23+
string(5) "tests"
24+
--CLEAN--
25+
<?php
26+
unlink(__DIR__ . '/bug78641');
27+
unlink(__DIR__ . '/bug78641.zip');
28+
?>

0 commit comments

Comments
 (0)