UA_readNumberWithBase:
  110|    572|UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base) {
  111|    572|    UA_assert(buf);
  ------------------
  |  |  399|    572|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (111:5): [True: 572, False: 0]
  ------------------
  112|    572|    UA_assert(number);
  ------------------
  |  |  399|    572|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (112:5): [True: 572, False: 0]
  ------------------
  113|    572|    u32 n = 0;
  114|    572|    size_t progress = 0;
  115|       |    /* read numbers until the end or a non-number character appears */
  116|  7.86M|    while(progress < buflen) {
  ------------------
  |  Branch (116:11): [True: 7.86M, False: 272]
  ------------------
  117|  7.86M|        u8 c = buf[progress];
  118|  7.86M|        if(c >= '0' && c <= '9' && c <= '0' + (base-1))
  ------------------
  |  Branch (118:12): [True: 7.86M, False: 211]
  |  Branch (118:24): [True: 7.85M, False: 89]
  |  Branch (118:36): [True: 7.85M, False: 0]
  ------------------
  119|  7.85M|           n = (n * base) + c - '0';
  120|    300|        else if(base > 9 && c >= 'a' && c <= 'z' && c <= 'a' + (base-11))
  ------------------
  |  Branch (120:17): [True: 300, False: 0]
  |  Branch (120:29): [True: 55, False: 245]
  |  Branch (120:41): [True: 18, False: 37]
  |  Branch (120:53): [True: 0, False: 18]
  ------------------
  121|      0|           n = (n * base) + c-'a' + 10;
  122|    300|        else if(base > 9 && c >= 'A' && c <= 'Z' && c <= 'A' + (base-11))
  ------------------
  |  Branch (122:17): [True: 300, False: 0]
  |  Branch (122:29): [True: 70, False: 230]
  |  Branch (122:41): [True: 10, False: 60]
  |  Branch (122:53): [True: 0, False: 10]
  ------------------
  123|      0|           n = (n * base) + c-'A' + 10;
  124|    300|        else
  125|    300|           break;
  126|  7.85M|        ++progress;
  127|  7.85M|    }
  128|    572|    *number = n;
  129|    572|    return progress;
  130|    572|}
UA_readNumber:
  133|    572|UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number) {
  134|    572|    return UA_readNumberWithBase(buf, buflen, number, 10);
  135|    572|}
UA_parseEndpointUrl:
  146|    502|                    UA_UInt16 *outPort, UA_String *outPath) {
  147|    502|    if(!endpointUrl || !endpointUrl->data)
  ------------------
  |  Branch (147:8): [True: 0, False: 502]
  |  Branch (147:24): [True: 0, False: 502]
  ------------------
  148|      0|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      0|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  149|       |
  150|       |    /* Which type of schema is this? */
  151|    502|    unsigned schemaType = 0;
  152|    502|    size_t schemaLen = 0;
  153|  2.59k|    for(; schemaType < UA_SCHEMAS_SIZE; schemaType++) {
  ------------------
  |  |  137|  2.59k|#define UA_SCHEMAS_SIZE 6
  ------------------
  |  Branch (153:11): [True: 2.49k, False: 104]
  ------------------
  154|  2.49k|        schemaLen = strlen(schemas[schemaType]);
  155|  2.49k|        if(endpointUrl->length >= schemaLen &&
  ------------------
  |  Branch (155:12): [True: 2.03k, False: 457]
  ------------------
  156|  2.03k|           strncmp((char*)endpointUrl->data, schemas[schemaType], schemaLen) == 0)
  ------------------
  |  Branch (156:12): [True: 398, False: 1.64k]
  ------------------
  157|    398|            break;
  158|  2.49k|    }
  159|    502|    if(schemaType == UA_SCHEMAS_SIZE)
  ------------------
  |  |  137|    502|#define UA_SCHEMAS_SIZE 6
  ------------------
  |  Branch (159:8): [True: 104, False: 398]
  ------------------
  160|    104|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|    104|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  161|       |
  162|       |    /* Forward the current position until the first colon or slash */
  163|    398|    size_t start = strlen(schemas[schemaType]);
  164|    398|    size_t curr = start;
  165|    398|    UA_Boolean ipv6 = false;
  166|    398|    if(endpointUrl->length > curr && endpointUrl->data[curr] == '[') {
  ------------------
  |  Branch (166:8): [True: 393, False: 5]
  |  Branch (166:38): [True: 126, False: 267]
  ------------------
  167|       |        /* IPv6: opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path */
  168|  8.73M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (168:15): [True: 8.73M, False: 23]
  ------------------
  169|  8.73M|            if(endpointUrl->data[curr] == ']')
  ------------------
  |  Branch (169:16): [True: 103, False: 8.73M]
  ------------------
  170|    103|                break;
  171|  8.73M|        }
  172|    126|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (172:12): [True: 23, False: 103]
  ------------------
  173|     23|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     23|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  174|    103|        curr++;
  175|    103|        ipv6 = true;
  176|    272|    } else {
  177|       |        /* IPv4 or hostname: opc.tcp://something.something:1234/path */
  178|  1.05M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (178:15): [True: 1.05M, False: 53]
  ------------------
  179|  1.05M|            if(endpointUrl->data[curr] == ':' || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (179:16): [True: 168, False: 1.05M]
  |  Branch (179:50): [True: 51, False: 1.05M]
  ------------------
  180|    219|                break;
  181|  1.05M|        }
  182|    272|    }
  183|       |
  184|       |    /* Set the hostname */
  185|    375|    if(ipv6) {
  ------------------
  |  Branch (185:8): [True: 103, False: 272]
  ------------------
  186|       |        /* Skip the ipv6 '[]' container for getaddrinfo() later */
  187|    103|        outHostname->data = &endpointUrl->data[start+1];
  188|    103|        outHostname->length = curr - (start+2);
  189|    272|    } else {
  190|    272|        outHostname->data = &endpointUrl->data[start];
  191|    272|        outHostname->length = curr - start;
  192|    272|    }
  193|       |
  194|       |    /* Empty string? */
  195|    375|    if(outHostname->length == 0)
  ------------------
  |  Branch (195:8): [True: 206, False: 169]
  ------------------
  196|    206|        outHostname->data = NULL;
  197|       |
  198|       |    /* Already at the end */
  199|    375|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (199:8): [True: 92, False: 283]
  ------------------
  200|     92|        if(outHostname->length == 0)
  ------------------
  |  Branch (200:12): [True: 6, False: 86]
  ------------------
  201|      6|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      6|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  202|     86|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     86|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  203|     92|    }
  204|       |
  205|       |    /* Set the port - and for ETH set the VID.PCP postfix in the outpath string.
  206|       |     * We have to parse that externally. */
  207|    283|    if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (207:8): [True: 205, False: 78]
  ------------------
  208|    205|        if(++curr == endpointUrl->length)
  ------------------
  |  Branch (208:12): [True: 4, False: 201]
  ------------------
  209|      4|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      4|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  210|       |
  211|       |        /* ETH schema */
  212|    201|        if(schemaType == UA_ETH_SCHEMA_INDEX) {
  ------------------
  |  |  138|    201|#define UA_ETH_SCHEMA_INDEX 2
  ------------------
  |  Branch (212:12): [True: 1, False: 200]
  ------------------
  213|      1|            if(outPath != NULL) {
  ------------------
  |  Branch (213:16): [True: 1, False: 0]
  ------------------
  214|      1|                outPath->data = &endpointUrl->data[curr];
  215|      1|                outPath->length = endpointUrl->length - curr;
  216|      1|            }
  217|      1|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|      1|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  218|      1|        }
  219|       |
  220|    200|        u32 largeNum;
  221|    200|        size_t progress = UA_readNumber(&endpointUrl->data[curr],
  222|    200|                                        endpointUrl->length - curr, &largeNum);
  223|    200|        if(progress == 0 || largeNum > 65535)
  ------------------
  |  Branch (223:12): [True: 48, False: 152]
  |  Branch (223:29): [True: 44, False: 108]
  ------------------
  224|     92|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     92|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  225|       |        /* Test if the end of a valid port was reached */
  226|    108|        curr += progress;
  227|    108|        if(curr == endpointUrl->length || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (227:12): [True: 54, False: 54]
  |  Branch (227:43): [True: 18, False: 36]
  ------------------
  228|     72|            *outPort = (u16)largeNum;
  229|    108|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (229:12): [True: 54, False: 54]
  ------------------
  230|     54|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     54|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  231|    108|    }
  232|       |
  233|       |    /* Set the path */
  234|    132|    UA_assert(curr < endpointUrl->length);
  ------------------
  |  |  399|    132|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (234:5): [True: 132, False: 0]
  ------------------
  235|    132|    if(endpointUrl->data[curr] != '/')
  ------------------
  |  Branch (235:8): [True: 62, False: 70]
  ------------------
  236|     62|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     62|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  237|     70|    if(++curr == endpointUrl->length)
  ------------------
  |  Branch (237:8): [True: 5, False: 65]
  ------------------
  238|      5|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|      5|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  239|     65|    if(outPath != NULL) {
  ------------------
  |  Branch (239:8): [True: 65, False: 0]
  ------------------
  240|     65|        outPath->data = &endpointUrl->data[curr];
  241|     65|        outPath->length = endpointUrl->length - curr;
  242|       |
  243|       |        /* Remove trailing slash from the path */
  244|     65|        if(endpointUrl->data[endpointUrl->length - 1] == '/')
  ------------------
  |  Branch (244:12): [True: 14, False: 51]
  ------------------
  245|     14|            outPath->length--;
  246|       |
  247|       |        /* Empty string? */
  248|     65|        if(outPath->length == 0)
  ------------------
  |  Branch (248:12): [True: 14, False: 51]
  ------------------
  249|     14|            outPath->data = NULL;
  250|     65|    }
  251|       |
  252|     65|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     65|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  253|     70|}
UA_parseEndpointUrlEthernet:
  257|    380|                            UA_UInt16 *vid, UA_Byte *pcp) {
  258|       |    /* Url must begin with "opc.eth://" */
  259|    380|    if(endpointUrl->length < 11) {
  ------------------
  |  Branch (259:8): [True: 7, False: 373]
  ------------------
  260|      7|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|      7|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  261|      7|    }
  262|    373|    if(strncmp((char*) endpointUrl->data, "opc.eth://", 10) != 0) {
  ------------------
  |  Branch (262:8): [True: 93, False: 280]
  ------------------
  263|     93|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     93|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  264|     93|    }
  265|       |
  266|       |    /* Where does the host address end? */
  267|    280|    size_t curr = 10;
  268|  1.11M|    for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (268:11): [True: 1.11M, False: 19]
  ------------------
  269|  1.11M|        if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (269:12): [True: 261, False: 1.11M]
  ------------------
  270|    261|           break;
  271|    261|        }
  272|  1.11M|    }
  273|       |
  274|       |    /* set host address */
  275|    280|    target->data = &endpointUrl->data[10];
  276|    280|    target->length = curr - 10;
  277|    280|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (277:8): [True: 19, False: 261]
  ------------------
  278|     19|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     19|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  279|     19|    }
  280|       |
  281|       |    /* Set VLAN */
  282|    261|    u32 value = 0;
  283|    261|    curr++;  /* skip ':' */
  284|    261|    size_t progress = UA_readNumber(&endpointUrl->data[curr],
  285|    261|                                    endpointUrl->length - curr, &value);
  286|    261|    if(progress == 0 || value > 4096) {
  ------------------
  |  Branch (286:8): [True: 29, False: 232]
  |  Branch (286:25): [True: 49, False: 183]
  ------------------
  287|     78|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     78|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  288|     78|    }
  289|    183|    curr += progress;
  290|    183|    if(curr == endpointUrl->length || endpointUrl->data[curr] == '.') {
  ------------------
  |  Branch (290:8): [True: 37, False: 146]
  |  Branch (290:39): [True: 111, False: 35]
  ------------------
  291|    148|        *vid = (UA_UInt16) value;
  292|    148|    }
  293|    183|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (293:8): [True: 37, False: 146]
  ------------------
  294|     37|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     37|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  295|     37|    }
  296|       |
  297|       |    /* Set priority */
  298|    146|    if(endpointUrl->data[curr] != '.') {
  ------------------
  |  Branch (298:8): [True: 35, False: 111]
  ------------------
  299|     35|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     35|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  300|     35|    }
  301|    111|    curr++;  /* skip '.' */
  302|    111|    progress = UA_readNumber(&endpointUrl->data[curr],
  303|    111|                             endpointUrl->length - curr, &value);
  304|    111|    if(progress == 0 || value > 7) {
  ------------------
  |  Branch (304:8): [True: 6, False: 105]
  |  Branch (304:25): [True: 65, False: 40]
  ------------------
  305|     71|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     71|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  306|     71|    }
  307|     40|    curr += progress;
  308|     40|    if(curr != endpointUrl->length) {
  ------------------
  |  Branch (308:8): [True: 29, False: 11]
  ------------------
  309|     29|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     29|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  310|     29|    }
  311|     11|    *pcp = (UA_Byte) value;
  312|       |
  313|     11|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     11|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  314|     40|}

UA_memoryManager_setLimitFromLast4Bytes:
  161|    887|int UA_memoryManager_setLimitFromLast4Bytes(const uint8_t *data, size_t size) {
  162|    887|    UA_mallocSingleton = UA_memoryManager_malloc;
  163|    887|    UA_freeSingleton = UA_memoryManager_free;
  164|    887|    UA_callocSingleton = UA_memoryManager_calloc;
  165|    887|    UA_reallocSingleton = UA_memoryManager_realloc;
  166|    887|    if(size <4)
  ------------------
  |  Branch (166:8): [True: 2, False: 885]
  ------------------
  167|      2|        return 0;
  168|       |    // just cast the last 4 bytes to uint32
  169|    885|    uint32_t limit;
  170|    885|    memcpy(&limit, &data[size-4], sizeof(uint32_t));
  171|    885|    memoryLimit = limit;
  172|    885|    return 1;
  173|    887|}

LLVMFuzzerTestOneInput:
   42|    887|extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   43|       |
   44|    887|    if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  ------------------
  |  Branch (44:9): [True: 2, False: 885]
  ------------------
   45|      2|        return 0;
   46|    885|    size -= 4;
   47|       |
   48|    885|    if (size == 0)
  ------------------
  |  Branch (48:9): [True: 1, False: 884]
  ------------------
   49|      1|        return 0;
   50|       |
   51|       |    // use first byte to decide which function should be fuzzed
   52|       |
   53|    884|    const uint8_t select = data[0];
   54|       |
   55|    884|    const uint8_t *newData = &data[1];
   56|    884|    size_t  newSize = size-1;
   57|       |
   58|    884|    switch(select) {
   59|    502|        case 0:
  ------------------
  |  Branch (59:9): [True: 502, False: 382]
  ------------------
   60|    502|            return tortureParseEndpointUrl(newData, newSize);
   61|    380|        case 1:
  ------------------
  |  Branch (61:9): [True: 380, False: 504]
  ------------------
   62|    380|            return tortureParseEndpointUrlEthernet(newData, newSize);
   63|      2|        default:
  ------------------
  |  Branch (63:9): [True: 2, False: 882]
  ------------------
   64|      2|            return 0;
   65|    884|    }
   66|       |
   67|    884|}
fuzz_src_ua_util.cc:_ZL23tortureParseEndpointUrlPKhm:
   14|    502|static int tortureParseEndpointUrl(const uint8_t *data, size_t size) {
   15|    502|    const UA_String endpointUrl = {
   16|    502|        size, (UA_Byte* )(void*)data
   17|    502|    };
   18|       |
   19|    502|    UA_String hostname;
   20|    502|    UA_UInt16 port;
   21|    502|    UA_String path;
   22|    502|    UA_parseEndpointUrl(&endpointUrl, &hostname, &port, &path);
   23|    502|    return 0;
   24|    502|}
fuzz_src_ua_util.cc:_ZL31tortureParseEndpointUrlEthernetPKhm:
   26|    380|static int tortureParseEndpointUrlEthernet(const uint8_t *data, size_t size) {
   27|    380|    const UA_String endpointUrl = {
   28|    380|        size, (UA_Byte* )(void*)data
   29|    380|    };
   30|       |
   31|    380|    UA_String target;
   32|    380|    UA_UInt16 vid;
   33|    380|    UA_Byte prid;
   34|    380|    UA_parseEndpointUrlEthernet(&endpointUrl, &target, &vid, &prid);
   35|    380|    return 0;
   36|    380|}

