UA_readNumberWithBase:
  111|    557|UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base) {
  112|    557|    UA_assert(buf);
  ------------------
  |  |  399|    557|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (112:5): [True: 557, False: 0]
  ------------------
  113|    557|    UA_assert(number);
  ------------------
  |  |  399|    557|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (113:5): [True: 557, False: 0]
  ------------------
  114|    557|    u32 n = 0;
  115|    557|    size_t progress = 0;
  116|       |    /* read numbers until the end or a non-number character appears */
  117|  6.99M|    while(progress < buflen) {
  ------------------
  |  Branch (117:11): [True: 6.99M, False: 262]
  ------------------
  118|  6.99M|        u8 c = buf[progress];
  119|  6.99M|        if(c >= '0' && c <= '9' && c <= '0' + (base-1))
  ------------------
  |  Branch (119:12): [True: 6.99M, False: 206]
  |  Branch (119:24): [True: 6.99M, False: 89]
  |  Branch (119:36): [True: 6.99M, False: 0]
  ------------------
  120|  6.99M|           n = (n * base) + c - '0';
  121|    295|        else if(base > 9 && c >= 'a' && c <= 'z' && c <= 'a' + (base-11))
  ------------------
  |  Branch (121:17): [True: 295, False: 0]
  |  Branch (121:29): [True: 58, False: 237]
  |  Branch (121:41): [True: 19, False: 39]
  |  Branch (121:53): [True: 0, False: 19]
  ------------------
  122|      0|           n = (n * base) + c-'a' + 10;
  123|    295|        else if(base > 9 && c >= 'A' && c <= 'Z' && c <= 'A' + (base-11))
  ------------------
  |  Branch (123:17): [True: 295, False: 0]
  |  Branch (123:29): [True: 70, False: 225]
  |  Branch (123:41): [True: 7, False: 63]
  |  Branch (123:53): [True: 0, False: 7]
  ------------------
  124|      0|           n = (n * base) + c-'A' + 10;
  125|    295|        else
  126|    295|           break;
  127|  6.99M|        ++progress;
  128|  6.99M|    }
  129|    557|    *number = n;
  130|    557|    return progress;
  131|    557|}
UA_readNumber:
  134|    557|UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number) {
  135|    557|    return UA_readNumberWithBase(buf, buflen, number, 10);
  136|    557|}
UA_parseEndpointUrl:
  148|    503|                    UA_UInt16 *outPort, UA_String *outPath) {
  149|    503|    if(!endpointUrl || !endpointUrl->data)
  ------------------
  |  Branch (149:8): [True: 0, False: 503]
  |  Branch (149:24): [True: 0, False: 503]
  ------------------
  150|      0|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      0|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  151|       |
  152|       |    /* Which type of schema is this? */
  153|    503|    unsigned schemaType = 0;
  154|    503|    size_t schemaLen = 0;
  155|  3.06k|    for(; schemaType < UA_SCHEMAS_SIZE; schemaType++) {
  ------------------
  |  |  138|  3.06k|#define UA_SCHEMAS_SIZE 8
  ------------------
  |  Branch (155:11): [True: 2.95k, False: 113]
  ------------------
  156|  2.95k|        schemaLen = strlen(schemas[schemaType]);
  157|  2.95k|        if(endpointUrl->length >= schemaLen &&
  ------------------
  |  Branch (157:12): [True: 2.31k, False: 642]
  ------------------
  158|  2.31k|           strncmp((char*)endpointUrl->data, schemas[schemaType], schemaLen) == 0)
  ------------------
  |  Branch (158:12): [True: 390, False: 1.92k]
  ------------------
  159|    390|            break;
  160|  2.95k|    }
  161|    503|    if(schemaType == UA_SCHEMAS_SIZE)
  ------------------
  |  |  138|    503|#define UA_SCHEMAS_SIZE 8
  ------------------
  |  Branch (161:8): [True: 113, False: 390]
  ------------------
  162|    113|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|    113|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  163|       |
  164|       |    /* Forward the current position until the first colon or slash */
  165|    390|    size_t start = strlen(schemas[schemaType]);
  166|    390|    size_t curr = start;
  167|    390|    UA_Boolean ipv6 = false;
  168|    390|    if(endpointUrl->length > curr && endpointUrl->data[curr] == '[') {
  ------------------
  |  Branch (168:8): [True: 383, False: 7]
  |  Branch (168:38): [True: 124, False: 259]
  ------------------
  169|       |        /* IPv6: opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path */
  170|  8.89M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (170:15): [True: 8.89M, False: 16]
  ------------------
  171|  8.89M|            if(endpointUrl->data[curr] == ']')
  ------------------
  |  Branch (171:16): [True: 108, False: 8.89M]
  ------------------
  172|    108|                break;
  173|  8.89M|        }
  174|    124|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (174:12): [True: 16, False: 108]
  ------------------
  175|     16|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     16|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  176|    108|        curr++;
  177|    108|        ipv6 = true;
  178|    266|    } else {
  179|       |        /* IPv4 or hostname: opc.tcp://something.something:1234/path */
  180|  1.29M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (180:15): [True: 1.29M, False: 53]
  ------------------
  181|  1.29M|            if(endpointUrl->data[curr] == ':' || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (181:16): [True: 164, False: 1.29M]
  |  Branch (181:50): [True: 49, False: 1.29M]
  ------------------
  182|    213|                break;
  183|  1.29M|        }
  184|    266|    }
  185|       |
  186|       |    /* Set the hostname */
  187|    374|    if(ipv6) {
  ------------------
  |  Branch (187:8): [True: 108, False: 266]
  ------------------
  188|       |        /* Skip the ipv6 '[]' container for getaddrinfo() later */
  189|    108|        outHostname->data = &endpointUrl->data[start+1];
  190|    108|        outHostname->length = curr - (start+2);
  191|    266|    } else {
  192|    266|        outHostname->data = &endpointUrl->data[start];
  193|    266|        outHostname->length = curr - start;
  194|    266|    }
  195|       |
  196|       |    /* Empty string? */
  197|    374|    if(outHostname->length == 0)
  ------------------
  |  Branch (197:8): [True: 193, False: 181]
  ------------------
  198|    193|        outHostname->data = NULL;
  199|       |
  200|       |    /* Already at the end */
  201|    374|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (201:8): [True: 97, False: 277]
  ------------------
  202|     97|        if(outHostname->length == 0)
  ------------------
  |  Branch (202:12): [True: 8, False: 89]
  ------------------
  203|      8|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      8|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  204|     89|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     89|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  205|     97|    }
  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|    277|    if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (209:8): [True: 199, False: 78]
  ------------------
  210|    199|        if(++curr == endpointUrl->length)
  ------------------
  |  Branch (210:12): [True: 1, False: 198]
  ------------------
  211|      1|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      1|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  212|       |
  213|       |        /* ETH schema */
  214|    198|        if(schemaType == UA_ETH_SCHEMA_INDEX) {
  ------------------
  |  |  139|    198|#define UA_ETH_SCHEMA_INDEX 2
  ------------------
  |  Branch (214:12): [True: 1, False: 197]
  ------------------
  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|    197|        u32 largeNum;
  223|    197|        size_t progress = UA_readNumber(&endpointUrl->data[curr],
  224|    197|                                        endpointUrl->length - curr, &largeNum);
  225|    197|        if(progress == 0 || largeNum > 65535)
  ------------------
  |  Branch (225:12): [True: 52, False: 145]
  |  Branch (225:29): [True: 42, False: 103]
  ------------------
  226|     94|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     94|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  227|       |        /* Test if the end of a valid port was reached */
  228|    103|        curr += progress;
  229|    103|        if(curr == endpointUrl->length || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (229:12): [True: 51, False: 52]
  |  Branch (229:43): [True: 16, False: 36]
  ------------------
  230|     67|            *outPort = (u16)largeNum;
  231|    103|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (231:12): [True: 51, False: 52]
  ------------------
  232|     51|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     51|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  233|    103|    }
  234|       |
  235|       |    /* Set the path */
  236|    130|    UA_assert(curr < endpointUrl->length);
  ------------------
  |  |  399|    130|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (236:5): [True: 130, False: 0]
  ------------------
  237|    130|    if(endpointUrl->data[curr] != '/')
  ------------------
  |  Branch (237:8): [True: 64, False: 66]
  ------------------
  238|     64|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     64|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  239|     66|    if(++curr == endpointUrl->length)
  ------------------
  |  Branch (239:8): [True: 5, False: 61]
  ------------------
  240|      5|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|      5|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  241|     61|    if(outPath != NULL) {
  ------------------
  |  Branch (241:8): [True: 61, False: 0]
  ------------------
  242|     61|        outPath->data = &endpointUrl->data[curr];
  243|     61|        outPath->length = endpointUrl->length - curr;
  244|       |
  245|       |        /* Remove trailing slash from the path */
  246|     61|        if(endpointUrl->data[endpointUrl->length - 1] == '/')
  ------------------
  |  Branch (246:12): [True: 14, False: 47]
  ------------------
  247|     14|            outPath->length--;
  248|       |
  249|       |        /* Empty string? */
  250|     61|        if(outPath->length == 0)
  ------------------
  |  Branch (250:12): [True: 14, False: 47]
  ------------------
  251|     14|            outPath->data = NULL;
  252|     61|    }
  253|       |
  254|     61|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     61|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  255|     66|}
UA_parseEndpointUrlEthernet:
  259|    369|                            UA_UInt16 *vid, UA_Byte *pcp) {
  260|       |    /* Url must begin with "opc.eth://" */
  261|    369|    if(endpointUrl->length < 11) {
  ------------------
  |  Branch (261:8): [True: 8, False: 361]
  ------------------
  262|      8|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|      8|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  263|      8|    }
  264|    361|    if(strncmp((char*) endpointUrl->data, "opc.eth://", 10) != 0) {
  ------------------
  |  Branch (264:8): [True: 92, False: 269]
  ------------------
  265|     92|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     92|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  266|     92|    }
  267|       |
  268|       |    /* Where does the host address end? */
  269|    269|    size_t curr = 10;
  270|  1.11M|    for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (270:11): [True: 1.11M, False: 16]
  ------------------
  271|  1.11M|        if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (271:12): [True: 253, False: 1.11M]
  ------------------
  272|    253|           break;
  273|    253|        }
  274|  1.11M|    }
  275|       |
  276|       |    /* set host address */
  277|    269|    target->data = &endpointUrl->data[10];
  278|    269|    target->length = curr - 10;
  279|    269|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (279:8): [True: 16, False: 253]
  ------------------
  280|     16|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     16|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  281|     16|    }
  282|       |
  283|       |    /* Set VLAN */
  284|    253|    u32 value = 0;
  285|    253|    curr++;  /* skip ':' */
  286|    253|    size_t progress = UA_readNumber(&endpointUrl->data[curr],
  287|    253|                                    endpointUrl->length - curr, &value);
  288|    253|    if(progress == 0 || value > 4096) {
  ------------------
  |  Branch (288:8): [True: 27, False: 226]
  |  Branch (288:25): [True: 53, False: 173]
  ------------------
  289|     80|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     80|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  290|     80|    }
  291|    173|    curr += progress;
  292|    173|    if(curr == endpointUrl->length || endpointUrl->data[curr] == '.') {
  ------------------
  |  Branch (292:8): [True: 35, False: 138]
  |  Branch (292:39): [True: 107, False: 31]
  ------------------
  293|    142|        *vid = (UA_UInt16) value;
  294|    142|    }
  295|    173|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (295:8): [True: 35, False: 138]
  ------------------
  296|     35|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     35|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  297|     35|    }
  298|       |
  299|       |    /* Set priority */
  300|    138|    if(endpointUrl->data[curr] != '.') {
  ------------------
  |  Branch (300:8): [True: 31, False: 107]
  ------------------
  301|     31|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     31|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  302|     31|    }
  303|    107|    curr++;  /* skip '.' */
  304|    107|    progress = UA_readNumber(&endpointUrl->data[curr],
  305|    107|                             endpointUrl->length - curr, &value);
  306|    107|    if(progress == 0 || value > 7) {
  ------------------
  |  Branch (306:8): [True: 5, False: 102]
  |  Branch (306:25): [True: 60, False: 42]
  ------------------
  307|     65|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     65|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  308|     65|    }
  309|     42|    curr += progress;
  310|     42|    if(curr != endpointUrl->length) {
  ------------------
  |  Branch (310:8): [True: 29, False: 13]
  ------------------
  311|     29|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     29|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  312|     29|    }
  313|     13|    *pcp = (UA_Byte) value;
  314|       |
  315|     13|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     13|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  316|     42|}

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

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

