Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_smart_str.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Dmitry Stogov <dmitry@php.net>                               |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#include <zend.h>
16
#include "zend_smart_str.h"
17
#include "zend_smart_string.h"
18
#include "zend_enum.h"
19
20
126k
#define SMART_STR_OVERHEAD   (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE + 1)
21
126k
#define SMART_STR_START_SIZE 256
22
126k
#define SMART_STR_START_LEN  (SMART_STR_START_SIZE - SMART_STR_OVERHEAD)
23
#define SMART_STR_PAGE       4096
24
25
#define SMART_STR_NEW_LEN(len) \
26
63.5k
  (ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STR_OVERHEAD, SMART_STR_PAGE) - SMART_STR_OVERHEAD)
27
28
ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
29
63.5k
{
30
63.5k
  if (UNEXPECTED(!str->s)) {
31
63.2k
    str->a = len <= SMART_STR_START_LEN
32
63.2k
        ? SMART_STR_START_LEN
33
63.2k
        : SMART_STR_NEW_LEN(len);
34
63.2k
    str->s = zend_string_alloc(str->a, 0);
35
63.2k
    ZSTR_LEN(str->s) = 0;
36
63.2k
  } else {
37
237
    str->a = SMART_STR_NEW_LEN(len);
38
237
    str->s = (zend_string *) erealloc2(str->s, str->a + _ZSTR_HEADER_SIZE + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s));
39
237
  }
40
63.5k
}
41
42
ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
43
0
{
44
0
  if (UNEXPECTED(!str->s)) {
45
0
    str->a = len <= SMART_STR_START_LEN
46
0
        ? SMART_STR_START_LEN
47
0
        : SMART_STR_NEW_LEN(len);
48
0
    str->s = zend_string_alloc(str->a, 1);
49
0
    ZSTR_LEN(str->s) = 0;
50
0
  } else {
51
0
    str->a = SMART_STR_NEW_LEN(len);
52
0
    str->s = (zend_string *) perealloc(str->s, str->a + _ZSTR_HEADER_SIZE + 1, 1);
53
0
  }
54
0
}
55
56
/* Windows uses VK_ESCAPE instead of \e */
57
#ifndef VK_ESCAPE
58
10.5k
#define VK_ESCAPE '\e'
59
#endif
60
61
802
static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
62
802
  size_t i, len = l;
63
12.3k
  for (i = 0; i < l; ++i) {
64
11.5k
    char c = s[i];
65
11.5k
    if (c == '\n' || c == '\r' || c == '\t' ||
66
10.9k
      c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
67
940
      len += 1;
68
10.5k
    } else if (c < 32 || c > 126) {
69
982
      len += 3;
70
982
    }
71
11.5k
  }
72
802
  return len;
73
802
}
74
75
802
ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
76
802
  char *res;
77
802
  size_t i, len = zend_compute_escaped_string_len(s, l);
78
79
802
  smart_str_alloc(str, len, 0);
80
802
  res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
81
802
  ZSTR_LEN(str->s) += len;
82
83
12.3k
  for (i = 0; i < l; ++i) {
84
11.5k
    unsigned char c = s[i];
85
11.5k
    if (c < 32 || c == '\\' || c > 126) {
86
1.92k
      *res++ = '\\';
87
1.92k
      switch (c) {
88
132
        case '\n': *res++ = 'n'; break;
89
78
        case '\r': *res++ = 'r'; break;
90
391
        case '\t': *res++ = 't'; break;
91
40
        case '\f': *res++ = 'f'; break;
92
297
        case '\v': *res++ = 'v'; break;
93
0
        case '\\': *res++ = '\\'; break;
94
2
        case VK_ESCAPE: *res++ = 'e'; break;
95
982
        default:
96
982
          *res++ = 'x';
97
982
          if ((c >> 4) < 10) {
98
286
            *res++ = (c >> 4) + '0';
99
696
          } else {
100
696
            *res++ = (c >> 4) + 'A' - 10;
101
696
          }
102
982
          if ((c & 0xf) < 10) {
103
703
            *res++ = (c & 0xf) + '0';
104
703
          } else {
105
279
            *res++ = (c & 0xf) + 'A' - 10;
106
279
          }
107
1.92k
      }
108
9.60k
    } else {
109
9.60k
      *res++ = c;
110
9.60k
    }
111
11.5k
  }
112
802
}
113
114
ZEND_API void ZEND_FASTCALL smart_str_append_double(
115
8
    smart_str *str, double num, int precision, bool zero_fraction) {
116
8
  char buf[ZEND_DOUBLE_MAX_LENGTH];
117
  /* Model snprintf precision behavior. */
118
8
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
119
8
  smart_str_appends(str, buf);
120
8
  if (zero_fraction && zend_finite(num) && !strchr(buf, '.')) {
121
1
    smart_str_appendl(str, ".0", 2);
122
1
  }
123
8
}
124
125
1.07k
ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
126
1.07k
  va_list arg;
127
1.07k
  va_start(arg, format);
128
1.07k
  zend_printf_to_smart_str(dest, format, arg);
129
1.07k
  va_end(arg);
130
1.07k
}
131
132
0
ZEND_API void smart_string_append_printf(smart_string *dest, const char *format, ...) {
133
0
  va_list arg;
134
0
  va_start(arg, format);
135
0
  zend_printf_to_smart_string(dest, format, arg);
136
0
  va_end(arg);
137
0
}
138
139
13.3M
#define SMART_STRING_OVERHEAD   (ZEND_MM_OVERHEAD + 1)
140
13.3M
#define SMART_STRING_START_SIZE 256
141
13.3M
#define SMART_STRING_START_LEN  (SMART_STRING_START_SIZE - SMART_STRING_OVERHEAD)
142
#define SMART_STRING_PAGE       4096
143
144
ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
145
0
{
146
0
  if (!str->c) {
147
0
    str->len = 0;
148
0
    if (len <= SMART_STRING_START_LEN) {
149
0
      str->a = SMART_STRING_START_LEN;
150
0
    } else {
151
0
      str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
152
0
    }
153
0
    str->c = pemalloc(str->a + 1, 1);
154
0
  } else {
155
0
    if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
156
0
      zend_error_noreturn(E_ERROR, "String size overflow");
157
0
    }
158
0
    len += str->len;
159
0
    str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
160
0
    str->c = perealloc(str->c, str->a + 1, 1);
161
0
  }
162
0
}
163
164
ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
165
6.65M
{
166
6.65M
  if (!str->c) {
167
6.65M
    str->len = 0;
168
6.65M
    if (len <= SMART_STRING_START_LEN) {
169
6.65M
      str->a = SMART_STRING_START_LEN;
170
6.65M
      str->c = emalloc(SMART_STRING_START_LEN + 1);
171
6.65M
    } else {
172
37
      str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
173
37
      if (EXPECTED(str->a < (ZEND_MM_CHUNK_SIZE - SMART_STRING_OVERHEAD))) {
174
37
        str->c = emalloc_large(str->a + 1);
175
37
      } else {
176
        /* allocate a huge chunk */
177
0
        str->c = emalloc(str->a + 1);
178
0
      }
179
37
    }
180
6.65M
  } else {
181
1
    if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
182
0
      zend_error_noreturn(E_ERROR, "String size overflow");
183
0
    }
184
1
    len += str->len;
185
1
    str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
186
1
    str->c = erealloc2(str->c, str->a + 1, str->len);
187
1
  }
188
6.65M
}
189
190
ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, const zend_string *value, size_t length)
191
719
{
192
719
  smart_str_append_escaped(str, ZSTR_VAL(value), MIN(length, ZSTR_LEN(value)));
193
194
719
  if (ZSTR_LEN(value) > length) {
195
20
    smart_str_appendl(str, "...", sizeof("...")-1);
196
20
  }
197
719
}
198
199
784
ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *dest, const zval *value, size_t truncate) {
200
784
  ZEND_ASSERT(Z_TYPE_P(value) <= IS_STRING);
201
202
784
  switch (Z_TYPE_P(value)) {
203
0
    case IS_UNDEF:
204
23
    case IS_NULL:
205
23
      smart_str_appendl(dest, "NULL", sizeof("NULL")-1);
206
23
    break;
207
208
6
    case IS_TRUE:
209
6
      smart_str_appendl(dest, "true", sizeof("true")-1);
210
6
    break;
211
212
6
    case IS_FALSE:
213
6
      smart_str_appendl(dest, "false", sizeof("false")-1);
214
6
    break;
215
216
5
    case IS_DOUBLE:
217
5
      smart_str_append_double(dest, Z_DVAL_P(value), (int) EG(precision), true);
218
5
    break;
219
220
25
    case IS_LONG:
221
25
      smart_str_append_long(dest, Z_LVAL_P(value));
222
25
    break;
223
224
719
    case IS_STRING:
225
719
      smart_str_appendc(dest, '\'');
226
719
      smart_str_append_escaped_truncated(dest, Z_STR_P(value), truncate);
227
719
      smart_str_appendc(dest, '\'');
228
719
    break;
229
230
0
    default: ZEND_UNREACHABLE();
231
784
  }
232
784
}
233
234
ZEND_API zend_result ZEND_FASTCALL smart_str_append_zval(smart_str *dest, const zval *value, size_t truncate)
235
1.48k
{
236
1.48k
  if (Z_TYPE_P(value) <= IS_STRING) {
237
784
    smart_str_append_scalar(dest, value, truncate);
238
784
  } else if (Z_TYPE_P(value) == IS_OBJECT && (Z_OBJCE_P(value)->ce_flags & ZEND_ACC_ENUM)) {
239
1
    smart_str_append(dest, Z_OBJCE_P(value)->name);
240
1
    smart_str_appends(dest, "::");
241
1
    smart_str_append(dest, Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(value))));
242
701
  } else {
243
701
    return FAILURE;
244
701
  }
245
785
  return SUCCESS;
246
1.48k
}