Skip to content

Commit 782ffd7

Browse files
authored
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
1 parent 32cdd33 commit 782ffd7

File tree

5 files changed

+20
-44
lines changed

5 files changed

+20
-44
lines changed

Zend/zend_compile.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,14 +1499,6 @@ ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_le
14991499
}
15001500
/* }}} */
15011501

1502-
static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
1503-
{
1504-
size_t len = 0;
1505-
while (*s++ && maxlen--) len++;
1506-
return len;
1507-
}
1508-
/* }}} */
1509-
15101502
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) /* {{{ */
15111503
{
15121504
size_t class_name_len;

Zend/zend_operators.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const
264264
}
265265
}
266266

267+
static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen)
268+
{
269+
#if defined(HAVE_STRNLEN)
270+
return strnlen(s, maxlen);
271+
#else
272+
const char *p = memchr(s, '\0', maxlen);
273+
return p ? p-s : maxlen;
274+
#endif
275+
}
276+
267277
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1);
268278
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op2);
269279

ext/exif/exif.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,6 @@ static ssize_t exif_read_from_stream_file_looped(php_stream *stream, char *buf,
234234
return total_read;
235235
}
236236

237-
/* {{{ php_strnlen
238-
* get length of string if buffer if less than buffer size or buffer size */
239-
static size_t php_strnlen(char* str, size_t maxlen) {
240-
size_t len = 0;
241-
242-
if (str && maxlen && *str) {
243-
do {
244-
len++;
245-
} while (--maxlen && *(++str));
246-
}
247-
return len;
248-
}
249237
/* }}} */
250238

251239
/* {{{ error messages */
@@ -2223,7 +2211,7 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c
22232211
value = NULL;
22242212
}
22252213
if (value) {
2226-
length = (int)php_strnlen(value, length);
2214+
length = (int)zend_strnlen(value, length);
22272215
info_value->s = estrndup(value, length);
22282216
info_data->length = length;
22292217
} else {
@@ -2254,7 +2242,7 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c
22542242
}
22552243
if (value) {
22562244
if (tag == TAG_MAKER_NOTE) {
2257-
length = (int) php_strnlen(value, length);
2245+
length = (int) zend_strnlen(value, length);
22582246
}
22592247

22602248
/* do not recompute length here */
@@ -3034,11 +3022,11 @@ static int exif_process_string(char **result, char *value, size_t byte_count) {
30343022
/* we cannot use strlcpy - here the problem is that we cannot use strlen to
30353023
* determine length of string and we cannot use strlcpy with len=byte_count+1
30363024
* because then we might get into an EXCEPTION if we exceed an allocated
3037-
* memory page...so we use php_strnlen in conjunction with memcpy and add the NUL
3025+
* memory page...so we use zend_strnlen in conjunction with memcpy and add the NUL
30383026
* char.
30393027
* estrdup would sometimes allocate more memory and does not return length
30403028
*/
3041-
if ((byte_count=php_strnlen(value, byte_count)) > 0) {
3029+
if ((byte_count=zend_strnlen(value, byte_count)) > 0) {
30423030
return exif_process_undefined(result, value, byte_count);
30433031
}
30443032
(*result) = estrndup("", 1); /* force empty string */
@@ -3412,7 +3400,7 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
34123400
switch(tag) {
34133401
case TAG_COPYRIGHT:
34143402
/* check for "<photographer> NUL <editor> NUL" */
3415-
if (byte_count>1 && (length=php_strnlen(value_ptr, byte_count)) > 0) {
3403+
if (byte_count>1 && (length=zend_strnlen(value_ptr, byte_count)) > 0) {
34163404
if (length<byte_count-1) {
34173405
/* When there are any characters after the first NUL */
34183406
EFREE_IF(ImageInfo->CopyrightPhotographer);
@@ -3776,10 +3764,10 @@ static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t
37763764
{
37773765
size_t l1, l2=0;
37783766

3779-
if ((l1 = php_strnlen(buffer+2, length-2)) > 0) {
3767+
if ((l1 = zend_strnlen(buffer+2, length-2)) > 0) {
37803768
exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2, l1);
37813769
if (length > 2+l1+1) {
3782-
l2 = php_strnlen(buffer+2+l1+1, length-2-l1-1);
3770+
l2 = zend_strnlen(buffer+2+l1+1, length-2-l1-1);
37833771
exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1, l2);
37843772
}
37853773
}

ext/phar/tar.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,6 @@ static int phar_tar_process_metadata(phar_entry_info *entry, php_stream *fp) /*
200200
}
201201
/* }}} */
202202

203-
#ifndef HAVE_STRNLEN
204-
static size_t strnlen(const char *s, size_t maxlen) {
205-
char *r = (char *)memchr(s, '\0', maxlen);
206-
return r ? r-s : maxlen;
207-
}
208-
#endif
209-
210203
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) /* {{{ */
211204
{
212205
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
286279
goto next;
287280
}
288281

289-
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)) {
282+
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)) {
290283
zend_off_t curloc;
291284
size_t sig_len;
292285

@@ -499,7 +492,7 @@ int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alia
499492
entry.link = NULL;
500493
/* link field is null-terminated unless it has 100 non-null chars.
501494
* Thus we cannot use strlen. */
502-
linkname_len = strnlen(hdr->linkname, 100);
495+
linkname_len = zend_strnlen(hdr->linkname, 100);
503496
if (entry.tar_type == TAR_LINK) {
504497
if (!zend_hash_str_exists(&myphar->manifest, hdr->linkname, linkname_len)) {
505498
if (error) {

main/spprintf.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@
175175

176176
/* }}} */
177177

178-
#if !HAVE_STRNLEN
179-
static size_t strnlen(const char *s, size_t maxlen) {
180-
char *r = memchr(s, '\0', maxlen);
181-
return r ? r-s : maxlen;
182-
}
183-
#endif
184-
185178
/*
186179
* Do format conversion placing the output in buffer
187180
*/
@@ -552,7 +545,7 @@ static void xbuf_format_converter(void *xbuf, bool is_char, const char *fmt, va_
552545
if (!adjust_precision) {
553546
s_len = strlen(s);
554547
} else {
555-
s_len = strnlen(s, precision);
548+
s_len = zend_strnlen(s, precision);
556549
}
557550
} else {
558551
s = S_NULL;

0 commit comments

Comments
 (0)