Skip to content

Commit 927adfb

Browse files
authored
Use a single version of mempcpy(3) (#12257)
While __php_mempcpy is only used by ext/standard/crypt_sha*, the mempcpy "pattern" is used everywhere. This commit removes __php_mempcpy, adds zend_mempcpy and transforms open-coded parts into function calls.
1 parent 1fc85a3 commit 927adfb

File tree

22 files changed

+96
-175
lines changed

22 files changed

+96
-175
lines changed

Zend/zend_operators.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen)
274274
#endif
275275
}
276276

277+
static zend_always_inline void *zend_mempcpy(void *dest, const void *src, size_t n)
278+
{
279+
#if defined(HAVE_MEMPCPY)
280+
return mempcpy(dest, src, n);
281+
#else
282+
return (char *)memcpy(dest, src, n) + n;
283+
#endif
284+
}
285+
277286
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1);
278287
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op2);
279288

Zend/zend_virtual_cwd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,7 @@ CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
16811681
dir = CWDG(cwd).cwd;
16821682

16831683
ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
1684-
memcpy(ptr, "cd ", sizeof("cd ")-1);
1685-
ptr += sizeof("cd ")-1;
1684+
ptr = zend_mempcpy(ptr, "cd ", sizeof("cd ") - 1);
16861685

16871686
if (CWDG(cwd).cwd_length == 0) {
16881687
*ptr++ = DEFAULT_SLASH;

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ asprintf \
640640
nanosleep \
641641
memmem \
642642
memrchr \
643+
mempcpy \
643644
)
644645

645646
AC_CHECK_FUNCS(inet_ntop,[],[

ext/ffi/ffi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,8 +1501,7 @@ static bool zend_ffi_ctype_name_append(zend_ffi_ctype_name_buf *buf, const char
15011501
if (buf->end + len > buf->buf + MAX_TYPE_NAME_LEN) {
15021502
return 0;
15031503
}
1504-
memcpy(buf->end, str, len);
1505-
buf->end += len;
1504+
buf->end = zend_mempcpy(buf->end, str, len);
15061505
return 1;
15071506
}
15081507
/* }}} */

ext/hash/hash_sha3.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ static void PHP_SHA3_Final(unsigned char* digest,
190190
// Square output for digest
191191
for(;;) {
192192
int bs = (len < block_size) ? len : block_size;
193-
memcpy(digest, ctx->state, bs);
194-
digest += bs;
193+
digest = zend_mempcpy(digest, ctx->state, bs);
195194
len -= bs;
196195
if (!len) break;
197196
permute(ctx);

ext/intl/converter/converter.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ static void php_converter_append_fromUnicode_target(zval *val, UConverterFromUni
265265
{
266266
size_t vallen = Z_STRLEN_P(val);
267267
if (TARGET_CHECK(args, vallen)) {
268-
memcpy(args->target, Z_STRVAL_P(val), vallen);
269-
args->target += vallen;
268+
args->target = zend_mempcpy(args->target, Z_STRVAL_P(val), vallen);
270269
}
271270
return;
272271
}

ext/mbstring/mbstring.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,8 +2621,7 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
26212621
append_trim_marker:
26222622
if (ZSTR_LEN(marker) > 0) {
26232623
MB_CONVERT_BUF_ENSURE((&buf), buf.out, buf.limit, ZSTR_LEN(marker));
2624-
memcpy(buf.out, ZSTR_VAL(marker), ZSTR_LEN(marker));
2625-
buf.out += ZSTR_LEN(marker);
2624+
buf.out = zend_mempcpy(buf.out, ZSTR_VAL(marker), ZSTR_LEN(marker));
26262625
}
26272626

26282627
/* Even if `enc` is UTF-8, don't mark the output string as valid UTF-8, because

ext/mysqlnd/mysqlnd_wireprotocol.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
516516

517517
if (packet->send_auth_data || packet->is_change_user_packet) {
518518
len = MIN(strlen(packet->user), MYSQLND_MAX_ALLOWED_USER_LEN);
519-
memcpy(p, packet->user, len);
520-
p+= len;
519+
p = zend_mempcpy(p, packet->user, len);
521520
*p++ = '\0';
522521

523522
/* defensive coding */
@@ -540,15 +539,13 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
540539
DBG_RETURN(0);
541540
}
542541
if (packet->auth_data_len) {
543-
memcpy(p, packet->auth_data, packet->auth_data_len);
544-
p+= packet->auth_data_len;
542+
p = zend_mempcpy(p, packet->auth_data, packet->auth_data_len);
545543
}
546544

547545
if (packet->db_len > 0) {
548546
/* CLIENT_CONNECT_WITH_DB should have been set */
549547
size_t real_db_len = MIN(MYSQLND_MAX_ALLOWED_DB_LEN, packet->db_len);
550-
memcpy(p, packet->db, real_db_len);
551-
p+= real_db_len;
548+
p = zend_mempcpy(p, packet->db, real_db_len);
552549
*p++= '\0';
553550
} else if (packet->is_change_user_packet) {
554551
*p++= '\0';
@@ -564,8 +561,7 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
564561

565562
if (packet->auth_plugin_name) {
566563
len = MIN(strlen(packet->auth_plugin_name), sizeof(buffer) - (p - buffer) - 1);
567-
memcpy(p, packet->auth_plugin_name, len);
568-
p+= len;
564+
p = zend_mempcpy(p, packet->auth_plugin_name, len);
569565
*p++= '\0';
570566
}
571567

@@ -599,12 +595,10 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
599595

600596
/* copy key */
601597
p = php_mysqlnd_net_store_length(p, ZSTR_LEN(key));
602-
memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
603-
p+= ZSTR_LEN(key);
598+
p = zend_mempcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
604599
/* copy value */
605600
p = php_mysqlnd_net_store_length(p, value_len);
606-
memcpy(p, Z_STRVAL_P(entry_value), value_len);
607-
p+= value_len;
601+
p = zend_mempcpy(p, Z_STRVAL_P(entry_value), value_len);
608602
}
609603
} ZEND_HASH_FOREACH_END();
610604
}
@@ -766,8 +760,7 @@ php_mysqlnd_change_auth_response_write(MYSQLND_CONN_DATA * conn, void * _packet)
766760
DBG_ENTER("php_mysqlnd_change_auth_response_write");
767761

768762
if (packet->auth_data_len) {
769-
memcpy(p, packet->auth_data, packet->auth_data_len);
770-
p+= packet->auth_data_len;
763+
p = zend_mempcpy(p, packet->auth_data, packet->auth_data_len);
771764
}
772765

773766
{

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,8 +3160,7 @@ static zend_string* ZEND_FASTCALL zend_jit_rope_end(zend_string **rope, uint32_t
31603160

31613161
char *target = ZSTR_VAL(ret);
31623162
for (i = 0; i <= count; i++) {
3163-
memcpy(target, ZSTR_VAL(rope[i]), ZSTR_LEN(rope[i]));
3164-
target += ZSTR_LEN(rope[i]);
3163+
target = zend_mempcpy(target, ZSTR_VAL(rope[i]), ZSTR_LEN(rope[i]));
31653164
zend_string_release_ex(rope[i], 0);
31663165
}
31673166
*target = '\0';

ext/opcache/shared_alloc_win32.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ static char *create_name_with_username(char *name)
7575
char *p = newname;
7676
p += strlcpy(newname, name, MAXPATHLEN + 1);
7777
*(p++) = '@';
78-
memcpy(p, accel_uname_id, 32);
79-
p += 32;
78+
p = zend_mempcpy(p, accel_uname_id, 32);
8079
*(p++) = '@';
8180
p += strlcpy(p, sapi_module.name, 21);
8281
*(p++) = '@';
83-
memcpy(p, zend_system_id, 32);
84-
p += 32;
82+
p = zend_mempcpy(p, zend_system_id, 32);
8583
*(p++) = '\0';
8684
ZEND_ASSERT(p - newname <= sizeof(newname));
8785

0 commit comments

Comments
 (0)