UA_readNumberWithBase:
  111|    579|UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base) {
  112|    579|    UA_assert(buf);
  ------------------
  |  |  400|    579|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (112:5): [True: 579, False: 0]
  ------------------
  113|    579|    UA_assert(number);
  ------------------
  |  |  400|    579|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (113:5): [True: 579, False: 0]
  ------------------
  114|    579|    u32 n = 0;
  115|    579|    size_t progress = 0;
  116|       |    /* read numbers until the end or a non-number character appears */
  117|  7.30M|    while(progress < buflen) {
  ------------------
  |  Branch (117:11): [True: 7.30M, False: 277]
  ------------------
  118|  7.30M|        u8 c = buf[progress];
  119|  7.30M|        if(c >= '0' && c <= '9' && c <= '0' + (base-1))
  ------------------
  |  Branch (119:12): [True: 7.30M, False: 211]
  |  Branch (119:24): [True: 7.30M, False: 91]
  |  Branch (119:36): [True: 7.30M, False: 0]
  ------------------
  120|  7.30M|           n = (n * base) + c - '0';
  121|    302|        else if(base > 9 && c >= 'a' && c <= 'z' && c <= 'a' + (base-11))
  ------------------
  |  Branch (121:17): [True: 302, False: 0]
  |  Branch (121:29): [True: 63, False: 239]
  |  Branch (121:41): [True: 21, False: 42]
  |  Branch (121:53): [True: 0, False: 21]
  ------------------
  122|      0|           n = (n * base) + c-'a' + 10;
  123|    302|        else if(base > 9 && c >= 'A' && c <= 'Z' && c <= 'A' + (base-11))
  ------------------
  |  Branch (123:17): [True: 302, False: 0]
  |  Branch (123:29): [True: 73, False: 229]
  |  Branch (123:41): [True: 6, False: 67]
  |  Branch (123:53): [True: 0, False: 6]
  ------------------
  124|      0|           n = (n * base) + c-'A' + 10;
  125|    302|        else
  126|    302|           break;
  127|  7.30M|        ++progress;
  128|  7.30M|    }
  129|    579|    *number = n;
  130|    579|    return progress;
  131|    579|}
UA_readNumber:
  134|    579|UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number) {
  135|    579|    return UA_readNumberWithBase(buf, buflen, number, 10);
  136|    579|}
UA_parseEndpointUrl:
  148|    519|                    UA_UInt16 *outPort, UA_String *outPath) {
  149|    519|    if(!endpointUrl || !endpointUrl->data)
  ------------------
  |  Branch (149:8): [True: 0, False: 519]
  |  Branch (149:24): [True: 0, False: 519]
  ------------------
  150|      0|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      0|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  151|       |
  152|       |    /* Which type of schema is this? */
  153|    519|    unsigned schemaType = 0;
  154|    519|    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: 115]
  ------------------
  156|  2.95k|        schemaLen = strlen(schemas[schemaType]);
  157|  2.95k|        if(endpointUrl->length >= schemaLen &&
  ------------------
  |  Branch (157:12): [True: 2.30k, False: 651]
  ------------------
  158|  2.30k|           strncmp((char*)endpointUrl->data, schemas[schemaType], schemaLen) == 0)
  ------------------
  |  Branch (158:12): [True: 404, False: 1.89k]
  ------------------
  159|    404|            break;
  160|  2.95k|    }
  161|    519|    if(schemaType == UA_SCHEMAS_SIZE)
  ------------------
  |  |  138|    519|#define UA_SCHEMAS_SIZE 8
  ------------------
  |  Branch (161:8): [True: 115, False: 404]
  ------------------
  162|    115|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|    115|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  163|       |
  164|       |    /* Forward the current position until the first colon or slash */
  165|    404|    size_t start = strlen(schemas[schemaType]);
  166|    404|    size_t curr = start;
  167|    404|    UA_Boolean ipv6 = false;
  168|    404|    if(endpointUrl->length > curr && endpointUrl->data[curr] == '[') {
  ------------------
  |  Branch (168:8): [True: 397, False: 7]
  |  Branch (168:38): [True: 127, False: 270]
  ------------------
  169|       |        /* IPv6: opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path */
  170|  9.36M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (170:15): [True: 9.36M, False: 19]
  ------------------
  171|  9.36M|            if(endpointUrl->data[curr] == ']')
  ------------------
  |  Branch (171:16): [True: 108, False: 9.36M]
  ------------------
  172|    108|                break;
  173|  9.36M|        }
  174|    127|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (174:12): [True: 19, False: 108]
  ------------------
  175|     19|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     19|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  176|    108|        curr++;
  177|    108|        ipv6 = true;
  178|    277|    } else {
  179|       |        /* IPv4 or hostname: opc.tcp://something.something:1234/path */
  180|  1.25M|        for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (180:15): [True: 1.25M, False: 57]
  ------------------
  181|  1.25M|            if(endpointUrl->data[curr] == ':' || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (181:16): [True: 174, False: 1.25M]
  |  Branch (181:50): [True: 46, False: 1.25M]
  ------------------
  182|    220|                break;
  183|  1.25M|        }
  184|    277|    }
  185|       |
  186|       |    /* Set the hostname */
  187|    385|    if(ipv6) {
  ------------------
  |  Branch (187:8): [True: 108, False: 277]
  ------------------
  188|       |        /* Skip the ipv6 '[]' container for getaddrinfo() later */
  189|    108|        outHostname->data = &endpointUrl->data[start+1];
  190|    108|        outHostname->length = curr - (start+2);
  191|    277|    } else {
  192|    277|        outHostname->data = &endpointUrl->data[start];
  193|    277|        outHostname->length = curr - start;
  194|    277|    }
  195|       |
  196|       |    /* Empty string? */
  197|    385|    if(outHostname->length == 0)
  ------------------
  |  Branch (197:8): [True: 209, False: 176]
  ------------------
  198|    209|        outHostname->data = NULL;
  199|       |
  200|       |    /* Already at the end */
  201|    385|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (201:8): [True: 105, False: 280]
  ------------------
  202|    105|        if(outHostname->length == 0)
  ------------------
  |  Branch (202:12): [True: 9, False: 96]
  ------------------
  203|      9|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      9|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  204|     96|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     96|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  205|    105|    }
  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|    280|    if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (209:8): [True: 205, False: 75]
  ------------------
  210|    205|        if(++curr == endpointUrl->length)
  ------------------
  |  Branch (210:12): [True: 2, False: 203]
  ------------------
  211|      2|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|      2|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  212|       |
  213|       |        /* ETH schema */
  214|    203|        if(schemaType == UA_ETH_SCHEMA_INDEX) {
  ------------------
  |  |  139|    203|#define UA_ETH_SCHEMA_INDEX 2
  ------------------
  |  Branch (214:12): [True: 1, False: 202]
  ------------------
  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|    202|        u32 largeNum;
  223|    202|        size_t progress = UA_readNumber(&endpointUrl->data[curr],
  224|    202|                                        endpointUrl->length - curr, &largeNum);
  225|    202|        if(progress == 0 || largeNum > 65535)
  ------------------
  |  Branch (225:12): [True: 49, False: 153]
  |  Branch (225:29): [True: 43, False: 110]
  ------------------
  226|     92|            return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     92|#define UA_STATUSCODE_BADTCPENDPOINTURLINVALID ((UA_StatusCode) 0x80830000)
  ------------------
  227|       |        /* Test if the end of a valid port was reached */
  228|    110|        curr += progress;
  229|    110|        if(curr == endpointUrl->length || endpointUrl->data[curr] == '/')
  ------------------
  |  Branch (229:12): [True: 59, False: 51]
  |  Branch (229:43): [True: 18, False: 33]
  ------------------
  230|     77|            *outPort = (u16)largeNum;
  231|    110|        if(curr == endpointUrl->length)
  ------------------
  |  Branch (231:12): [True: 59, False: 51]
  ------------------
  232|     59|            return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     59|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  233|    110|    }
  234|       |
  235|       |    /* Set the path */
  236|    126|    UA_assert(curr < endpointUrl->length);
  ------------------
  |  |  400|    126|# define UA_assert(ignore) assert(ignore)
  ------------------
  |  Branch (236:5): [True: 126, False: 0]
  ------------------
  237|    126|    if(endpointUrl->data[curr] != '/')
  ------------------
  |  Branch (237:8): [True: 60, False: 66]
  ------------------
  238|     60|        return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  ------------------
  |  |  500|     60|#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: 12, False: 49]
  ------------------
  247|     12|            outPath->length--;
  248|       |
  249|       |        /* Empty string? */
  250|     61|        if(outPath->length == 0)
  ------------------
  |  Branch (250:12): [True: 12, False: 49]
  ------------------
  251|     12|            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|    384|                            UA_UInt16 *vid, UA_Byte *pcp) {
  260|       |    /* Url must begin with "opc.eth://" */
  261|    384|    if(endpointUrl->length < 11) {
  ------------------
  |  Branch (261:8): [True: 6, False: 378]
  ------------------
  262|      6|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|      6|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  263|      6|    }
  264|    378|    if(strncmp((char*) endpointUrl->data, "opc.eth://", 10) != 0) {
  ------------------
  |  Branch (264:8): [True: 93, False: 285]
  ------------------
  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|    285|    size_t curr = 10;
  270|  1.12M|    for(; curr < endpointUrl->length; ++curr) {
  ------------------
  |  Branch (270:11): [True: 1.12M, False: 16]
  ------------------
  271|  1.12M|        if(endpointUrl->data[curr] == ':') {
  ------------------
  |  Branch (271:12): [True: 269, False: 1.11M]
  ------------------
  272|    269|           break;
  273|    269|        }
  274|  1.12M|    }
  275|       |
  276|       |    /* set host address */
  277|    285|    target->data = &endpointUrl->data[10];
  278|    285|    target->length = curr - 10;
  279|    285|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (279:8): [True: 16, False: 269]
  ------------------
  280|     16|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     16|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  281|     16|    }
  282|       |
  283|       |    /* Set VLAN */
  284|    269|    u32 value = 0;
  285|    269|    curr++;  /* skip ':' */
  286|    269|    size_t progress = UA_readNumber(&endpointUrl->data[curr],
  287|    269|                                    endpointUrl->length - curr, &value);
  288|    269|    if(progress == 0 || value > 4096) {
  ------------------
  |  Branch (288:8): [True: 32, False: 237]
  |  Branch (288:25): [True: 55, False: 182]
  ------------------
  289|     87|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     87|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  290|     87|    }
  291|    182|    curr += progress;
  292|    182|    if(curr == endpointUrl->length || endpointUrl->data[curr] == '.') {
  ------------------
  |  Branch (292:8): [True: 38, False: 144]
  |  Branch (292:39): [True: 108, False: 36]
  ------------------
  293|    146|        *vid = (UA_UInt16) value;
  294|    146|    }
  295|    182|    if(curr == endpointUrl->length) {
  ------------------
  |  Branch (295:8): [True: 38, False: 144]
  ------------------
  296|     38|        return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     38|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  297|     38|    }
  298|       |
  299|       |    /* Set priority */
  300|    144|    if(endpointUrl->data[curr] != '.') {
  ------------------
  |  Branch (300:8): [True: 36, False: 108]
  ------------------
  301|     36|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     36|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  302|     36|    }
  303|    108|    curr++;  /* skip '.' */
  304|    108|    progress = UA_readNumber(&endpointUrl->data[curr],
  305|    108|                             endpointUrl->length - curr, &value);
  306|    108|    if(progress == 0 || value > 7) {
  ------------------
  |  Branch (306:8): [True: 6, False: 102]
  |  Branch (306:25): [True: 59, False: 43]
  ------------------
  307|     65|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     65|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  308|     65|    }
  309|     43|    curr += progress;
  310|     43|    if(curr != endpointUrl->length) {
  ------------------
  |  Branch (310:8): [True: 31, False: 12]
  ------------------
  311|     31|        return UA_STATUSCODE_BADINTERNALERROR;
  ------------------
  |  |   29|     31|#define UA_STATUSCODE_BADINTERNALERROR ((UA_StatusCode) 0x80020000)
  ------------------
  312|     31|    }
  313|     12|    *pcp = (UA_Byte) value;
  314|       |
  315|     12|    return UA_STATUSCODE_GOOD;
  ------------------
  |  |   17|     12|#define UA_STATUSCODE_GOOD ((UA_StatusCode) 0x00000000)
  ------------------
  316|     43|}

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

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

