Skip to content

Commit

Permalink
Make sure core module has number 0 (php#12272)
Browse files Browse the repository at this point in the history
* Make sure core module has number 0

Some places, possibly also outside PHP, assume the core extension has
module number 0. After 8a812c3 this wasn't the case anymore as
reported in [1]. Fix it by changing how the next module ID is computed.

[1] php#12246 (comment)

* Add assertion

* Add test
  • Loading branch information
nielsdos authored Sep 25, 2023
1 parent f1f04cf commit 9b6afd8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3271,7 +3271,7 @@ ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
/* return the next free module number */
ZEND_API int zend_next_free_module(void) /* {{{ */
{
return zend_hash_num_elements(&module_registry) + 1;
return zend_hash_num_elements(&module_registry);
}
/* }}} */

Expand Down
8 changes: 7 additions & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ zend_module_entry zend_builtin_module = { /* {{{ */

zend_result zend_startup_builtin_functions(void) /* {{{ */
{
return (EG(current_module) = zend_register_module_ex(&zend_builtin_module, MODULE_PERSISTENT)) == NULL ? FAILURE : SUCCESS;
zend_module_entry *module;
EG(current_module) = module = zend_register_module_ex(&zend_builtin_module, MODULE_PERSISTENT);
if (UNEXPECTED(module == NULL)) {
return FAILURE;
}
ZEND_ASSERT(module->module_number == 0);
return SUCCESS;
}
/* }}} */

Expand Down
19 changes: 19 additions & 0 deletions sapi/cli/tests/025.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
CLI php -i extension_dir
--SKIPIF--
<?php
include "skipif.inc";
if (substr(PHP_OS, 0, 3) == 'WIN') {
die ("skip not for Windows");
}
?>
--FILE--
<?php

$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$output = `$php -n -i`;
var_dump(str_contains($output, "extension_dir => "));

?>
--EXPECT--
bool(true)

0 comments on commit 9b6afd8

Please sign in to comment.