fuzz.c:recv_validate_datagram:
  583|    373|static bool recv_validate_datagram(const recv_ntb_t *ntb, uint32_t len) {
  584|    373|  const nth16_t *nth16 = &(ntb->nth);
  585|       |
  586|    373|  TU_LOG_DRV("recv_validate_datagram(%p, %d)\n", ntb, (int) len);
  587|       |
  588|       |  // check header
  589|    373|  if (nth16->wHeaderLength != sizeof(nth16_t)) {
  ------------------
  |  Branch (589:7): [True: 25, False: 348]
  ------------------
  590|     25|    TU_LOG_DRV("(EE) ill nth16 length: %d\n", nth16->wHeaderLength);
  591|     25|    return false;
  592|     25|  }
  593|    348|  if (nth16->dwSignature != NTH16_SIGNATURE) {
  ------------------
  |  |   83|    348|#define NTH16_SIGNATURE 0x484D434E
  ------------------
  |  Branch (593:7): [True: 51, False: 297]
  ------------------
  594|     51|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) nth16->dwSignature);
  595|     51|    return false;
  596|     51|  }
  597|    297|  if (len < sizeof(nth16_t) + sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (597:7): [True: 8, False: 289]
  ------------------
  598|      8|    TU_LOG_DRV("(EE) ill min len: %lu\n", len);
  599|      8|    return false;
  600|      8|  }
  601|    289|  if (nth16->wBlockLength > len) {
  ------------------
  |  Branch (601:7): [True: 15, False: 274]
  ------------------
  602|     15|    TU_LOG_DRV("(EE) ill block length: %d > %lu\n", nth16->wBlockLength, len);
  603|     15|    return false;
  604|     15|  }
  605|    274|  if (nth16->wBlockLength > CFG_TUD_NCM_OUT_NTB_MAX_SIZE) {
  ------------------
  |  |   17|    274|  #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
  ------------------
  |  Branch (605:7): [True: 0, False: 274]
  ------------------
  606|      0|    TU_LOG_DRV("(EE) ill block length2: %d > %d\n", nth16->wBlockLength, CFG_TUD_NCM_OUT_NTB_MAX_SIZE);
  607|      0|    return false;
  608|      0|  }
  609|    274|  if (nth16->wNdpIndex < sizeof(nth16_t) || nth16->wNdpIndex > len - (sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t))) {
  ------------------
  |  Branch (609:7): [True: 13, False: 261]
  |  Branch (609:45): [True: 22, False: 239]
  ------------------
  610|     35|    TU_LOG_DRV("(EE) ill position of first ndp: %d (%lu)\n", nth16->wNdpIndex, len);
  611|     35|    return false;
  612|     35|  }
  613|       |
  614|       |  // check (first) NDP(16)
  615|    239|  const ndp16_t *ndp16 = (const ndp16_t *) (ntb->data + nth16->wNdpIndex);
  616|       |
  617|    239|  if (ndp16->wLength < sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (617:7): [True: 8, False: 231]
  ------------------
  618|      8|    TU_LOG_DRV("(EE) ill ndp16 length: %d\n", ndp16->wLength);
  619|      8|    return false;
  620|      8|  }
  621|       |  // the NDP block (wLength bytes from wNdpIndex) must fit within the received NTB, otherwise the
  622|       |  // datagram pointer array walked below (max_ndx is derived from wLength) runs past ntb->data
  623|    231|  if ((uint32_t) nth16->wNdpIndex + ndp16->wLength > len) {
  ------------------
  |  Branch (623:7): [True: 32, False: 199]
  ------------------
  624|     32|    TU_LOG_DRV("(EE) ill ndp16 length: %d (%lu)\n", ndp16->wLength, len);
  625|     32|    return false;
  626|     32|  }
  627|    199|  if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   84|    398|#define NDP16_SIGNATURE_NCM0 0x304D434E
  ------------------
                if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   85|    133|#define NDP16_SIGNATURE_NCM1 0x314D434E
  ------------------
  |  Branch (627:7): [True: 133, False: 66]
  |  Branch (627:53): [True: 87, False: 46]
  ------------------
  628|     87|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) ndp16->dwSignature);
  629|     87|    return false;
  630|     87|  }
  631|    112|  if (ndp16->wNextNdpIndex != 0) {
  ------------------
  |  Branch (631:7): [True: 17, False: 95]
  ------------------
  632|     17|    TU_LOG_DRV("(EE) cannot handle wNextNdpIndex!=0 (%d)\n", ndp16->wNextNdpIndex);
  633|     17|    return false;
  634|     17|  }
  635|       |
  636|     95|  const ndp16_datagram_t *ndp16_datagram = (const ndp16_datagram_t *) (ntb->data + nth16->wNdpIndex + sizeof(ndp16_t));
  637|     95|  int ndx = 0;
  638|     95|  uint16_t max_ndx = (uint16_t) ((ndp16->wLength - sizeof(ndp16_t)) / sizeof(ndp16_datagram_t));
  639|       |
  640|     95|  if (max_ndx > 2) { // number of datagrams in NTB > 1
  ------------------
  |  Branch (640:7): [True: 25, False: 70]
  ------------------
  641|     25|    TU_LOG_DRV("<< %d (%d)\n", max_ndx - 1, ntb->nth.wBlockLength);
  642|     25|  }
  643|     95|  if (ndp16_datagram[max_ndx - 1].wDatagramIndex != 0 || ndp16_datagram[max_ndx - 1].wDatagramLength != 0) {
  ------------------
  |  Branch (643:7): [True: 23, False: 72]
  |  Branch (643:58): [True: 20, False: 52]
  ------------------
  644|     43|    TU_LOG_DRV("  max_ndx != 0\n");
  645|     43|    return false;
  646|     43|  }
  647|    335|  while (ndp16_datagram[ndx].wDatagramIndex != 0 && ndp16_datagram[ndx].wDatagramLength != 0) {
  ------------------
  |  Branch (647:10): [True: 322, False: 13]
  |  Branch (647:53): [True: 318, False: 4]
  ------------------
  648|    318|    TU_LOG_DRV("  << %d %d\n", ndp16_datagram[ndx].wDatagramIndex, ndp16_datagram[ndx].wDatagramLength);
  649|    318|    if (ndp16_datagram[ndx].wDatagramIndex > len) {
  ------------------
  |  Branch (649:9): [True: 21, False: 297]
  ------------------
  650|     21|      TU_LOG_DRV("(EE) ill start of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex, len);
  651|     21|      return false;
  652|     21|    }
  653|    297|    if (ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength > len) {
  ------------------
  |  Branch (653:9): [True: 14, False: 283]
  ------------------
  654|     14|      TU_LOG_DRV("(EE) ill end of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength, len);
  655|     14|      return false;
  656|     14|    }
  657|    283|    ++ndx;
  658|    283|  }
  659|     17|  if (ndx == 0) {
  ------------------
  |  Branch (659:7): [True: 4, False: 13]
  ------------------
  660|      4|    TU_LOG_DRV("(EE) no datagram in ndp16\n");
  661|      4|    return false;
  662|      4|  }
  663|       |
  664|       |  #if CFG_TUD_NCM_LOG_LEVEL >= 3
  665|       |  TU_LOG_BUF(3, ntb->data[i], len);
  666|       |  #endif
  667|       |
  668|       |  // -> ntb contains a valid packet structure
  669|       |  //    ok... I did not check for garbage within the datagram indices...
  670|     13|  return true;
  671|     17|} // recv_validate_datagram

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

