Commit a6e0d8e3595 for php.net
commit a6e0d8e359531e6daf8bdfa5f6f381a598fbe142
Author: Niels Dossche <7771979+ndossche@users.noreply.github.com>
Date: Fri Jan 9 22:03:07 2026 +0100
Fix GH-20882: phar buildFromIterator breaks with missing base directory
Broke in f57526a07a because of changing a char*+size_t pair to
zend_string* (which can't handle NULL pointers in its macros).
Closes GH-20888.
diff --git a/NEWS b/NEWS
index a8cb10ca8f3..d54ff4f6039 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ PHP NEWS
. Fixed bug GH-20833 (mb_str_pad() divide by zero if padding string is
invalid in the encoding). (ndossche)
+- Phar:
+ . Fixed bug GH-20882 (buildFromIterator breaks with missing base directory).
+ (ndossche)
+
- Readline:
. Fixed bug GH-18139 (Memory leak when overriding some settings
via readline_info()). (ndossche)
diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c
index 5ad31f7c295..18db3190bb0 100644
--- a/ext/phar/phar_object.c
+++ b/ext/phar/phar_object.c
@@ -1397,12 +1397,12 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
zval *value;
bool close_fp = 1;
struct _phar_t *p_obj = (struct _phar_t*) puser;
- size_t str_key_len, base_len = ZSTR_LEN(p_obj->base);
+ size_t str_key_len, base_len = p_obj->base ? ZSTR_LEN(p_obj->base) : 0;
phar_entry_data *data;
php_stream *fp;
size_t fname_len;
size_t contents_len;
- char *fname, *error = NULL, *base = ZSTR_VAL(p_obj->base), *save = NULL, *temp = NULL;
+ char *fname, *error = NULL, *base = p_obj->base ? ZSTR_VAL(p_obj->base) : NULL, *save = NULL, *temp = NULL;
zend_string *opened;
char *str_key;
zend_class_entry *ce = p_obj->c;
diff --git a/ext/phar/tests/gh20882.phpt b/ext/phar/tests/gh20882.phpt
new file mode 100644
index 00000000000..8928178a22a
--- /dev/null
+++ b/ext/phar/tests/gh20882.phpt
@@ -0,0 +1,22 @@
+--TEST--
+GH-20882 (phar buildFromIterator breaks with missing base directory)
+--EXTENSIONS--
+phar
+--INI--
+phar.readonly=0
+--FILE--
+<?php
+$phar = new \Phar(__DIR__ . "/test.phar");
+try {
+ $phar->buildFromIterator(
+ new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator(__DIR__.'/test79082', FilesystemIterator::SKIP_DOTS)
+ ),
+ null
+ );
+} catch (BadMethodCallException $e) {
+ echo $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+Iterator RecursiveIteratorIterator returns an SplFileInfo object, so base directory must be specified