fuzz.c:recv_validate_datagram:
  579|    384|static bool recv_validate_datagram(const recv_ntb_t *ntb, uint32_t len) {
  580|    384|  const nth16_t *nth16 = &(ntb->nth);
  581|       |
  582|    384|  TU_LOG_DRV("recv_validate_datagram(%p, %d)\n", ntb, (int) len);
  583|       |
  584|       |  // check header
  585|    384|  if (nth16->wHeaderLength != sizeof(nth16_t)) {
  ------------------
  |  Branch (585:7): [True: 22, False: 362]
  ------------------
  586|     22|    TU_LOG_DRV("(EE) ill nth16 length: %d\n", nth16->wHeaderLength);
  587|     22|    return false;
  588|     22|  }
  589|    362|  if (nth16->dwSignature != NTH16_SIGNATURE) {
  ------------------
  |  |   83|    362|#define NTH16_SIGNATURE 0x484D434E
  ------------------
  |  Branch (589:7): [True: 53, False: 309]
  ------------------
  590|     53|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) nth16->dwSignature);
  591|     53|    return false;
  592|     53|  }
  593|    309|  if (len < sizeof(nth16_t) + sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (593:7): [True: 8, False: 301]
  ------------------
  594|      8|    TU_LOG_DRV("(EE) ill min len: %lu\n", len);
  595|      8|    return false;
  596|      8|  }
  597|    301|  if (nth16->wBlockLength > len) {
  ------------------
  |  Branch (597:7): [True: 16, False: 285]
  ------------------
  598|     16|    TU_LOG_DRV("(EE) ill block length: %d > %lu\n", nth16->wBlockLength, len);
  599|     16|    return false;
  600|     16|  }
  601|    285|  if (nth16->wBlockLength > CFG_TUD_NCM_OUT_NTB_MAX_SIZE) {
  ------------------
  |  |   17|    285|  #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
  ------------------
  |  Branch (601:7): [True: 0, False: 285]
  ------------------
  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|    285|  if (nth16->wNdpIndex < sizeof(nth16_t) || nth16->wNdpIndex > len - (sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t))) {
  ------------------
  |  Branch (605:7): [True: 13, False: 272]
  |  Branch (605:45): [True: 20, False: 252]
  ------------------
  606|     33|    TU_LOG_DRV("(EE) ill position of first ndp: %d (%lu)\n", nth16->wNdpIndex, len);
  607|     33|    return false;
  608|     33|  }
  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: 9, False: 243]
  ------------------
  614|      9|    TU_LOG_DRV("(EE) ill ndp16 length: %d\n", ndp16->wLength);
  615|      9|    return false;
  616|      9|  }
  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|    243|  if ((uint32_t) nth16->wNdpIndex + ndp16->wLength > len) {
  ------------------
  |  Branch (619:7): [True: 40, False: 203]
  ------------------
  620|     40|    TU_LOG_DRV("(EE) ill ndp16 length: %d (%lu)\n", ndp16->wLength, len);
  621|     40|    return false;
  622|     40|  }
  623|    203|  if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   84|    406|#define NDP16_SIGNATURE_NCM0 0x304D434E
  ------------------
                if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   85|    128|#define NDP16_SIGNATURE_NCM1 0x314D434E
  ------------------
  |  Branch (623:7): [True: 128, False: 75]
  |  Branch (623:53): [True: 84, False: 44]
  ------------------
  624|     84|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) ndp16->dwSignature);
  625|     84|    return false;
  626|     84|  }
  627|    119|  if (ndp16->wNextNdpIndex != 0) {
  ------------------
  |  Branch (627:7): [True: 18, False: 101]
  ------------------
  628|     18|    TU_LOG_DRV("(EE) cannot handle wNextNdpIndex!=0 (%d)\n", ndp16->wNextNdpIndex);
  629|     18|    return false;
  630|     18|  }
  631|       |
  632|    101|  const ndp16_datagram_t *ndp16_datagram = (const ndp16_datagram_t *) (ntb->data + nth16->wNdpIndex + sizeof(ndp16_t));
  633|    101|  int ndx = 0;
  634|    101|  uint16_t max_ndx = (uint16_t) ((ndp16->wLength - sizeof(ndp16_t)) / sizeof(ndp16_datagram_t));
  635|       |
  636|    101|  if (max_ndx > 2) { // number of datagrams in NTB > 1
  ------------------
  |  Branch (636:7): [True: 24, False: 77]
  ------------------
  637|     24|    TU_LOG_DRV("<< %d (%d)\n", max_ndx - 1, ntb->nth.wBlockLength);
  638|     24|  }
  639|    101|  if (ndp16_datagram[max_ndx - 1].wDatagramIndex != 0 || ndp16_datagram[max_ndx - 1].wDatagramLength != 0) {
  ------------------
  |  Branch (639:7): [True: 22, False: 79]
  |  Branch (639:58): [True: 19, False: 60]
  ------------------
  640|     41|    TU_LOG_DRV("  max_ndx != 0\n");
  641|     41|    return false;
  642|     41|  }
  643|    288|  while (ndp16_datagram[ndx].wDatagramIndex != 0 && ndp16_datagram[ndx].wDatagramLength != 0) {
  ------------------
  |  Branch (643:10): [True: 277, False: 11]
  |  Branch (643:53): [True: 274, False: 3]
  ------------------
  644|    274|    TU_LOG_DRV("  << %d %d\n", ndp16_datagram[ndx].wDatagramIndex, ndp16_datagram[ndx].wDatagramLength);
  645|    274|    if (ndp16_datagram[ndx].wDatagramIndex > len) {
  ------------------
  |  Branch (645:9): [True: 27, False: 247]
  ------------------
  646|     27|      TU_LOG_DRV("(EE) ill start of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex, len);
  647|     27|      return false;
  648|     27|    }
  649|    247|    if (ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength > len) {
  ------------------
  |  Branch (649:9): [True: 19, False: 228]
  ------------------
  650|     19|      TU_LOG_DRV("(EE) ill end of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength, len);
  651|     19|      return false;
  652|     19|    }
  653|    228|    ++ndx;
  654|    228|  }
  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|     14|  return true;
  663|     60|} // recv_validate_datagram

LLVMFuzzerTestOneInput:
   77|    384|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|    384|  recv_ntb_t *ntb = (recv_ntb_t *) malloc(sizeof(recv_ntb_t));
   81|    384|  if (ntb == NULL) {
  ------------------
  |  Branch (81:7): [True: 0, False: 384]
  ------------------
   82|      0|    return 0;
   83|      0|  }
   84|    384|  size_t n = size < sizeof(ntb->data) ? size : sizeof(ntb->data);
  ------------------
  |  Branch (84:14): [True: 374, False: 10]
  ------------------
   85|    384|  memset(ntb->data, 0, sizeof(ntb->data));
   86|    384|  memcpy(ntb->data, data, n);
   87|    384|  recv_validate_datagram(ntb, (uint32_t) n);
   88|    384|  free(ntb);
   89|    384|  return 0;
   90|    384|}

