Skip to content

Commit a231da8

Browse files
authored
sapi/phpdbg: refactor phpdbg_load_module_or_extension() (#21578)
Accept a zend_string* to be able to use the zend_string_concat APIs Make the function static as it's not exported in a header and only used inside this file
1 parent bd042a5 commit a231da8

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,32 +1188,39 @@ static void add_zendext_info(zend_extension *ext) /* {{{ */ {
11881188
/* }}} */
11891189

11901190
#ifdef HAVE_LIBDL
1191-
PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, const char **name) /* {{{ */ {
1191+
static const char *phpdbg_load_module_or_extension(zend_string **path, const char **name) /* {{{ */ {
11921192
DL_HANDLE handle;
1193-
zend_string *extension_dir = zend_ini_str_literal("extension_dir");
1193+
const zend_string *extension_dir = zend_ini_str_literal("extension_dir");
11941194

11951195
if (UNEXPECTED(zend_str_has_nul_byte(extension_dir))) {
11961196
phpdbg_error("extension_dir ini setting contains a nul byte");
11971197
return NULL;
11981198
}
1199-
if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) {
1199+
if (memchr(ZSTR_VAL(*path), '/', ZSTR_LEN(*path)) != NULL || memchr(ZSTR_VAL(*path), '/', ZSTR_LEN(*path)) != NULL) {
12001200
/* path is fine */
12011201
} else if (extension_dir && ZSTR_LEN(extension_dir) > 0) {
1202-
char *libpath;
1202+
zend_string *libpath;
12031203
if (IS_SLASH(ZSTR_VAL(extension_dir)[ZSTR_LEN(extension_dir)-1])) {
1204-
spprintf(&libpath, 0, "%s%s", ZSTR_VAL(extension_dir), *path); /* SAFE */
1204+
libpath = zend_string_concat2(
1205+
ZSTR_VAL(extension_dir), ZSTR_LEN(extension_dir),
1206+
ZSTR_VAL(*path), ZSTR_LEN(*path)
1207+
);
12051208
} else {
1206-
spprintf(&libpath, 0, "%s%c%s", ZSTR_VAL(extension_dir), DEFAULT_SLASH, *path); /* SAFE */
1209+
libpath = zend_string_concat3(
1210+
ZSTR_VAL(extension_dir), ZSTR_LEN(extension_dir),
1211+
"/", 1,
1212+
ZSTR_VAL(*path), ZSTR_LEN(*path)
1213+
);
12071214
}
1208-
efree(*path);
1215+
zend_string_release_ex(*path, false);
12091216
*path = libpath;
12101217
} else {
12111218
phpdbg_error("Not a full path given or extension_dir ini setting is not set");
12121219

12131220
return NULL;
12141221
}
12151222

1216-
handle = DL_LOAD(*path);
1223+
handle = DL_LOAD(ZSTR_VAL(*path));
12171224

12181225
if (!handle) {
12191226
#ifdef PHP_WIN32
@@ -1331,7 +1338,6 @@ PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, const char *
13311338
PHPDBG_COMMAND(dl) /* {{{ */
13321339
{
13331340
const char *type, *name;
1334-
char *path;
13351341

13361342
if (!param || param->type == EMPTY_PARAM) {
13371343
phpdbg_notice("Zend extensions");
@@ -1340,23 +1346,24 @@ PHPDBG_COMMAND(dl) /* {{{ */
13401346
phpdbg_notice("Modules");
13411347
zend_hash_apply(&module_registry, (apply_func_t) add_module_info);
13421348
} else switch (param->type) {
1343-
case STR_PARAM:
1349+
case STR_PARAM: {
13441350
#ifdef HAVE_LIBDL
1345-
path = estrndup(param->str, param->len);
1351+
zend_string *path = zend_string_init(param->str, param->len, false);
13461352

13471353
phpdbg_activate_err_buf(1);
13481354
if ((type = phpdbg_load_module_or_extension(&path, &name)) == NULL) {
1349-
phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", path, PHPDBG_G(err_buf).msg);
1355+
phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", ZSTR_VAL(path), PHPDBG_G(err_buf).msg);
13501356
} else {
1351-
phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, path);
1357+
phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, ZSTR_VAL(path));
13521358
}
13531359
phpdbg_activate_err_buf(0);
13541360
phpdbg_free_err_buf();
1355-
efree(path);
1361+
zend_string_release_ex(path, false);
13561362
#else
13571363
phpdbg_error("Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str);
13581364
#endif
13591365
break;
1366+
}
13601367

13611368
phpdbg_default_switch_case();
13621369
}

0 commit comments

Comments
 (0)