Coverage Report

Created: 2026-06-02 06:40

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
1.94M
#define SMART_STR_OVERHEAD   (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE + 1)
21
1.92M
#define SMART_STR_START_SIZE 256
22
1.92M
#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
978k
  (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
978k
{
30
978k
  if (UNEXPECTED(!str->s)) {
31
964k
    str->a = len <= SMART_STR_START_LEN
32
964k
        ? SMART_STR_START_LEN
33
964k
        : SMART_STR_NEW_LEN(len);
34
964k
    str->s = zend_string_alloc(str->a, 0);
35
964k
    ZSTR_LEN(str->s) = 0;
36
964k
  } else {
37
14.4k
    str->a = SMART_STR_NEW_LEN(len);
38
14.4k
    str->s = (zend_string *) erealloc2(str->s, str->a + _ZSTR_HEADER_SIZE + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s));
39
14.4k
  }
40
978k
}
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
194k
#define VK_ESCAPE '\e'
59
#endif
60
61
10.2k
static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
62
10.2k
  size_t i, len = l;
63
205k
  for (i = 0; i < l; ++i) {
64
195k
    char c = s[i];
65
195k
    if (c == '\n' || c == '\r' || c == '\t' ||
66
194k
      c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
67
1.30k
      len += 1;
68
194k
    } else if (c < 32 || c > 126) {
69
38.6k
      len += 3;
70
38.6k
    }
71
195k
  }
72
10.2k
  return len;
73
10.2k
}
74
75
10.2k
ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
76
10.2k
  char *res;
77
10.2k
  size_t i, len = zend_compute_escaped_string_len(s, l);
78
79
10.2k
  smart_str_alloc(str, len, 0);
80
10.2k
  res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
81
10.2k
  ZSTR_LEN(str->s) += len;
82
83
205k
  for (i = 0; i < l; ++i) {
84
195k
    unsigned char c = s[i];
85
195k
    if (c < 32 || c == '\\' || c > 126) {
86
39.9k
      *res++ = '\\';
87
39.9k
      switch (c) {
88
398
        case '\n': *res++ = 'n'; break;
89
182
        case '\r': *res++ = 'r'; break;
90
162
        case '\t': *res++ = 't'; break;
91
198
        case '\f': *res++ = 'f'; break;
92
32
        case '\v': *res++ = 'v'; break;
93
120
        case '\\': *res++ = '\\'; break;
94
216
        case VK_ESCAPE: *res++ = 'e'; break;
95
38.6k
        default:
96
38.6k
          *res++ = 'x';
97
38.6k
          if ((c >> 4) < 10) {
98
30.6k
            *res++ = (c >> 4) + '0';
99
30.6k
          } else {
100
8.06k
            *res++ = (c >> 4) + 'A' - 10;
101
8.06k
          }
102
38.6k
          if ((c & 0xf) < 10) {
103
23.4k
            *res++ = (c & 0xf) + '0';
104
23.4k
          } else {
105
15.2k
            *res++ = (c & 0xf) + 'A' - 10;
106
15.2k
          }
107
39.9k
      }
108
155k
    } else {
109
155k
      *res++ = c;
110
155k
    }
111
195k
  }
112
10.2k
}
113
114
ZEND_API void ZEND_FASTCALL smart_str_append_double(
115
113
    smart_str *str, double num, int precision, bool zero_fraction) {
116
113
  char buf[ZEND_DOUBLE_MAX_LENGTH];
117
  /* Model snprintf precision behavior. */
118
113
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
119
113
  smart_str_appends(str, buf);
120
113
  if (zero_fraction && zend_finite(num) && !strchr(buf, '.')) {
121
31
    smart_str_appendl(str, ".0", 2);
122
31
  }
123
113
}
124
125
1.97k
ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
126
1.97k
  va_list arg;
127
1.97k
  va_start(arg, format);
128
1.97k
  zend_printf_to_smart_str(dest, format, arg);
129
1.97k
  va_end(arg);
130
1.97k
}
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
10.0M
#define SMART_STRING_OVERHEAD   (ZEND_MM_OVERHEAD + 1)
140
10.0M
#define SMART_STRING_START_SIZE 256
141
10.0M
#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
5.01M
{
166
5.01M
  if (!str->c) {
167
5.01M
    str->len = 0;
168
5.01M
    if (len <= SMART_STRING_START_LEN) {
169
5.01M
      str->a = SMART_STRING_START_LEN;
170
5.01M
      str->c = emalloc(SMART_STRING_START_LEN + 1);
171
5.01M
    } else {
172
0
      str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
173
0
      if (EXPECTED(str->a < (ZEND_MM_CHUNK_SIZE - SMART_STRING_OVERHEAD))) {
174
0
        str->c = emalloc_large(str->a + 1);
175
0
      } else {
176
        /* allocate a huge chunk */
177
0
        str->c = emalloc(str->a + 1);
178
0
      }
179
0
    }
180
5.01M
  } else {
181
30
    if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
182
0
      zend_error_noreturn(E_ERROR, "String size overflow");
183
0
    }
184
30
    len += str->len;
185
30
    str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
186
30
    str->c = erealloc2(str->c, str->a + 1, str->len);
187
30
  }
188
5.01M
}
189
190
ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, const zend_string *value, size_t length)
191
9.99k
{
192
9.99k
  smart_str_append_escaped(str, ZSTR_VAL(value), MIN(length, ZSTR_LEN(value)));
193
194
9.99k
  if (ZSTR_LEN(value) > length) {
195
1.56k
    smart_str_appendl(str, "...", sizeof("...")-1);
196
1.56k
  }
197
9.99k
}
198
199
11.2k
ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *dest, const zval *value, size_t truncate) {
200
11.2k
  ZEND_ASSERT(Z_TYPE_P(value) <= IS_STRING);
201
202
11.2k
  switch (Z_TYPE_P(value)) {
203
4
    case IS_UNDEF:
204
1.11k
    case IS_NULL:
205
1.11k
      smart_str_appendl(dest, "NULL", sizeof("NULL")-1);
206
1.11k
    break;
207
208
10
    case IS_TRUE:
209
10
      smart_str_appendl(dest, "true", sizeof("true")-1);
210
10
    break;
211
212
10
    case IS_FALSE:
213
10
      smart_str_appendl(dest, "false", sizeof("false")-1);
214
10
    break;
215
216
4
    case IS_DOUBLE:
217
4
      smart_str_append_double(dest, Z_DVAL_P(value), (int) EG(precision), true);
218
4
    break;
219
220
100
    case IS_LONG:
221
100
      smart_str_append_long(dest, Z_LVAL_P(value));
222
100
    break;
223
224
9.99k
    case IS_STRING:
225
9.99k
      smart_str_appendc(dest, '\'');
226
9.99k
      smart_str_append_escaped_truncated(dest, Z_STR_P(value), truncate);
227
9.99k
      smart_str_appendc(dest, '\'');
228
9.99k
    break;
229
230
0
    default: ZEND_UNREACHABLE();
231
11.2k
  }
232
11.2k
}
233
234
ZEND_API zend_result ZEND_FASTCALL smart_str_append_zval(smart_str *dest, const zval *value, size_t truncate)
235
11.9k
{
236
11.9k
  if (Z_TYPE_P(value) <= IS_STRING) {
237
11.2k
    smart_str_append_scalar(dest, value, truncate);
238
11.2k
  } else if (Z_TYPE_P(value) == IS_OBJECT && (Z_OBJCE_P(value)->ce_flags & ZEND_ACC_ENUM)) {
239
2
    smart_str_append(dest, Z_OBJCE_P(value)->name);
240
2
    smart_str_appends(dest, "::");
241
2
    smart_str_append(dest, Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(value))));
242
676
  } else {
243
676
    return FAILURE;
244
676
  }
245
11.2k
  return SUCCESS;
246
11.9k
}