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

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|    505|        case 0:
  ------------------
  |  Branch (59:9): [True: 505, False: 379]
  ------------------
   60|    505|            return tortureParseEndpointUrl(newData, newSize);
   61|    377|        case 1:
  ------------------
  |  Branch (61:9): [True: 377, False: 507]
  ------------------
   62|    377|            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|    505|static int tortureParseEndpointUrl(const uint8_t *data, size_t size) {
   15|    505|    const UA_String endpointUrl = {
   16|    505|        size, (UA_Byte* )(void*)data
   17|    505|    };
   18|       |
   19|    505|    UA_String hostname;
   20|    505|    UA_UInt16 port;
   21|    505|    UA_String path;
   22|    505|    UA_parseEndpointUrl(&endpointUrl, &hostname, &port, &path);
   23|    505|    return 0;
   24|    505|}
fuzz_src_ua_util.cc:_ZL31tortureParseEndpointUrlEthernetPKhm:
   26|    377|static int tortureParseEndpointUrlEthernet(const uint8_t *data, size_t size) {
   27|    377|    const UA_String endpointUrl = {
   28|    377|        size, (UA_Byte* )(void*)data
   29|    377|    };
   30|       |
   31|    377|    UA_String target;
   32|    377|    UA_UInt16 vid;
   33|    377|    UA_Byte prid;
   34|    377|    UA_parseEndpointUrlEthernet(&endpointUrl, &target, &vid, &prid);
   35|    377|    return 0;
   36|    377|}

