Coverage Report

Created: 2026-11-05 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/tests/fuzz/fuzz_json_decode_encode.cc
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
 *
5
 *    Copyright 2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
6
 */
7
8
#include <open62541/types.h>
9
10
/* Decode a message, then encode, decode, encode.
11
 * The two encodings must be bit-equal. */
12
extern "C" int
13
24.4k
LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
14
24.4k
    UA_ByteString buf;
15
24.4k
    buf.data = (UA_Byte*)data;
16
24.4k
    buf.length = size;
17
18
24.4k
    UA_Variant value;
19
24.4k
    UA_Variant_init(&value);
20
21
24.4k
    UA_StatusCode retval = UA_decodeJson(&buf, &value, &UA_TYPES[UA_TYPES_VARIANT], NULL);
22
24.4k
    if(retval != UA_STATUSCODE_GOOD)
23
15.6k
        return 0;
24
25
    /* This can fail for now. For example length limits are not always computed
26
     * 100% identical between encoding and decoding. */
27
8.79k
    size_t jsonSize = UA_calcSizeJson(&value, &UA_TYPES[UA_TYPES_VARIANT], NULL);
28
8.79k
    if(jsonSize == 0) {
29
99
        UA_Variant_clear(&value);
30
99
        return 0;
31
99
    }
32
33
8.69k
    UA_ByteString buf2 = UA_BYTESTRING_NULL;
34
8.69k
    retval = UA_ByteString_allocBuffer(&buf2, jsonSize);
35
8.69k
    if(retval != UA_STATUSCODE_GOOD) {
36
0
        UA_Variant_clear(&value);
37
0
        return 0;
38
0
    }
39
40
8.69k
    retval = UA_encodeJson(&value, &UA_TYPES[UA_TYPES_VARIANT], &buf2, NULL);
41
8.69k
    UA_assert(retval == UA_STATUSCODE_GOOD);
42
43
8.69k
    UA_Variant value2;
44
8.69k
    UA_Variant_init(&value2);
45
8.69k
    retval = UA_decodeJson(&buf2, &value2, &UA_TYPES[UA_TYPES_VARIANT], NULL);
46
8.69k
    if(retval == UA_STATUSCODE_BADOUTOFMEMORY) {
47
0
        UA_Variant_clear(&value);
48
0
        UA_ByteString_clear(&buf2);
49
0
        return 0;
50
0
    }
51
8.69k
    UA_assert(retval == UA_STATUSCODE_GOOD);
52
53
8.69k
    UA_assert(UA_order(&value, &value2, &UA_TYPES[UA_TYPES_VARIANT]) == UA_ORDER_EQ);
54
55
8.69k
    UA_ByteString buf3 = UA_BYTESTRING_NULL;
56
8.69k
    retval = UA_ByteString_allocBuffer(&buf3, jsonSize);
57
8.69k
    if(retval != UA_STATUSCODE_GOOD) {
58
0
        UA_Variant_clear(&value);
59
0
        UA_Variant_clear(&value2);
60
0
        UA_ByteString_clear(&buf2);
61
0
        return 0;
62
0
    }
63
64
8.69k
    retval = UA_encodeJson(&value2, &UA_TYPES[UA_TYPES_VARIANT], &buf3, NULL);
65
8.69k
    UA_assert(retval == UA_STATUSCODE_GOOD);
66
67
8.69k
    UA_assert(buf2.length == buf3.length);
68
8.69k
    UA_assert(memcmp(buf2.data, buf3.data, buf2.length) == 0);
69
70
8.69k
    UA_Variant_clear(&value);
71
8.69k
    UA_Variant_clear(&value2);
72
8.69k
    UA_ByteString_clear(&buf2);
73
8.69k
    UA_ByteString_clear(&buf3);
74
8.69k
    return 0;
75
8.69k
}