Skip to content

Commit 32b107e

Browse files
committed
Use smart_str_extend() instead of smart_str_alloc()
These usages were re-implementing the exact functionality of smart_str_extend().
1 parent 072c50f commit 32b107e

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

ext/standard/var.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,7 @@ static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{
714714
char b[32];
715715
char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val);
716716
size_t l = b + sizeof(b) - 1 - s;
717-
size_t new_len = smart_str_alloc(buf, 2 + l + 1, 0);
718-
char *res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
719-
720-
ZSTR_LEN(buf->s) = new_len;
717+
char *res = smart_str_extend(buf, 2 + l + 1);
721718
memcpy(res, "i:", 2);
722719
res += 2;
723720
memcpy(res, s, l);
@@ -730,10 +727,7 @@ static inline void php_var_serialize_string(smart_str *buf, char *str, size_t le
730727
char b[32];
731728
char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len);
732729
size_t l = b + sizeof(b) - 1 - s;
733-
size_t new_len = smart_str_alloc(buf, 2 + l + 2 + len + 2, 0);
734-
char *res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
735-
736-
ZSTR_LEN(buf->s) = new_len;
730+
char *res = smart_str_extend(buf, 2 + l + 2 + len + 2);
737731
memcpy(res, "s:", 2);
738732
res += 2;
739733
memcpy(res, s, l);
@@ -748,17 +742,14 @@ static inline void php_var_serialize_string(smart_str *buf, char *str, size_t le
748742

749743
static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
750744
{
751-
char b[32], *s, *res;
752-
size_t l, new_len;
745+
char b[32];
753746
PHP_CLASS_ATTRIBUTES;
754747

755748
PHP_SET_CLASS_ATTRIBUTES(struc);
756749
size_t class_name_len = ZSTR_LEN(class_name);
757-
s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
758-
l = b + sizeof(b) - 1 - s;
759-
new_len = smart_str_alloc(buf, 2 + l + 2 + class_name_len + 2, 0);
760-
res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
761-
ZSTR_LEN(buf->s) = new_len;
750+
char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
751+
size_t l = b + sizeof(b) - 1 - s;
752+
char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2);
762753
memcpy(res, "O:", 2);
763754
res += 2;
764755
memcpy(res, s, l);
@@ -1044,15 +1035,11 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
10441035
return;
10451036

10461037
case IS_DOUBLE: {
1047-
char tmp_str[PHP_DOUBLE_MAX_LENGTH], *res;
1048-
size_t len, new_len;
1049-
1038+
char tmp_str[PHP_DOUBLE_MAX_LENGTH];
10501039
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
1051-
len = strlen(tmp_str);
1052-
new_len = smart_str_alloc(buf, 2 + len + 1, 0);
1053-
res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
1054-
ZSTR_LEN(buf->s) = new_len;
10551040

1041+
size_t len = strlen(tmp_str);
1042+
char *res = smart_str_extend(buf, 2 + len + 1);
10561043
memcpy(res, "d:", 2);
10571044
res += 2;
10581045
memcpy(res, tmp_str, len);
@@ -1129,16 +1116,12 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
11291116
size_t serialized_length;
11301117

11311118
if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
1132-
char b1[32], b2[32], *s1, *s2, *res;
1133-
size_t l1, l2, new_len;
1134-
1135-
s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1136-
l1 = b1 + sizeof(b1) - 1 - s1;
1137-
s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1138-
l2 = b2 + sizeof(b2) - 1 - s2;
1139-
new_len = smart_str_alloc(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1, 0);
1140-
res = ZSTR_VAL(buf->s) + ZSTR_LEN(buf->s);
1141-
ZSTR_LEN(buf->s) = new_len;
1119+
char b1[32], b2[32];
1120+
char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1121+
size_t l1 = b1 + sizeof(b1) - 1 - s1;
1122+
char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1123+
size_t l2 = b2 + sizeof(b2) - 1 - s2;
1124+
char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1);
11421125
memcpy(res, "C:", 2);
11431126
res += 2;
11441127
memcpy(res, s1, l1);

0 commit comments

Comments
 (0)