fuzz.c:recv_validate_datagram:
  579|    389|static bool recv_validate_datagram(const recv_ntb_t *ntb, uint32_t len) {
  580|    389|  const nth16_t *nth16 = &(ntb->nth);
  581|       |
  582|    389|  TU_LOG_DRV("recv_validate_datagram(%p, %d)\n", ntb, (int) len);
  583|       |
  584|       |  // check header
  585|    389|  if (nth16->wHeaderLength != sizeof(nth16_t)) {
  ------------------
  |  Branch (585:7): [True: 25, False: 364]
  ------------------
  586|     25|    TU_LOG_DRV("(EE) ill nth16 length: %d\n", nth16->wHeaderLength);
  587|     25|    return false;
  588|     25|  }
  589|    364|  if (nth16->dwSignature != NTH16_SIGNATURE) {
  ------------------
  |  |   83|    364|#define NTH16_SIGNATURE 0x484D434E
  ------------------
  |  Branch (589:7): [True: 51, False: 313]
  ------------------
  590|     51|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) nth16->dwSignature);
  591|     51|    return false;
  592|     51|  }
  593|    313|  if (len < sizeof(nth16_t) + sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (593:7): [True: 9, False: 304]
  ------------------
  594|      9|    TU_LOG_DRV("(EE) ill min len: %lu\n", len);
  595|      9|    return false;
  596|      9|  }
  597|    304|  if (nth16->wBlockLength > len) {
  ------------------
  |  Branch (597:7): [True: 17, False: 287]
  ------------------
  598|     17|    TU_LOG_DRV("(EE) ill block length: %d > %lu\n", nth16->wBlockLength, len);
  599|     17|    return false;
  600|     17|  }
  601|    287|  if (nth16->wBlockLength > CFG_TUD_NCM_OUT_NTB_MAX_SIZE) {
  ------------------
  |  |   17|    287|  #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
  ------------------
  |  Branch (601:7): [True: 0, False: 287]
  ------------------
  602|      0|    TU_LOG_DRV("(EE) ill block length2: %d > %d\n", nth16->wBlockLength, CFG_TUD_NCM_OUT_NTB_MAX_SIZE);
  603|      0|    return false;
  604|      0|  }
  605|    287|  if (nth16->wNdpIndex < sizeof(nth16_t) || nth16->wNdpIndex > len - (sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t))) {
  ------------------
  |  Branch (605:7): [True: 13, False: 274]
  |  Branch (605:45): [True: 22, False: 252]
  ------------------
  606|     35|    TU_LOG_DRV("(EE) ill position of first ndp: %d (%lu)\n", nth16->wNdpIndex, len);
  607|     35|    return false;
  608|     35|  }
  609|       |
  610|       |  // check (first) NDP(16)
  611|    252|  const ndp16_t *ndp16 = (const ndp16_t *) (ntb->data + nth16->wNdpIndex);
  612|       |
  613|    252|  if (ndp16->wLength < sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (613:7): [True: 11, False: 241]
  ------------------
  614|     11|    TU_LOG_DRV("(EE) ill ndp16 length: %d\n", ndp16->wLength);
  615|     11|    return false;
  616|     11|  }
  617|       |  // the NDP block (wLength bytes from wNdpIndex) must fit within the received NTB, otherwise the
  618|       |  // datagram pointer array walked below (max_ndx is derived from wLength) runs past ntb->data
  619|    241|  if ((uint32_t) nth16->wNdpIndex + ndp16->wLength > len) {
  ------------------
  |  Branch (619:7): [True: 39, False: 202]
  ------------------
  620|     39|    TU_LOG_DRV("(EE) ill ndp16 length: %d (%lu)\n", ndp16->wLength, len);
  621|     39|    return false;
  622|     39|  }
  623|    202|  if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   84|    404|#define NDP16_SIGNATURE_NCM0 0x304D434E
  ------------------
                if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   85|    133|#define NDP16_SIGNATURE_NCM1 0x314D434E
  ------------------
  |  Branch (623:7): [True: 133, False: 69]
  |  Branch (623:53): [True: 88, False: 45]
  ------------------
  624|     88|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) ndp16->dwSignature);
  625|     88|    return false;
  626|     88|  }
  627|    114|  if (ndp16->wNextNdpIndex != 0) {
  ------------------
  |  Branch (627:7): [True: 17, False: 97]
  ------------------
  628|     17|    TU_LOG_DRV("(EE) cannot handle wNextNdpIndex!=0 (%d)\n", ndp16->wNextNdpIndex);
  629|     17|    return false;
  630|     17|  }
  631|       |
  632|     97|  const ndp16_datagram_t *ndp16_datagram = (const ndp16_datagram_t *) (ntb->data + nth16->wNdpIndex + sizeof(ndp16_t));
  633|     97|  int ndx = 0;
  634|     97|  uint16_t max_ndx = (uint16_t) ((ndp16->wLength - sizeof(ndp16_t)) / sizeof(ndp16_datagram_t));
  635|       |
  636|     97|  if (max_ndx > 2) { // number of datagrams in NTB > 1
  ------------------
  |  Branch (636:7): [True: 22, False: 75]
  ------------------
  637|     22|    TU_LOG_DRV("<< %d (%d)\n", max_ndx - 1, ntb->nth.wBlockLength);
  638|     22|  }
  639|     97|  if (ndp16_datagram[max_ndx - 1].wDatagramIndex != 0 || ndp16_datagram[max_ndx - 1].wDatagramLength != 0) {
  ------------------
  |  Branch (639:7): [True: 19, False: 78]
  |  Branch (639:58): [True: 17, False: 61]
  ------------------
  640|     36|    TU_LOG_DRV("  max_ndx != 0\n");
  641|     36|    return false;
  642|     36|  }
  643|    375|  while (ndp16_datagram[ndx].wDatagramIndex != 0 && ndp16_datagram[ndx].wDatagramLength != 0) {
  ------------------
  |  Branch (643:10): [True: 361, False: 14]
  |  Branch (643:53): [True: 359, False: 2]
  ------------------
  644|    359|    TU_LOG_DRV("  << %d %d\n", ndp16_datagram[ndx].wDatagramIndex, ndp16_datagram[ndx].wDatagramLength);
  645|    359|    if (ndp16_datagram[ndx].wDatagramIndex > len) {
  ------------------
  |  Branch (645:9): [True: 25, False: 334]
  ------------------
  646|     25|      TU_LOG_DRV("(EE) ill start of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex, len);
  647|     25|      return false;
  648|     25|    }
  649|    334|    if (ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength > len) {
  ------------------
  |  Branch (649:9): [True: 20, False: 314]
  ------------------
  650|     20|      TU_LOG_DRV("(EE) ill end of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength, len);
  651|     20|      return false;
  652|     20|    }
  653|    314|    ++ndx;
  654|    314|  }
  655|       |
  656|       |  #if CFG_TUD_NCM_LOG_LEVEL >= 3
  657|       |  TU_LOG_BUF(3, ntb->data[i], len);
  658|       |  #endif
  659|       |
  660|       |  // -> ntb contains a valid packet structure
  661|       |  //    ok... I did not check for garbage within the datagram indices...
  662|     16|  return true;
  663|     61|} // recv_validate_datagram

LLVMFuzzerTestOneInput:
   77|    389|int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   78|       |  // Give the NTB a heap allocation of the exact receive-buffer size so any read
   79|       |  // past ntb->data is caught by AddressSanitizer.
   80|    389|  recv_ntb_t *ntb = (recv_ntb_t *) malloc(sizeof(recv_ntb_t));
   81|    389|  if (ntb == NULL) {
  ------------------
  |  Branch (81:7): [True: 0, False: 389]
  ------------------
   82|      0|    return 0;
   83|      0|  }
   84|    389|  size_t n = size < sizeof(ntb->data) ? size : sizeof(ntb->data);
  ------------------
  |  Branch (84:14): [True: 378, False: 11]
  ------------------
   85|    389|  memset(ntb->data, 0, sizeof(ntb->data));
   86|    389|  memcpy(ntb->data, data, n);
   87|    389|  recv_validate_datagram(ntb, (uint32_t) n);
   88|    389|  free(ntb);
   89|    389|  return 0;
   90|    389|}

