fuzz.c:recv_validate_datagram:
  612|    392|static bool recv_validate_datagram(const recv_ntb_t *ntb, uint32_t len) {
  613|    392|  const nth16_t *nth16 = &(ntb->nth);
  614|       |
  615|    392|  TU_LOG_DRV("recv_validate_datagram(%p, %d)\n", ntb, (int) len);
  616|       |
  617|       |  // check header
  618|    392|  if (nth16->wHeaderLength != sizeof(nth16_t)) {
  ------------------
  |  Branch (618:7): [True: 28, False: 364]
  ------------------
  619|     28|    TU_LOG_DRV("(EE) ill nth16 length: %d\n", nth16->wHeaderLength);
  620|     28|    return false;
  621|     28|  }
  622|    364|  if (nth16->dwSignature != NTH16_SIGNATURE) {
  ------------------
  |  |   83|    364|#define NTH16_SIGNATURE 0x484D434E
  ------------------
  |  Branch (622:7): [True: 51, False: 313]
  ------------------
  623|     51|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) nth16->dwSignature);
  624|     51|    return false;
  625|     51|  }
  626|    313|  if (!TUD_VALIDATE_BASIC(len <= CFG_TUD_NCM_OUT_NTB_MAX_SIZE)) {
  ------------------
  |  |  313|    313|#define TUD_VALIDATE_BASIC(_cond) TUD_VALIDATION_CHECK(TUSB_VALIDATION_BASIC, _cond)
  |  |  ------------------
  |  |  |  |  310|    313|#define TUD_VALIDATION_CHECK(_level, _cond) ((CFG_TUD_VALIDATION_LEVEL < (_level)) || (_cond))
  |  |  |  |  ------------------
  |  |  |  |  |  |  287|    313|  #define CFG_TUD_VALIDATION_LEVEL CFG_TUSB_VALIDATION_LEVEL
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  283|    313|  #define CFG_TUSB_VALIDATION_LEVEL TUSB_VALIDATION_BASIC
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  262|    313|#define TUSB_VALIDATION_BASIC  1 ///< structural memory-safety checks where supported
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  |  |  |  Branch (310:46): [Folded, False: 313]
  |  |  |  |  |  Branch (310:87): [True: 313, False: 0]
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  627|      0|    TU_LOG_DRV("(EE) ill len: %lu > %d\n", len, CFG_TUD_NCM_OUT_NTB_MAX_SIZE);
  628|      0|    return false;
  629|      0|  }
  630|    313|  if (len < sizeof(nth16_t) + sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (630:7): [True: 10, False: 303]
  ------------------
  631|     10|    TU_LOG_DRV("(EE) ill min len: %lu\n", len);
  632|     10|    return false;
  633|     10|  }
  634|    303|  if (nth16->wBlockLength > len) {
  ------------------
  |  Branch (634:7): [True: 20, False: 283]
  ------------------
  635|     20|    TU_LOG_DRV("(EE) ill block length: %d > %lu\n", nth16->wBlockLength, len);
  636|     20|    return false;
  637|     20|  }
  638|    283|  if (nth16->wBlockLength > CFG_TUD_NCM_OUT_NTB_MAX_SIZE) {
  ------------------
  |  |   17|    283|  #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
  ------------------
  |  Branch (638:7): [True: 0, False: 283]
  ------------------
  639|      0|    TU_LOG_DRV("(EE) ill block length2: %d > %d\n", nth16->wBlockLength, CFG_TUD_NCM_OUT_NTB_MAX_SIZE);
  640|      0|    return false;
  641|      0|  }
  642|    283|  if (nth16->wNdpIndex < sizeof(nth16_t) || nth16->wNdpIndex > len - (sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t))) {
  ------------------
  |  Branch (642:7): [True: 4, False: 279]
  |  Branch (642:45): [True: 20, False: 259]
  ------------------
  643|     24|    TU_LOG_DRV("(EE) ill position of first ndp: %d (%lu)\n", nth16->wNdpIndex, len);
  644|     24|    return false;
  645|     24|  }
  646|       |
  647|       |  // check (first) NDP(16)
  648|    259|  const ndp16_t *ndp16 = (const ndp16_t *) (ntb->data + nth16->wNdpIndex);
  649|       |
  650|    259|  if (ndp16->wLength < sizeof(ndp16_t) + 2 * sizeof(ndp16_datagram_t)) {
  ------------------
  |  Branch (650:7): [True: 7, False: 252]
  ------------------
  651|      7|    TU_LOG_DRV("(EE) ill ndp16 length: %d\n", ndp16->wLength);
  652|      7|    return false;
  653|      7|  }
  654|       |  // the NDP block (wLength bytes from wNdpIndex) must fit within the received NTB, otherwise the
  655|       |  // datagram pointer array walked below (max_ndx is derived from wLength) runs past ntb->data
  656|    252|  if ((uint32_t) nth16->wNdpIndex + ndp16->wLength > len) {
  ------------------
  |  Branch (656:7): [True: 36, False: 216]
  ------------------
  657|     36|    TU_LOG_DRV("(EE) ill ndp16 length: %d (%lu)\n", ndp16->wLength, len);
  658|     36|    return false;
  659|     36|  }
  660|    216|  if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   84|    432|#define NDP16_SIGNATURE_NCM0 0x304D434E
  ------------------
                if (ndp16->dwSignature != NDP16_SIGNATURE_NCM0 && ndp16->dwSignature != NDP16_SIGNATURE_NCM1) {
  ------------------
  |  |   85|    154|#define NDP16_SIGNATURE_NCM1 0x314D434E
  ------------------
  |  Branch (660:7): [True: 154, False: 62]
  |  Branch (660:53): [True: 88, False: 66]
  ------------------
  661|     88|    TU_LOG_DRV("(EE) ill signature: 0x%08x\n", (unsigned) ndp16->dwSignature);
  662|     88|    return false;
  663|     88|  }
  664|    128|  if (ndp16->wNextNdpIndex != 0) {
  ------------------
  |  Branch (664:7): [True: 19, False: 109]
  ------------------
  665|     19|    TU_LOG_DRV("(EE) cannot handle wNextNdpIndex!=0 (%d)\n", ndp16->wNextNdpIndex);
  666|     19|    return false;
  667|     19|  }
  668|       |
  669|    109|  const ndp16_datagram_t *ndp16_datagram = (const ndp16_datagram_t *) (ntb->data + nth16->wNdpIndex + sizeof(ndp16_t));
  670|    109|  int ndx = 0;
  671|    109|  uint16_t max_ndx = (uint16_t) ((ndp16->wLength - sizeof(ndp16_t)) / sizeof(ndp16_datagram_t));
  672|       |
  673|    109|  if (max_ndx > 2) { // number of datagrams in NTB > 1
  ------------------
  |  Branch (673:7): [True: 39, False: 70]
  ------------------
  674|     39|    TU_LOG_DRV("<< %d (%d)\n", max_ndx - 1, ntb->nth.wBlockLength);
  675|     39|  }
  676|    109|  if (ndp16_datagram[max_ndx - 1].wDatagramIndex != 0 || ndp16_datagram[max_ndx - 1].wDatagramLength != 0) {
  ------------------
  |  Branch (676:7): [True: 26, False: 83]
  |  Branch (676:58): [True: 19, False: 64]
  ------------------
  677|     45|    TU_LOG_DRV("  max_ndx != 0\n");
  678|     45|    return false;
  679|     45|  }
  680|    782|  while (ndp16_datagram[ndx].wDatagramIndex != 0 && ndp16_datagram[ndx].wDatagramLength != 0) {
  ------------------
  |  Branch (680:10): [True: 765, False: 17]
  |  Branch (680:53): [True: 760, False: 5]
  ------------------
  681|    760|    TU_LOG_DRV("  << %d %d\n", ndp16_datagram[ndx].wDatagramIndex, ndp16_datagram[ndx].wDatagramLength);
  682|    760|    if (ndp16_datagram[ndx].wDatagramIndex > len) {
  ------------------
  |  Branch (682:9): [True: 25, False: 735]
  ------------------
  683|     25|      TU_LOG_DRV("(EE) ill start of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex, len);
  684|     25|      return false;
  685|     25|    }
  686|    735|    if (ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength > len) {
  ------------------
  |  Branch (686:9): [True: 17, False: 718]
  ------------------
  687|     17|      TU_LOG_DRV("(EE) ill end of datagram[%d]: %d (%lu)\n", ndx, ndp16_datagram[ndx].wDatagramIndex + ndp16_datagram[ndx].wDatagramLength, len);
  688|     17|      return false;
  689|     17|    }
  690|    718|    ++ndx;
  691|    718|  }
  692|     22|  if (ndx == 0) {
  ------------------
  |  Branch (692:7): [True: 2, False: 20]
  ------------------
  693|      2|    TU_LOG_DRV("(EE) no datagram in ndp16\n");
  694|      2|    return false;
  695|      2|  }
  696|       |
  697|       |  #if CFG_TUD_NCM_LOG_LEVEL >= 3
  698|       |  TU_LOG_BUF(3, ntb->data[i], len);
  699|       |  #endif
  700|       |
  701|       |  // -> ntb contains a valid packet structure
  702|       |  //    ok... I did not check for garbage within the datagram indices...
  703|     20|  return true;
  704|     22|} // recv_validate_datagram

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

