Use a single version of strnlen (#12015)

* Zend: Make zend_strnlen available for use outside zend_compile

* exif: remove local php_strnlen, use zend_strnlen instead

* main: remove local strnlen, use zend_strnlen instead

* phar: remove local strnlen, use zend_strnlen
This commit is contained in:
Cristian Rodríguez 2023-08-22 11:40:24 -04:00 committed by GitHub
parent 32cdd330f3
commit 782ffd761b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 44 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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 "<photographer> NUL <editor> 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 (length<byte_count-1) {
/* When there are any characters after the first NUL */
EFREE_IF(ImageInfo->CopyrightPhotographer);
@ -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);
}
}

View file

@ -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 @@ bail:
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) {

View file

@ -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;