Coverage Report

Created: 2026-06-02 06:39

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