From 4bfa1b64b0f328cfc93d636b09725a0c9bb84572 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Wed, 25 Mar 2026 13:30:34 +0000 Subject: [PATCH 1/2] ext/spl: add const qualifiers when dealing with CEs (#21512) --- ext/spl/php_spl.c | 8 ++++---- ext/spl/spl_array.c | 2 +- ext/spl/spl_dllist.c | 4 ++-- ext/spl/spl_functions.c | 16 +++++++--------- ext/spl/spl_functions.h | 8 ++++---- ext/spl/spl_heap.c | 2 +- ext/spl/spl_observer.c | 2 +- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index e705a1e303a25..0c64d815dc812 100644 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -64,7 +64,7 @@ static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload) PHP_FUNCTION(class_parents) { zval *obj; - zend_class_entry *parent_class, *ce; + zend_class_entry *ce; bool autoload = true; /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ @@ -86,7 +86,7 @@ PHP_FUNCTION(class_parents) } array_init(return_value); - parent_class = ce->parent; + const zend_class_entry *parent_class = ce->parent; while (parent_class) { spl_add_class_name(return_value, parent_class, 0, 0); parent_class = parent_class->parent; @@ -99,7 +99,7 @@ PHP_FUNCTION(class_implements) { zval *obj; bool autoload = true; - zend_class_entry *ce; + const zend_class_entry *ce; /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { @@ -128,7 +128,7 @@ PHP_FUNCTION(class_uses) { zval *obj; bool autoload = true; - zend_class_entry *ce; + const zend_class_entry *ce; /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 3d6870a7ee953..efbe331104ab6 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -157,7 +157,7 @@ static void spl_array_object_free_storage(zend_object *object) static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) { spl_array_object *intern; - zend_class_entry *parent = class_type; + const zend_class_entry *parent = class_type; int inherited = 0; intern = zend_object_alloc(sizeof(spl_array_object), parent); diff --git a/ext/spl/spl_dllist.c b/ext/spl/spl_dllist.c index 5eceff88dfab6..c72cae38a9ceb 100644 --- a/ext/spl/spl_dllist.c +++ b/ext/spl/spl_dllist.c @@ -304,7 +304,7 @@ static void spl_dllist_object_free_storage(zend_object *object) /* {{{ */ static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */ { spl_dllist_object *intern; - zend_class_entry *parent = class_type; + const zend_class_entry *parent = class_type; int inherited = 0; intern = zend_object_alloc(sizeof(spl_dllist_object), parent); @@ -316,7 +316,7 @@ static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_ intern->traverse_position = 0; if (orig) { - spl_dllist_object *other = spl_dllist_from_obj(orig); + const spl_dllist_object *other = spl_dllist_from_obj(orig); if (clone_orig) { intern->llist = spl_ptr_llist_init(); diff --git a/ext/spl/spl_functions.c b/ext/spl/spl_functions.c index e7ecbc33f92ef..7b7991a121708 100644 --- a/ext/spl/spl_functions.c +++ b/ext/spl/spl_functions.c @@ -21,12 +21,12 @@ #include "php.h" /* {{{ spl_add_class_name */ -void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_flags) +void spl_add_class_name(zval *list, const zend_class_entry *pce, int allow, int ce_flags) { if (!allow || (allow > 0 && (pce->ce_flags & ce_flags)) || (allow < 0 && !(pce->ce_flags & ce_flags))) { - zval *tmp; + const zval *tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name); - if ((tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name)) == NULL) { + if (tmp == NULL) { zval t; ZVAL_STR_COPY(&t, pce->name); zend_hash_add(Z_ARRVAL_P(list), pce->name, &t); @@ -36,7 +36,7 @@ void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_fla /* }}} */ /* {{{ spl_add_interfaces */ -void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_flags) +void spl_add_interfaces(zval *list, const zend_class_entry *pce, int allow, int ce_flags) { if (pce->num_interfaces) { ZEND_ASSERT(pce->ce_flags & ZEND_ACC_LINKED); @@ -48,12 +48,10 @@ void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_fl /* }}} */ /* {{{ spl_add_traits */ -void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags) +void spl_add_traits(zval *list, const zend_class_entry *pce, int allow, int ce_flags) { - zend_class_entry *trait; - for (uint32_t num_traits = 0; num_traits < pce->num_traits; num_traits++) { - trait = zend_fetch_class_by_name(pce->trait_names[num_traits].name, + const zend_class_entry *trait = zend_fetch_class_by_name(pce->trait_names[num_traits].name, pce->trait_names[num_traits].lc_name, ZEND_FETCH_CLASS_TRAIT); ZEND_ASSERT(trait); spl_add_class_name(list, trait, allow, ce_flags); @@ -63,7 +61,7 @@ void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags) /* {{{ spl_add_classes */ -void spl_add_classes(zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags) +void spl_add_classes(const zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags) { ZEND_ASSERT(pce); spl_add_class_name(list, pce, allow, ce_flags); diff --git a/ext/spl/spl_functions.h b/ext/spl/spl_functions.h index eb81a8b8f818c..ca92a237169ee 100644 --- a/ext/spl/spl_functions.h +++ b/ext/spl/spl_functions.h @@ -24,10 +24,10 @@ allow > 0: allow all that match and mask ce_flags allow < 0: disallow all that match and mask ce_flags */ -void spl_add_class_name(zval * list, zend_class_entry * pce, int allow, int ce_flags); -void spl_add_interfaces(zval * list, zend_class_entry * pce, int allow, int ce_flags); -void spl_add_traits(zval * list, zend_class_entry * pce, int allow, int ce_flags); -void spl_add_classes(zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags); +void spl_add_class_name(zval * list, const zend_class_entry *pce, int allow, int ce_flags); +void spl_add_interfaces(zval * list, const zend_class_entry *pce, int allow, int ce_flags); +void spl_add_traits(zval * list, const zend_class_entry *pce, int allow, int ce_flags); +void spl_add_classes(const zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags); void spl_set_private_debug_info_property(const zend_class_entry *ce, const char *property, size_t property_len, HashTable *debug_info, zval *value); diff --git a/ext/spl/spl_heap.c b/ext/spl/spl_heap.c index afe3489e89843..eef25b9e35933 100644 --- a/ext/spl/spl_heap.c +++ b/ext/spl/spl_heap.c @@ -412,7 +412,7 @@ static void spl_heap_object_free_storage(zend_object *object) /* {{{ */ static zend_object *spl_heap_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */ { spl_heap_object *intern; - zend_class_entry *parent = class_type; + const zend_class_entry *parent = class_type; int inherited = 0; intern = zend_object_alloc(sizeof(spl_heap_object), parent); diff --git a/ext/spl/spl_observer.c b/ext/spl/spl_observer.c index 801c091fbb42d..68f132069a7c5 100644 --- a/ext/spl/spl_observer.c +++ b/ext/spl/spl_observer.c @@ -257,7 +257,7 @@ static void spl_object_storage_addall(spl_SplObjectStorage *intern, spl_SplObjec static zend_object *spl_object_storage_new_ex(zend_class_entry *class_type, zend_object *orig) /* {{{ */ { spl_SplObjectStorage *intern; - zend_class_entry *parent = class_type; + const zend_class_entry *parent = class_type; intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent); intern->pos = 0; From 41394223fef6dd027127cf72f5622d23ee94002d Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 25 Mar 2026 09:48:15 -0400 Subject: [PATCH 2/2] fileinfo: Don't compile libmagic/strcasestr.c (GH-21522) * ext/fileinfo/libmagic/file.h: delete HAVE_STRCASESTR ifdef PHP's copy of libmagic doesn't use the strcasestr() function, so there is no reason to prototype it, regardless of whether or not the OS (libc) provides it. * ext/fileinfo/libmagic.patch: update Run ext/fileinfo/generate_patch.sh to re-roll this patch, a recent commit removes HAVE_STRCASESTR from PHP's copy of libmagic. * ext/fileinfo/config.{m4,w32}: don't compile strcasestr.c Unlike the upstream library, PHP's copy of libmagic doesn't use the strcasestr() function, so there is no reason to include strcasestr.c in the list of source files. --- ext/fileinfo/config.m4 | 5 ----- ext/fileinfo/config.w32 | 2 +- ext/fileinfo/libmagic.patch | 17 +++++++++++++---- ext/fileinfo/libmagic/file.h | 3 --- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ext/fileinfo/config.m4 b/ext/fileinfo/config.m4 index 346061411e086..2d6139f06dff1 100644 --- a/ext/fileinfo/config.m4 +++ b/ext/fileinfo/config.m4 @@ -29,11 +29,6 @@ if test "$PHP_FILEINFO" != "no"; then AC_CHECK_HEADERS([sys/sysmacros.h]) - AC_CHECK_FUNCS([strcasestr],,[ - AC_MSG_NOTICE([using libmagic strcasestr implementation]) - libmagic_sources="$libmagic_sources libmagic/strcasestr.c" - ]) - AX_GCC_FUNC_ATTRIBUTE([visibility]) PHP_NEW_EXTENSION([fileinfo], diff --git a/ext/fileinfo/config.w32 b/ext/fileinfo/config.w32 index 2a42dc45a1d12..34670e7451e48 100644 --- a/ext/fileinfo/config.w32 +++ b/ext/fileinfo/config.w32 @@ -8,7 +8,7 @@ if (PHP_FILEINFO != 'no') { encoding.c fsmagic.c funcs.c \ is_json.c is_tar.c is_simh.c magic.c print.c \ readcdf.c softmagic.c der.c \ - strcasestr.c buffer.c is_csv.c"; + buffer.c is_csv.c"; EXTENSION('fileinfo', 'fileinfo.c php_libmagic.c', true, "/I" + configure_module_dirname + "/libmagic /I" + configure_module_dirname); ADD_EXTENSION_DEP('fileinfo', 'pcre'); diff --git a/ext/fileinfo/libmagic.patch b/ext/fileinfo/libmagic.patch index d2db3c044d3f6..6e3751fa943f8 100644 --- a/ext/fileinfo/libmagic.patch +++ b/ext/fileinfo/libmagic.patch @@ -1727,7 +1727,7 @@ diff -u libmagic.orig/encoding.c libmagic/encoding.c } diff -u libmagic.orig/file.h libmagic/file.h --- libmagic.orig/file.h 2024-11-27 10:37:00.000000000 -0500 -+++ libmagic/file.h 2026-03-24 10:45:21.427536159 -0400 ++++ libmagic/file.h 2026-03-25 08:13:23.336328498 -0400 @@ -27,15 +27,13 @@ */ /* @@ -1883,7 +1883,7 @@ diff -u libmagic.orig/file.h libmagic/file.h typedef struct { char *buf; size_t blen; -@@ -649,19 +629,6 @@ +@@ -649,28 +629,12 @@ extern file_protected const size_t file_nnames; #endif @@ -1903,7 +1903,16 @@ diff -u libmagic.orig/file.h libmagic/file.h #ifndef HAVE_STRLCPY size_t strlcpy(char *, const char *, size_t); #endif -@@ -681,31 +648,6 @@ + #ifndef HAVE_STRLCAT + size_t strlcat(char *, const char *, size_t); + #endif +-#ifndef HAVE_STRCASESTR +-char *strcasestr(const char *, const char *); +-#endif + #ifndef HAVE_GETLINE + ssize_t getline(char **, size_t *, FILE *); + ssize_t getdelim(char **, size_t *, int, FILE *); +@@ -681,31 +645,6 @@ #ifndef HAVE_ASCTIME_R char *asctime_r(const struct tm *, char *); #endif @@ -3066,7 +3075,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c } return file_getbuffer(ms); diff -u libmagic.orig/magic.h libmagic/magic.h ---- libmagic.orig/magic.h 2026-03-24 10:45:56.975553410 -0400 +--- libmagic.orig/magic.h 2026-03-25 08:16:04.280413419 -0400 +++ libmagic/magic.h 2026-03-20 12:10:19.777614667 -0400 @@ -47,8 +47,6 @@ * extensions */ diff --git a/ext/fileinfo/libmagic/file.h b/ext/fileinfo/libmagic/file.h index f8fe588950021..718a60b2e1b00 100644 --- a/ext/fileinfo/libmagic/file.h +++ b/ext/fileinfo/libmagic/file.h @@ -635,9 +635,6 @@ size_t strlcpy(char *, const char *, size_t); #ifndef HAVE_STRLCAT size_t strlcat(char *, const char *, size_t); #endif -#ifndef HAVE_STRCASESTR -char *strcasestr(const char *, const char *); -#endif #ifndef HAVE_GETLINE ssize_t getline(char **, size_t *, FILE *); ssize_t getdelim(char **, size_t *, int, FILE *);