UA_readNumberWithBase:
  110|    588|UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base) {
  111|    588|    UA_assert(buf);
  ------------------
  |  |  395|    588|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (111:5): [True: 588, False: 0]
  ------------------
  112|    588|    UA_assert(number);
  ------------------
  |  |  395|    588|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (112:5): [True: 588, False: 0]
  ------------------
  113|    588|    u32 n = 0;
  114|    588|    size_t progress = 0;
  115|       |    /* read numbers until the end or a non-number character appears */
  116|  7.90M|    while(progress < buflen) {
  ------------------
  |  Branch (116:11): [True: 7.90M, False: 280]
  ------------------
  117|  7.90M|        u8 c = buf[progress];
  118|  7.90M|        if(c >= '0' && c <= '9' && c <= '0' + (base-1))
  ------------------
  |  Branch (118:12): [True: 7.90M, False: 202]
  |  Branch (118:24): [True: 7.90M, False: 106]
  |  Branch (118:36): [True: 7.90M, False: 0]
  ------------------
  119|  7.90M|           n = (n * base) + c - '0';
  120|    308|        else if(base > 9 && c >= 'a' && c <= 'z' && c <= 'a' + (base-11))
  ------------------
  |  Branch (120:17): [True: 308, False: 0]
  |  Branch (120:29): [True: 68, False: 240]
  |  Branch (120:41): [True: 19, False: 49]
  |  Branch (120:53): [True: 0, False: 19]
  ------------------
  121|      0|           n = (n * base) + c-'a' + 10;
  122|    308|        else if(base > 9 && c >= 'A' && c <= 'Z' && c <= 'A' + (base-11))
  ------------------
  |  Branch (122:17): [True: 308, False: 0]
  |  Branch (122:29): [True: 84, False: 224]
  |  Branch (122:41): [True: 8, False: 76]
  |  Branch (122:53): [True: 0, False: 8]
  ------------------
  123|      0|           n = (n * base) + c-'A' + 10;
  124|    308|        else
  125|    308|           break;
  126|  7.90M|        ++progress;
  127|  7.90M|    }
  128|    588|    *number = n;
  129|    588|    return progress;
  130|    588|}
UA_readNumber:
  133|    588|UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number) {
  134|    588|    return UA_readNumberWithBase(buf, buflen, number, 10);
  135|    588|}
UA_parseEndpointUrl:
  146|    512|                    UA_UInt16 *outPort, UA_String *outPath) {
  147|       |    /* Url must begin with "opc.tcp://" or opc.udp:// (if pubsub enabled) */
  148|    512|    if(endpointUrl->length < 11) {
  ------------------
  |  Branch (148:8): [True: 6, False: 506]
  ------------------
  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|    506|    unsigned schemaType = 0;
  154|  1.33k|    for(; schemaType < UA_SCHEMAS_SIZE; schemaType++) {
  ------------------
  |  |  137|  1.33k|#define UA_SCHEMAS_SIZE 4
  ------------------
  |  Branch (154:11): [True: 1.24k, False: 95]
  ------------------
  155|  1.24k|        if(strncmp((char*)endpointUrl->data,
  ------------------
  |  Branch (155:12): [True: 411, False: 830]
  ------------------
  156|  1.24k|                   schemas[schemaType],
  157|  1.24k|                   strlen(schemas[schemaType])) == 0)
  158|    411|            break;
  159|  1.24k|    }
  160|    506|    if(schemaType == UA_SCHEMAS_SIZE)
  ------------------
  |  |  137|    506|#define UA_SCHEMAS_SIZE 4
  ------------------
  |  Branch (160:8): [True: 95, False: 411]
  ------------------
  161|     95|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  499|     95|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  162|       |
  163|       |    /* Forward the current position until the first colon or slash */
  164|    411|    size_t start = strlen(schemas[schemaType]);
  165|    411|    size_t curr = start;
  166|    411|    UA_Boolean ipv6 = false;
  167|    411|    if(endpointUrl->length > curr && endpointUrl->data[curr] == '[') {
  ------------------
  |  Branch (167:8): [True: 410, False: 1]
  |  Branch (167:38): [True: 124, False: 286]
  ------------------
  168|       |        /* IPv6: opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path */
  169|  8.82M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (169:15): [True: 8.82M, False: 17]
  ------------------
  170|  8.82M|            if(endpointUrl->data[curr] == ']')
  ------------------
  |  Branch (170:16): [True: 107, False: 8.82M]
  ------------------
  171|    107|                break;
  172|  8.82M|        }
  173|    124|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (173:12): [True: 17, False: 107]
  ------------------
  174|     17|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  499|     17|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  175|    107|        curr++;
  176|    107|        ipv6 = true;
  177|    287|    } else {
  178|       |        /* IPv4 or hostname: opc.tcp://something.something:1234/path */
  179|  1.05M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (179:15): [True: 1.05M, False: 52]
  ------------------
  180|  1.05M|            if(endpointUrl->data[curr] == ':' || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (180:16): [True: 179, False: 1.05M]
  |  Branch (180:50): [True: 56, False: 1.05M]
  ------------------
  181|    235|                break;
  182|  1.05M|        }
  183|    287|    }
  184|       |
  185|       |    /* Set the hostname */
  186|    394|    if(ipv6) {
  ------------------
  |  Branch (186:8): [True: 107, False: 287]
  ------------------
  187|       |        /* Skip the ipv6 '[]' container for getaddrinfo() later */
  188|    107|        outHostname->data = &endpointUrl->data[start+1];
  189|    107|        outHostname->length = curr - (start+2);
  190|    287|    } else {
  191|    287|        outHostname->data = &endpointUrl->data[start];
  192|    287|        outHostname->length = curr - start;
  193|    287|    }
  194|       |
  195|       |    /* Empty string? */
  196|    394|    if(outHostname->length == 0)
  ------------------
  |  Branch (196:8): [True: 218, False: 176]
  ------------------
  197|    218|        outHostname->data = NULL;
  198|       |
  199|       |    /* Already at the end */
  200|    394|    if(curr == endpointUrl->length)
  ------------------
  |  Branch (200:8): [True: 94, False: 300]
  ------------------
  201|     94|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|     94|#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|    300|    if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (205:8): [True: 211, False: 89]
  ------------------
  206|    211|        if(++curr == endpointUrl->length)
  ------------------
  |  Branch (206:12): [True: 3, False: 208]
  ------------------
  207|      3|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  499|      3|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  208|       |
  209|       |        /* ETH schema */
  210|    208|        if(schemaType == UA_ETH_SCHEMA_INDEX) {
  ------------------
  |  |  138|    208|#define UA_ETH_SCHEMA_INDEX 2
  ------------------
  |  Branch (210:12): [True: 2, False: 206]
  ------------------
  211|      2|            if(outPath != NULL) {
  ------------------
  |  Branch (211:16): [True: 2, False: 0]
  ------------------
  212|      2|                outPath->data = &endpointUrl->data[curr];
  213|      2|                outPath->length = endpointUrl->length - curr;
  214|      2|            }
  215|      2|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|      2|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  216|      2|        }
  217|       |
  218|    206|        u32 largeNum;
  219|    206|        size_t progress = UA_readNumber(&endpointUrl->data[curr],
  220|    206|                                        endpointUrl->length - curr, &largeNum);
  221|    206|        if(progress == 0 || largeNum > 65535)
  ------------------
  |  Branch (221:12): [True: 49, False: 157]
  |  Branch (221:29): [True: 44, False: 113]
  ------------------
  222|     93|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  499|     93|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  223|       |        /* Test if the end of a valid port was reached */
  224|    113|        curr += progress;
  225|    113|        if(curr == endpointUrl->length || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (225:12): [True: 59, False: 54]
  |  Branch (225:43): [True: 12, False: 42]
  ------------------
  226|     71|            *outPort = (u16)largeNum;
  227|    113|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (227:12): [True: 59, False: 54]
  ------------------
  228|     59|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|     59|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  229|    113|    }
  230|       |
  231|       |    /* Set the path */
  232|    143|    UA_assert(curr < endpointUrl->length);
  ------------------
  |  |  395|    143|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (232:5): [True: 143, False: 0]
  ------------------
  233|    143|    if(endpointUrl->data[curr] != '/')
  ------------------
  |  Branch (233:8): [True: 73, False: 70]
  ------------------
  234|     73|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  499|     73|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  235|     70|    if(++curr == endpointUrl->length)
  ------------------
  |  Branch (235:8): [True: 5, False: 65]
  ------------------
  236|      5|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|      5|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  237|     65|    if(outPath != NULL) {
  ------------------
  |  Branch (237:8): [True: 65, False: 0]
  ------------------
  238|     65|        outPath->data = &endpointUrl->data[curr];
  239|     65|        outPath->length = endpointUrl->length - curr;
  240|       |
  241|       |        /* Remove trailing slash from the path */
  242|     65|        if(endpointUrl->data[endpointUrl->length - 1] == '/')
  ------------------
  |  Branch (242:12): [True: 8, False: 57]
  ------------------
  243|      8|            outPath->length--;
  244|       |
  245|       |        /* Empty string? */
  246|     65|        if(outPath->length == 0)
  ------------------
  |  Branch (246:12): [True: 8, False: 57]
  ------------------
  247|      8|            outPath->data = NULL;
  248|     65|    }
  249|       |
  250|     65|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|     65|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  251|     70|}
UA_parseEndpointUrlEthernet:
  255|    387|                            UA_UInt16 *vid, UA_Byte *pcp) {
  256|       |    /* Url must begin with "opc.eth://" */
  257|    387|    if(endpointUrl->length < 11) {
  ------------------
  |  Branch (257:8): [True: 7, False: 380]
  ------------------
  258|      7|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   28|      7|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  259|      7|    }
  260|    380|    if(strncmp((char*) endpointUrl->data, "opc.eth://", 10) != 0) {
  ------------------
  |  Branch (260:8): [True: 92, False: 288]
  ------------------
  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|    288|    size_t curr = 10;
  266|  1.10M|    for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (266:11): [True: 1.10M, False: 17]
  ------------------
  267|  1.10M|        if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (267:12): [True: 271, False: 1.10M]
  ------------------
  268|    271|           break;
  269|    271|        }
  270|  1.10M|    }
  271|       |
  272|       |    /* set host address */
  273|    288|    target->data = &endpointUrl->data[10];
  274|    288|    target->length = curr - 10;
  275|    288|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (275:8): [True: 17, False: 271]
  ------------------
  276|     17|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|     17|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  277|     17|    }
  278|       |
  279|       |    /* Set VLAN */
  280|    271|    u32 value = 0;
  281|    271|    curr++;  /* skip ':' */
  282|    271|    size_t progress = UA_readNumber(&endpointUrl->data[curr],
  283|    271|                                    endpointUrl->length - curr, &value);
  284|    271|    if(progress == 0 || value > 4096) {
  ------------------
  |  Branch (284:8): [True: 30, False: 241]
  |  Branch (284:25): [True: 55, False: 186]
  ------------------
  285|     85|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   28|     85|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  286|     85|    }
  287|    186|    curr += progress;
  288|    186|    if(curr == endpointUrl->length || endpointUrl->data[curr] == '.') {
  ------------------
  |  Branch (288:8): [True: 37, False: 149]
  |  Branch (288:39): [True: 111, False: 38]
  ------------------
  289|    148|        *vid = (UA_UInt16) value;
  290|    148|    }
  291|    186|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (291:8): [True: 37, False: 149]
  ------------------
  292|     37|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|     37|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  293|     37|    }
  294|       |
  295|       |    /* Set priority */
  296|    149|    if(endpointUrl->data[curr] != '.') {
  ------------------
  |  Branch (296:8): [True: 38, False: 111]
  ------------------
  297|     38|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   28|     38|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  298|     38|    }
  299|    111|    curr++;  /* skip '.' */
  300|    111|    progress = UA_readNumber(&endpointUrl->data[curr],
  301|    111|                             endpointUrl->length - curr, &value);
  302|    111|    if(progress == 0 || value > 7) {
  ------------------
  |  Branch (302:8): [True: 5, False: 106]
  |  Branch (302:25): [True: 67, False: 39]
  ------------------
  303|     72|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   28|     72|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  304|     72|    }
  305|     39|    curr += progress;
  306|     39|    if(curr != endpointUrl->length) {
  ------------------
  |  Branch (306:8): [True: 30, False: 9]
  ------------------
  307|     30|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   28|     30|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  308|     30|    }
  309|      9|    *pcp = (UA_Byte) value;
  310|       |
  311|      9|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   16|      9|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  312|     39|}

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

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

