Skip to content

Commit 654ae4a

Browse files
committed
fix(globals): correctly fetch $_REQUEST super global
Refs: #331
1 parent eb39949 commit 654ae4a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

allowed_bindings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ bind! {
3939
_zend_new_array,
4040
_zval_struct__bindgen_ty_1,
4141
_zval_struct__bindgen_ty_2,
42+
_zend_known_string_id,
4243
// ext_php_rs_executor_globals,
4344
// ext_php_rs_php_build_id,
4445
// ext_php_rs_zend_object_alloc,
@@ -84,6 +85,7 @@ bind! {
8485
zend_execute_data,
8586
zend_function_entry,
8687
zend_hash_clean,
88+
zend_hash_find_known_hash,
8789
zend_hash_index_del,
8890
zend_hash_index_find,
8991
zend_hash_index_update,
@@ -95,6 +97,7 @@ bind! {
9597
zend_is_callable,
9698
zend_is_identical,
9799
zend_is_iterable,
100+
zend_known_strings,
98101
zend_long,
99102
zend_lookup_class_ex,
100103
zend_module_entry,

src/zend/globals.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use crate::exception::PhpResult;
1313
#[cfg(php82)]
1414
use crate::ffi::zend_atomic_bool_store;
1515
use crate::ffi::{
16-
_sapi_module_struct, _zend_executor_globals, ext_php_rs_executor_globals,
17-
ext_php_rs_file_globals, ext_php_rs_process_globals, ext_php_rs_sapi_globals,
18-
ext_php_rs_sapi_module, php_core_globals, php_file_globals, sapi_globals_struct,
19-
sapi_header_struct, sapi_headers_struct, sapi_request_info, zend_ini_entry,
20-
zend_is_auto_global, TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET,
16+
_sapi_module_struct, _zend_executor_globals, _zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST,
17+
executor_globals, ext_php_rs_executor_globals, ext_php_rs_file_globals,
18+
ext_php_rs_process_globals, ext_php_rs_sapi_globals, ext_php_rs_sapi_module, php_core_globals,
19+
php_file_globals, sapi_globals_struct, sapi_header_struct, sapi_headers_struct,
20+
sapi_request_info, zend_hash_find_known_hash, zend_ini_entry, zend_is_auto_global,
21+
zend_known_strings, TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET,
2122
TRACK_VARS_POST, TRACK_VARS_SERVER,
2223
};
2324

@@ -285,16 +286,24 @@ impl ProcessGlobals {
285286
/// Get the HTTP Request variables. Equivalent of $_REQUEST.
286287
///
287288
/// # Panics
288-
/// There is an outstanding issue with the implementation of this function.
289-
/// Untill resolved, this function will allways panic.
290-
///
291-
/// - <https://github.com/davidcole1340/ext-php-rs/issues/331>
292-
/// - <https://github.com/php/php-src/issues/16541>
293-
pub fn http_request_vars(&self) -> &ZendHashTable {
294-
todo!("$_REQUEST super global was erroneously fetched from http_globals which resulted in an out-of-bounds access. A new implementation is needed.");
295-
// self.http_globals[TRACK_VARS_REQUEST as usize]
296-
// .array()
297-
// .expect("Type is not a ZendArray")
289+
/// - If the request global is not found or fails to be populated.
290+
/// - If the request global is not a ZendArray.
291+
pub fn http_request_vars(&self) -> Option<&ZendHashTable> {
292+
let key = unsafe {
293+
*zend_known_strings.add(_zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST as usize)
294+
};
295+
296+
// `$_REQUEST` is lazy-initted, we need to call `zend_is_auto_global` to make sure it's populated.
297+
if !unsafe { zend_is_auto_global(key) } {
298+
panic!("Failed to get request global");
299+
}
300+
301+
let request = unsafe { zend_hash_find_known_hash(&executor_globals.symbol_table, key) };
302+
if request.is_null() {
303+
return None;
304+
}
305+
306+
Some(unsafe { (*request).array() }.expect("Type is not a ZendArray"))
298307
}
299308

300309
/// Get the HTTP Environment variables. Equivalent of $_ENV.

0 commit comments

Comments
 (0)