@@ -1016,6 +1016,20 @@ ZEND_TSRMLS_CACHE_UPDATE();
1016
1016
mbstring_globals -> internal_encoding_set = 0 ;
1017
1017
mbstring_globals -> http_output_set = 0 ;
1018
1018
mbstring_globals -> http_input_set = 0 ;
1019
+
1020
+ /* Initialize immutable array of supported encoding names
1021
+ * This is done so that we can check if `mb_list_encodings()` is being
1022
+ * passed to other mbstring functions using a cheap pointer equality check */
1023
+ HashTable * array = malloc (sizeof (HashTable ));
1024
+ zend_hash_init (array , 80 , NULL , NULL , true);
1025
+ GC_ADD_FLAGS (array , IS_ARRAY_IMMUTABLE );
1026
+ for (const mbfl_encoding * * encodings = mbfl_get_supported_encodings (); * encodings ; encodings ++ ) {
1027
+ zval tmp ;
1028
+ ZVAL_PSTRING (& tmp , (* encodings )-> name );
1029
+ zend_hash_next_index_insert (array , & tmp );
1030
+ }
1031
+ GC_SET_REFCOUNT (array , 2 );
1032
+ mbstring_globals -> all_encodings_list = array ;
1019
1033
}
1020
1034
/* }}} */
1021
1035
@@ -1031,6 +1045,8 @@ static PHP_GSHUTDOWN_FUNCTION(mbstring)
1031
1045
if (mbstring_globals -> http_output_conv_mimetypes ) {
1032
1046
_php_mb_free_regex (mbstring_globals -> http_output_conv_mimetypes );
1033
1047
}
1048
+ GC_DELREF (mbstring_globals -> all_encodings_list );
1049
+ zend_hash_destroy (mbstring_globals -> all_encodings_list );
1034
1050
#ifdef HAVE_MBREGEX
1035
1051
php_mb_regex_globals_free (mbstring_globals -> mb_regex_globals );
1036
1052
#endif
@@ -3205,10 +3221,7 @@ PHP_FUNCTION(mb_list_encodings)
3205
3221
{
3206
3222
ZEND_PARSE_PARAMETERS_NONE ();
3207
3223
3208
- array_init (return_value );
3209
- for (const mbfl_encoding * * encodings = mbfl_get_supported_encodings (); * encodings ; encodings ++ ) {
3210
- add_next_index_string (return_value , (* encodings )-> name );
3211
- }
3224
+ RETURN_ARR (MBSTRG (all_encodings_list ));
3212
3225
}
3213
3226
/* }}} */
3214
3227
0 commit comments