Skip to content

Commit

Permalink
Minor refactor of load extension by name impl
Browse files Browse the repository at this point in the history
Minimize the #ifdef surface area
Localize orig_libpath to retry scope
Send errors to php_error() rathern than stderr
  • Loading branch information
sgolemon committed Jun 22, 2017
1 parent fe5c8f2 commit d09edf7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
22 changes: 10 additions & 12 deletions ext/standard/dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ PHPAPI PHP_FUNCTION(dl)
PHPAPI int php_load_extension(char *filename, int type, int start_now)
{
void *handle;
char *libpath, *orig_libpath;
char *libpath;
zend_module_entry *module_entry;
zend_module_entry *(*get_module)(void);
int error_type, slash_suffix;
Expand Down Expand Up @@ -118,22 +118,20 @@ PHPAPI int php_load_extension(char *filename, int type, int start_now)
}
if (VCWD_ACCESS(libpath, F_OK)) {
/* If file does not exist, consider as extension name and build file name */
orig_libpath = libpath;
const char *libpath_prefix = "";
char *orig_libpath = libpath;
#if PHP_WIN32
libpath_prefix = "php_";
#endif
if (slash_suffix) {
spprintf(&libpath, 0, "%sphp_%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
} else {
spprintf(&libpath, 0, "%s%cphp_%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
}
#else
if (slash_suffix) {
spprintf(&libpath, 0, "%s%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
spprintf(&libpath, 0, "%s%s%s." PHP_SHLIB_SUFFIX, extension_dir, libpath_prefix, filename); /* SAFE */
} else {
spprintf(&libpath, 0, "%s%c%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
spprintf(&libpath, 0, "%s%c%s%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, libpath_prefix, filename); /* SAFE */
}
#endif

if (VCWD_ACCESS(libpath, F_OK)) {
php_error_docref(NULL TSRMLS_CC, error_type, "Cannot access dynamic library '%s' (tried : %s, %s)", filename, orig_libpath, libpath);
php_error(error_type, "Cannot access dynamic library '%s' (tried : %s, %s)",
filename, orig_libpath, libpath);
efree(orig_libpath);
efree(libpath);
return FAILURE;
Expand Down
27 changes: 13 additions & 14 deletions main/php_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ static void php_load_zend_extension_cb(void *arg)
if (IS_ABSOLUTE_PATH(filename, length)) {
zend_load_extension(filename);
} else {
char *libpath, *orig_libpath;
char *libpath;
char *extension_dir = INI_STR("extension_dir");
int extension_dir_len = (int)strlen(extension_dir);
int slash_suffix = IS_SLASH(extension_dir[extension_dir_len-1]);
Expand All @@ -372,30 +372,29 @@ static void php_load_zend_extension_cb(void *arg)
} else {
spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
}

if (VCWD_ACCESS(libpath, F_OK)) {
/* If file does not exist, consider as extension name and build file name */
orig_libpath = libpath;
const char *libpath_prefix = "";
char *orig_libpath = libpath;
#if PHP_WIN32
libpath_prefix = "php_";
#endif

if (slash_suffix) {
spprintf(&libpath, 0, "%sphp_%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
} else {
spprintf(&libpath, 0, "%s%cphp_%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
}
#else
if (slash_suffix) {
spprintf(&libpath, 0, "%s%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
spprintf(&libpath, 0, "%s%s%s." PHP_SHLIB_SUFFIX, extension_dir, libpath_prefix, filename); /* SAFE */
} else {
spprintf(&libpath, 0, "%s%c%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
spprintf(&libpath, 0, "%s%c%s%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, libpath_prefix, filename); /* SAFE */
}
#endif

if (VCWD_ACCESS(libpath, F_OK)) {
fprintf(stderr, "Cannot access Zend extension %s (Tried: %s, %s)\n", filename, orig_libpath, libpath);
/* See http://support.microsoft.com/kb/190351 */
fflush(stderr);
php_error(E_CORE_WARNING, "Cannot access Zend extension %s (Tried: %s, %s)\n",
filename, orig_libpath, libpath);
efree(orig_libpath);
efree(libpath);
return;
}

efree(orig_libpath);
}

Expand Down

0 comments on commit d09edf7

Please sign in to comment.