Coverage Report

Created: 2026-06-01 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/univalue/lib/univalue_read.cpp
Line
Count
Source
1
// Copyright 2014 BitPay Inc.
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or https://opensource.org/licenses/mit-license.php.
4
5
#include <univalue.h>
6
#include <univalue_utffilter.h>
7
8
#include <cstdint>
9
#include <cstring>
10
#include <string>
11
#include <string_view>
12
#include <vector>
13
14
/*
15
 * According to stackexchange, the original json test suite wanted
16
 * to limit depth to 22.  Widely-deployed PHP bails at depth 512,
17
 * so we will follow PHP's lead, which should be more than sufficient
18
 * (further stackexchange comments indicate depth > 32 rarely occurs).
19
 */
20
static constexpr size_t MAX_JSON_DEPTH = 512;
21
22
static bool json_isdigit(int ch)
23
1.82k
{
24
1.82k
    return ((ch >= '0') && (ch <= '9'));
  Branch (24:13): [True: 913, False: 913]
  Branch (24:28): [True: 913, False: 0]
25
1.82k
}
26
27
// convert hexadecimal string to unsigned integer
28
static const char *hatoui(const char *first, const char *last,
29
                          unsigned int& out)
30
0
{
31
0
    unsigned int result = 0;
32
0
    for (; first != last; ++first)
  Branch (32:12): [True: 0, False: 0]
33
0
    {
34
0
        int digit;
35
0
        if (json_isdigit(*first))
  Branch (35:13): [True: 0, False: 0]
36
0
            digit = *first - '0';
37
38
0
        else if (*first >= 'a' && *first <= 'f')
  Branch (38:18): [True: 0, False: 0]
  Branch (38:35): [True: 0, False: 0]
39
0
            digit = *first - 'a' + 10;
40
41
0
        else if (*first >= 'A' && *first <= 'F')
  Branch (41:18): [True: 0, False: 0]
  Branch (41:35): [True: 0, False: 0]
42
0
            digit = *first - 'A' + 10;
43
44
0
        else
45
0
            break;
46
47
0
        result = 16 * result + digit;
48
0
    }
49
0
    out = result;
50
51
0
    return first;
52
0
}
53
54
enum jtokentype getJsonToken(std::string& tokenVal, unsigned int& consumed,
55
                            const char *raw, const char *end)
56
17.6k
{
57
17.6k
    tokenVal.clear();
58
17.6k
    consumed = 0;
59
60
17.6k
    const char *rawStart = raw;
61
62
17.6k
    while (raw < end && (json_isspace(*raw)))          // skip whitespace
  Branch (62:12): [True: 16.7k, False: 913]
  Branch (62:25): [True: 0, False: 16.7k]
63
0
        raw++;
64
65
17.6k
    if (raw >= end)
  Branch (65:9): [True: 913, False: 16.7k]
66
913
        return JTOK_NONE;
67
68
16.7k
    switch (*raw) {
69
70
913
    case '{':
  Branch (70:5): [True: 913, False: 15.8k]
71
913
        raw++;
72
913
        consumed = (raw - rawStart);
73
913
        return JTOK_OBJ_OPEN;
74
913
    case '}':
  Branch (74:5): [True: 913, False: 15.8k]
75
913
        raw++;
76
913
        consumed = (raw - rawStart);
77
913
        return JTOK_OBJ_CLOSE;
78
913
    case '[':
  Branch (78:5): [True: 913, False: 15.8k]
79
913
        raw++;
80
913
        consumed = (raw - rawStart);
81
913
        return JTOK_ARR_OPEN;
82
913
    case ']':
  Branch (82:5): [True: 913, False: 15.8k]
83
913
        raw++;
84
913
        consumed = (raw - rawStart);
85
913
        return JTOK_ARR_CLOSE;
86
87
3.65k
    case ':':
  Branch (87:5): [True: 3.65k, False: 13.0k]
88
3.65k
        raw++;
89
3.65k
        consumed = (raw - rawStart);
90
3.65k
        return JTOK_COLON;
91
2.73k
    case ',':
  Branch (91:5): [True: 2.73k, False: 13.9k]
92
2.73k
        raw++;
93
2.73k
        consumed = (raw - rawStart);
94
2.73k
        return JTOK_COMMA;
95
96
0
    case 'n':
  Branch (96:5): [True: 0, False: 16.7k]
97
0
    case 't':
  Branch (97:5): [True: 0, False: 16.7k]
98
0
    case 'f':
  Branch (98:5): [True: 0, False: 16.7k]
99
0
        if (!strncmp(raw, "null", 4)) {
  Branch (99:13): [True: 0, False: 0]
100
0
            raw += 4;
101
0
            consumed = (raw - rawStart);
102
0
            return JTOK_KW_NULL;
103
0
        } else if (!strncmp(raw, "true", 4)) {
  Branch (103:20): [True: 0, False: 0]
104
0
            raw += 4;
105
0
            consumed = (raw - rawStart);
106
0
            return JTOK_KW_TRUE;
107
0
        } else if (!strncmp(raw, "false", 5)) {
  Branch (107:20): [True: 0, False: 0]
108
0
            raw += 5;
109
0
            consumed = (raw - rawStart);
110
0
            return JTOK_KW_FALSE;
111
0
        } else
112
0
            return JTOK_ERR;
113
114
0
    case '-':
  Branch (114:5): [True: 0, False: 16.7k]
115
0
    case '0':
  Branch (115:5): [True: 0, False: 16.7k]
116
0
    case '1':
  Branch (116:5): [True: 0, False: 16.7k]
117
305
    case '2':
  Branch (117:5): [True: 305, False: 16.4k]
118
609
    case '3':
  Branch (118:5): [True: 304, False: 16.4k]
119
913
    case '4':
  Branch (119:5): [True: 304, False: 16.4k]
120
913
    case '5':
  Branch (120:5): [True: 0, False: 16.7k]
121
913
    case '6':
  Branch (121:5): [True: 0, False: 16.7k]
122
913
    case '7':
  Branch (122:5): [True: 0, False: 16.7k]
123
913
    case '8':
  Branch (123:5): [True: 0, False: 16.7k]
124
913
    case '9': {
  Branch (124:5): [True: 0, False: 16.7k]
125
        // part 1: int
126
913
        std::string numStr;
127
128
913
        const char *first = raw;
129
130
913
        const char *firstDigit = first;
131
913
        if (!json_isdigit(*firstDigit))
  Branch (131:13): [True: 0, False: 913]
132
0
            firstDigit++;
133
913
        if ((*firstDigit == '0') && json_isdigit(firstDigit[1]))
  Branch (133:13): [True: 0, False: 913]
  Branch (133:37): [True: 0, False: 0]
134
0
            return JTOK_ERR;
135
136
913
        numStr += *raw;                       // copy first char
137
913
        raw++;
138
139
913
        if ((*first == '-') && (raw < end) && (!json_isdigit(*raw)))
  Branch (139:13): [True: 0, False: 913]
  Branch (139:32): [True: 0, False: 0]
  Branch (139:47): [True: 0, False: 0]
140
0
            return JTOK_ERR;
141
142
913
        while (raw < end && json_isdigit(*raw)) {  // copy digits
  Branch (142:16): [True: 913, False: 0]
  Branch (142:29): [True: 0, False: 913]
143
0
            numStr += *raw;
144
0
            raw++;
145
0
        }
146
147
        // part 2: frac
148
913
        if (raw < end && *raw == '.') {
  Branch (148:13): [True: 913, False: 0]
  Branch (148:26): [True: 0, False: 913]
149
0
            numStr += *raw;                   // copy .
150
0
            raw++;
151
152
0
            if (raw >= end || !json_isdigit(*raw))
  Branch (152:17): [True: 0, False: 0]
  Branch (152:31): [True: 0, False: 0]
153
0
                return JTOK_ERR;
154
0
            while (raw < end && json_isdigit(*raw)) { // copy digits
  Branch (154:20): [True: 0, False: 0]
  Branch (154:33): [True: 0, False: 0]
155
0
                numStr += *raw;
156
0
                raw++;
157
0
            }
158
0
        }
159
160
        // part 3: exp
161
913
        if (raw < end && (*raw == 'e' || *raw == 'E')) {
  Branch (161:13): [True: 913, False: 0]
  Branch (161:27): [True: 0, False: 913]
  Branch (161:42): [True: 0, False: 913]
162
0
            numStr += *raw;                   // copy E
163
0
            raw++;
164
165
0
            if (raw < end && (*raw == '-' || *raw == '+')) { // copy +/-
  Branch (165:17): [True: 0, False: 0]
  Branch (165:31): [True: 0, False: 0]
  Branch (165:46): [True: 0, False: 0]
166
0
                numStr += *raw;
167
0
                raw++;
168
0
            }
169
170
0
            if (raw >= end || !json_isdigit(*raw))
  Branch (170:17): [True: 0, False: 0]
  Branch (170:31): [True: 0, False: 0]
171
0
                return JTOK_ERR;
172
0
            while (raw < end && json_isdigit(*raw)) { // copy digits
  Branch (172:20): [True: 0, False: 0]
  Branch (172:33): [True: 0, False: 0]
173
0
                numStr += *raw;
174
0
                raw++;
175
0
            }
176
0
        }
177
178
913
        tokenVal = numStr;
179
913
        consumed = (raw - rawStart);
180
913
        return JTOK_NUMBER;
181
913
        }
182
183
5.78k
    case '"': {
  Branch (183:5): [True: 5.78k, False: 10.9k]
184
5.78k
        raw++;                                // skip "
185
186
5.78k
        std::string valStr;
187
5.78k
        JSONUTF8StringFilter writer(valStr);
188
189
90.0k
        while (true) {
  Branch (189:16): [Folded - Ignored]
190
90.0k
            if (raw >= end || (unsigned char)*raw < 0x20)
  Branch (190:17): [True: 0, False: 90.0k]
  Branch (190:31): [True: 0, False: 90.0k]
191
0
                return JTOK_ERR;
192
193
90.0k
            else if (*raw == '\\') {
  Branch (193:22): [True: 1.52k, False: 88.4k]
194
1.52k
                raw++;                        // skip backslash
195
196
1.52k
                if (raw >= end)
  Branch (196:21): [True: 0, False: 1.52k]
197
0
                    return JTOK_ERR;
198
199
1.52k
                switch (*raw) {
200
0
                case '"':  writer.push_back('\"'); break;
  Branch (200:17): [True: 0, False: 1.52k]
201
0
                case '\\': writer.push_back('\\'); break;
  Branch (201:17): [True: 0, False: 1.52k]
202
0
                case '/':  writer.push_back('/'); break;
  Branch (202:17): [True: 0, False: 1.52k]
203
0
                case 'b':  writer.push_back('\b'); break;
  Branch (203:17): [True: 0, False: 1.52k]
204
0
                case 'f':  writer.push_back('\f'); break;
  Branch (204:17): [True: 0, False: 1.52k]
205
1.52k
                case 'n':  writer.push_back('\n'); break;
  Branch (205:17): [True: 1.52k, False: 0]
206
0
                case 'r':  writer.push_back('\r'); break;
  Branch (206:17): [True: 0, False: 1.52k]
207
0
                case 't':  writer.push_back('\t'); break;
  Branch (207:17): [True: 0, False: 1.52k]
208
209
0
                case 'u': {
  Branch (209:17): [True: 0, False: 1.52k]
210
0
                    unsigned int codepoint;
211
0
                    if (raw + 1 + 4 >= end ||
  Branch (211:25): [True: 0, False: 0]
212
0
                        hatoui(raw + 1, raw + 1 + 4, codepoint) !=
  Branch (212:25): [True: 0, False: 0]
213
0
                               raw + 1 + 4)
214
0
                        return JTOK_ERR;
215
0
                    writer.push_back_u(codepoint);
216
0
                    raw += 4;
217
0
                    break;
218
0
                    }
219
0
                default:
  Branch (219:17): [True: 0, False: 1.52k]
220
0
                    return JTOK_ERR;
221
222
1.52k
                }
223
224
1.52k
                raw++;                        // skip esc'd char
225
1.52k
            }
226
227
88.4k
            else if (*raw == '"') {
  Branch (227:22): [True: 5.78k, False: 82.7k]
228
5.78k
                raw++;                        // skip "
229
5.78k
                break;                        // stop scanning
230
5.78k
            }
231
232
82.7k
            else {
233
82.7k
                writer.push_back(static_cast<unsigned char>(*raw));
234
82.7k
                raw++;
235
82.7k
            }
236
90.0k
        }
237
238
5.78k
        if (!writer.finalize())
  Branch (238:13): [True: 0, False: 5.78k]
239
0
            return JTOK_ERR;
240
5.78k
        tokenVal = valStr;
241
5.78k
        consumed = (raw - rawStart);
242
5.78k
        return JTOK_STRING;
243
5.78k
        }
244
245
0
    default:
  Branch (245:5): [True: 0, False: 16.7k]
246
0
        return JTOK_ERR;
247
16.7k
    }
248
16.7k
}
249
250
enum expect_bits : unsigned {
251
    EXP_OBJ_NAME = (1U << 0),
252
    EXP_COLON = (1U << 1),
253
    EXP_ARR_VALUE = (1U << 2),
254
    EXP_VALUE = (1U << 3),
255
    EXP_NOT_VALUE = (1U << 4),
256
};
257
258
82.7k
#define expect(bit) (expectMask & (EXP_##bit))
259
20.3k
#define setExpect(bit) (expectMask |= EXP_##bit)
260
21.3k
#define clearExpect(bit) (expectMask &= ~EXP_##bit)
261
262
bool UniValue::read(std::string_view str_in)
263
913
{
264
913
    clear();
265
266
913
    uint32_t expectMask = 0;
267
913
    std::vector<UniValue*> stack;
268
269
913
    std::string tokenVal;
270
913
    unsigned int consumed;
271
913
    enum jtokentype tok = JTOK_NONE;
272
913
    enum jtokentype last_tok = JTOK_NONE;
273
913
    const char* raw{str_in.data()};
274
913
    const char* end{raw + str_in.size()};
275
16.7k
    do {
276
16.7k
        last_tok = tok;
277
278
16.7k
        tok = getJsonToken(tokenVal, consumed, raw, end);
279
16.7k
        if (tok == JTOK_NONE || tok == JTOK_ERR)
  Branch (279:13): [True: 0, False: 16.7k]
  Branch (279:33): [True: 0, False: 16.7k]
280
0
            return false;
281
16.7k
        raw += consumed;
282
283
16.7k
        bool isValueOpen = jsonTokenIsValue(tok) ||
  Branch (283:28): [True: 6.69k, False: 10.0k]
284
16.7k
            tok == JTOK_OBJ_OPEN || tok == JTOK_ARR_OPEN;
  Branch (284:13): [True: 913, False: 9.13k]
  Branch (284:37): [True: 913, False: 8.21k]
285
286
16.7k
        if (expect(VALUE)) {
287
3.65k
            if (!isValueOpen)
  Branch (287:17): [True: 0, False: 3.65k]
288
0
                return false;
289
3.65k
            clearExpect(VALUE);
290
291
13.0k
        } else if (expect(ARR_VALUE)) {
292
913
            bool isArrValue = isValueOpen || (tok == JTOK_ARR_CLOSE);
  Branch (292:31): [True: 304, False: 609]
  Branch (292:46): [True: 609, False: 0]
293
913
            if (!isArrValue)
  Branch (293:17): [True: 0, False: 913]
294
0
                return false;
295
296
913
            clearExpect(ARR_VALUE);
297
298
12.1k
        } else if (expect(OBJ_NAME)) {
299
3.65k
            bool isObjName = (tok == JTOK_OBJ_CLOSE || tok == JTOK_STRING);
  Branch (299:31): [True: 0, False: 3.65k]
  Branch (299:56): [True: 3.65k, False: 0]
300
3.65k
            if (!isObjName)
  Branch (300:17): [True: 0, False: 3.65k]
301
0
                return false;
302
303
8.52k
        } else if (expect(COLON)) {
304
3.65k
            if (tok != JTOK_COLON)
  Branch (304:17): [True: 0, False: 3.65k]
305
0
                return false;
306
3.65k
            clearExpect(COLON);
307
308
4.86k
        } else if (!expect(COLON) && (tok == JTOK_COLON)) {
  Branch (308:20): [True: 4.86k, False: 0]
  Branch (308:38): [True: 0, False: 4.86k]
309
0
            return false;
310
0
        }
311
312
16.7k
        if (expect(NOT_VALUE)) {
313
7.60k
            if (isValueOpen)
  Branch (313:17): [True: 0, False: 7.60k]
314
0
                return false;
315
7.60k
            clearExpect(NOT_VALUE);
316
7.60k
        }
317
318
16.7k
        switch (tok) {
319
320
913
        case JTOK_OBJ_OPEN:
  Branch (320:9): [True: 913, False: 15.8k]
321
1.82k
        case JTOK_ARR_OPEN: {
  Branch (321:9): [True: 913, False: 15.8k]
322
1.82k
            VType utyp = (tok == JTOK_OBJ_OPEN ? VOBJ : VARR);
  Branch (322:27): [True: 913, False: 913]
323
1.82k
            if (!stack.size()) {
  Branch (323:17): [True: 913, False: 913]
324
913
                if (utyp == VOBJ)
  Branch (324:21): [True: 913, False: 0]
325
913
                    setObject();
326
0
                else
327
0
                    setArray();
328
913
                stack.push_back(this);
329
913
            } else {
330
913
                UniValue tmpVal(utyp);
331
913
                UniValue *top = stack.back();
332
913
                top->values.push_back(tmpVal);
333
334
913
                UniValue *newTop = &(top->values.back());
335
913
                stack.push_back(newTop);
336
913
            }
337
338
1.82k
            if (stack.size() > MAX_JSON_DEPTH)
  Branch (338:17): [True: 0, False: 1.82k]
339
0
                return false;
340
341
1.82k
            if (utyp == VOBJ)
  Branch (341:17): [True: 913, False: 913]
342
913
                setExpect(OBJ_NAME);
343
913
            else
344
913
                setExpect(ARR_VALUE);
345
1.82k
            break;
346
1.82k
            }
347
348
913
        case JTOK_OBJ_CLOSE:
  Branch (348:9): [True: 913, False: 15.8k]
349
1.82k
        case JTOK_ARR_CLOSE: {
  Branch (349:9): [True: 913, False: 15.8k]
350
1.82k
            if (!stack.size() || (last_tok == JTOK_COMMA))
  Branch (350:17): [True: 0, False: 1.82k]
  Branch (350:34): [True: 0, False: 1.82k]
351
0
                return false;
352
353
1.82k
            VType utyp = (tok == JTOK_OBJ_CLOSE ? VOBJ : VARR);
  Branch (353:27): [True: 913, False: 913]
354
1.82k
            UniValue *top = stack.back();
355
1.82k
            if (utyp != top->getType())
  Branch (355:17): [True: 0, False: 1.82k]
356
0
                return false;
357
358
1.82k
            stack.pop_back();
359
1.82k
            clearExpect(OBJ_NAME);
360
1.82k
            setExpect(NOT_VALUE);
361
1.82k
            break;
362
1.82k
            }
363
364
3.65k
        case JTOK_COLON: {
  Branch (364:9): [True: 3.65k, False: 13.0k]
365
3.65k
            if (!stack.size())
  Branch (365:17): [True: 0, False: 3.65k]
366
0
                return false;
367
368
3.65k
            UniValue *top = stack.back();
369
3.65k
            if (top->getType() != VOBJ)
  Branch (369:17): [True: 0, False: 3.65k]
370
0
                return false;
371
372
3.65k
            setExpect(VALUE);
373
3.65k
            break;
374
3.65k
            }
375
376
2.73k
        case JTOK_COMMA: {
  Branch (376:9): [True: 2.73k, False: 13.9k]
377
2.73k
            if (!stack.size() ||
  Branch (377:17): [True: 0, False: 2.73k]
378
2.73k
                (last_tok == JTOK_COMMA) || (last_tok == JTOK_ARR_OPEN))
  Branch (378:17): [True: 0, False: 2.73k]
  Branch (378:45): [True: 0, False: 2.73k]
379
0
                return false;
380
381
2.73k
            UniValue *top = stack.back();
382
2.73k
            if (top->getType() == VOBJ)
  Branch (382:17): [True: 2.73k, False: 0]
383
2.73k
                setExpect(OBJ_NAME);
384
0
            else
385
0
                setExpect(ARR_VALUE);
386
2.73k
            break;
387
2.73k
            }
388
389
0
        case JTOK_KW_NULL:
  Branch (389:9): [True: 0, False: 16.7k]
390
0
        case JTOK_KW_TRUE:
  Branch (390:9): [True: 0, False: 16.7k]
391
0
        case JTOK_KW_FALSE: {
  Branch (391:9): [True: 0, False: 16.7k]
392
0
            UniValue tmpVal;
393
0
            switch (tok) {
394
0
            case JTOK_KW_NULL:
  Branch (394:13): [True: 0, False: 0]
395
                // do nothing more
396
0
                break;
397
0
            case JTOK_KW_TRUE:
  Branch (397:13): [True: 0, False: 0]
398
0
                tmpVal.setBool(true);
399
0
                break;
400
0
            case JTOK_KW_FALSE:
  Branch (400:13): [True: 0, False: 0]
401
0
                tmpVal.setBool(false);
402
0
                break;
403
0
            default: /* impossible */ break;
  Branch (403:13): [True: 0, False: 0]
404
0
            }
405
406
0
            if (!stack.size()) {
  Branch (406:17): [True: 0, False: 0]
407
0
                *this = tmpVal;
408
0
                break;
409
0
            }
410
411
0
            UniValue *top = stack.back();
412
0
            top->values.push_back(tmpVal);
413
414
0
            setExpect(NOT_VALUE);
415
0
            break;
416
0
            }
417
418
913
        case JTOK_NUMBER: {
  Branch (418:9): [True: 913, False: 15.8k]
419
913
            UniValue tmpVal(VNUM, tokenVal);
420
913
            if (!stack.size()) {
  Branch (420:17): [True: 0, False: 913]
421
0
                *this = tmpVal;
422
0
                break;
423
0
            }
424
425
913
            UniValue *top = stack.back();
426
913
            top->values.push_back(tmpVal);
427
428
913
            setExpect(NOT_VALUE);
429
913
            break;
430
913
            }
431
432
5.78k
        case JTOK_STRING: {
  Branch (432:9): [True: 5.78k, False: 10.9k]
433
5.78k
            if (expect(OBJ_NAME)) {
434
3.65k
                UniValue *top = stack.back();
435
3.65k
                top->keys.push_back(tokenVal);
436
3.65k
                clearExpect(OBJ_NAME);
437
3.65k
                setExpect(COLON);
438
3.65k
            } else {
439
2.13k
                UniValue tmpVal(VSTR, tokenVal);
440
2.13k
                if (!stack.size()) {
  Branch (440:21): [True: 0, False: 2.13k]
441
0
                    *this = tmpVal;
442
0
                    break;
443
0
                }
444
2.13k
                UniValue *top = stack.back();
445
2.13k
                top->values.push_back(tmpVal);
446
2.13k
            }
447
448
5.78k
            setExpect(NOT_VALUE);
449
5.78k
            break;
450
5.78k
            }
451
452
0
        default:
  Branch (452:9): [True: 0, False: 16.7k]
453
0
            return false;
454
16.7k
        }
455
16.7k
    } while (!stack.empty ());
  Branch (455:14): [True: 15.8k, False: 913]
456
457
    /* Check that nothing follows the initial construct (parsed above).  */
458
913
    tok = getJsonToken(tokenVal, consumed, raw, end);
459
913
    if (tok != JTOK_NONE)
  Branch (459:9): [True: 0, False: 913]
460
0
        return false;
461
462
913
    return true;
463
913
}
464