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

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

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

