From 43cff80dbf47730093abe15b268e304f58225493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= Date: Mon, 21 Aug 2023 21:42:37 +0000 Subject: [PATCH 1/4] Zend: Make zend_strnlen available for use outside zend_compile --- Zend/zend_compile.c | 8 -------- Zend/zend_operators.h | 10 ++++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 26705861c228c..8d56da9eac6d1 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -1499,14 +1499,6 @@ ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_le } /* }}} */ -static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */ -{ - size_t len = 0; - while (*s++ && maxlen--) len++; - return len; -} -/* }}} */ - ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */ { size_t class_name_len; diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index 4b47debc8032d..2c2d8ed7aa7fb 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -264,6 +264,16 @@ zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const } } +static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) +{ +#if defined(HAVE_STRNLEN) + return strnlen(s, maxlen); +#else + const char *p = memchr(s, '\0', maxlen); + return p ? p-s : maxlen; +#endif +} + ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1); ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op2); From ed9c0bb354b08146545939857169276dad6c73dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= Date: Mon, 21 Aug 2023 21:45:17 +0000 Subject: [PATCH 2/4] exif: remove local php_strnlen, use zend_strnlen instead --- ext/exif/exif.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/ext/exif/exif.c b/ext/exif/exif.c index fa5ec9c855d4a..aa83fd969ef42 100644 --- a/ext/exif/exif.c +++ b/ext/exif/exif.c @@ -234,18 +234,6 @@ static ssize_t exif_read_from_stream_file_looped(php_stream *stream, char *buf, return total_read; } -/* {{{ php_strnlen - * get length of string if buffer if less than buffer size or buffer size */ -static size_t php_strnlen(char* str, size_t maxlen) { - size_t len = 0; - - if (str && maxlen && *str) { - do { - len++; - } while (--maxlen && *(++str)); - } - return len; -} /* }}} */ /* {{{ error messages */ @@ -2223,7 +2211,7 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c value = NULL; } if (value) { - length = (int)php_strnlen(value, length); + length = (int)zend_strnlen(value, length); info_value->s = estrndup(value, length); info_data->length = length; } else { @@ -2254,7 +2242,7 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c } if (value) { if (tag == TAG_MAKER_NOTE) { - length = (int) php_strnlen(value, length); + length = (int) zend_strnlen(value, length); } /* do not recompute length here */ @@ -3034,11 +3022,11 @@ static int exif_process_string(char **result, char *value, size_t byte_count) { /* we cannot use strlcpy - here the problem is that we cannot use strlen to * determine length of string and we cannot use strlcpy with len=byte_count+1 * because then we might get into an EXCEPTION if we exceed an allocated - * memory page...so we use php_strnlen in conjunction with memcpy and add the NUL + * memory page...so we use zend_strnlen in conjunction with memcpy and add the NUL * char. * estrdup would sometimes allocate more memory and does not return length */ - if ((byte_count=php_strnlen(value, byte_count)) > 0) { + if ((byte_count=zend_strnlen(value, byte_count)) > 0) { return exif_process_undefined(result, value, byte_count); } (*result) = estrndup("", 1); /* force empty string */ @@ -3412,7 +3400,7 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr switch(tag) { case TAG_COPYRIGHT: /* check for " NUL NUL" */ - if (byte_count>1 && (length=php_strnlen(value_ptr, byte_count)) > 0) { + if (byte_count>1 && (length=zend_strnlen(value_ptr, byte_count)) > 0) { if (lengthCopyrightPhotographer); @@ -3776,10 +3764,10 @@ static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t { size_t l1, l2=0; - if ((l1 = php_strnlen(buffer+2, length-2)) > 0) { + if ((l1 = zend_strnlen(buffer+2, length-2)) > 0) { exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2, l1); if (length > 2+l1+1) { - l2 = php_strnlen(buffer+2+l1+1, length-2-l1-1); + l2 = zend_strnlen(buffer+2+l1+1, length-2-l1-1); exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1, l2); } } From 9eb54324a670e8b22652366a7aeb2e95fe781d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= Date: Mon, 21 Aug 2023 21:47:41 +0000 Subject: [PATCH 3/4] main: remove local strnlen, use zend_strnlen instead --- main/spprintf.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/main/spprintf.c b/main/spprintf.c index 37b81dc6d530b..64bde33406073 100644 --- a/main/spprintf.c +++ b/main/spprintf.c @@ -175,13 +175,6 @@ /* }}} */ -#if !HAVE_STRNLEN -static size_t strnlen(const char *s, size_t maxlen) { - char *r = memchr(s, '\0', maxlen); - return r ? r-s : maxlen; -} -#endif - /* * Do format conversion placing the output in buffer */ @@ -552,7 +545,7 @@ static void xbuf_format_converter(void *xbuf, bool is_char, const char *fmt, va_ if (!adjust_precision) { s_len = strlen(s); } else { - s_len = strnlen(s, precision); + s_len = zend_strnlen(s, precision); } } else { s = S_NULL; From 6bd1193c9ef44239191cbee796b6aec5ed212e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= Date: Mon, 21 Aug 2023 21:54:33 +0000 Subject: [PATCH 4/4] phar: remove local strnlen, use zend_strnlen --- ext/phar/tar.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/ext/phar/tar.c b/ext/phar/tar.c index 60e248c78df5f..ce8ee0a369882 100644 --- a/ext/phar/tar.c +++ b/ext/phar/tar.c @@ -200,13 +200,6 @@ static int phar_tar_process_metadata(phar_entry_info *entry, php_stream *fp) /* } /* }}} */ -#ifndef HAVE_STRNLEN -static size_t strnlen(const char *s, size_t maxlen) { - char *r = (char *)memchr(s, '\0', maxlen); - return r ? r-s : maxlen; -} -#endif - int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, int is_data, uint32_t compression, char **error) /* {{{ */ { char buf[512], *actual_alias = NULL, *p; @@ -286,7 +279,7 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia goto next; } - if (((!old && hdr->prefix[0] == 0) || old) && strnlen(hdr->name, 100) == sizeof(".phar/signature.bin")-1 && !strncmp(hdr->name, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) { + if (((!old && hdr->prefix[0] == 0) || old) && zend_strnlen(hdr->name, 100) == sizeof(".phar/signature.bin")-1 && !strncmp(hdr->name, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) { zend_off_t curloc; size_t sig_len; @@ -499,7 +492,7 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia entry.link = NULL; /* link field is null-terminated unless it has 100 non-null chars. * Thus we cannot use strlen. */ - linkname_len = strnlen(hdr->linkname, 100); + linkname_len = zend_strnlen(hdr->linkname, 100); if (entry.tar_type == TAR_LINK) { if (!zend_hash_str_exists(&myphar->manifest, hdr->linkname, linkname_len)) { if (error) {