From 9af71c25a61e34032a66e351ca67a2ed196f715c Mon Sep 17 00:00:00 2001
From: wuchangsheng <wuchangsheng2@huawei.com>
Date: Thu, 6 Oct 2022 21:09:57 +0800
Subject: [PATCH] fix free pbuf miss data

---
 src/core/tcp_out.c     | 18 ++++++++++++++++++
 src/include/lwipopts.h |  2 +-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c
index 061af5f..7ebcfea 100644
--- a/src/core/tcp_out.c
+++ b/src/core/tcp_out.c
@@ -682,11 +682,24 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
     if (queuelen > LWIP_MIN(TCP_SND_QUEUELEN, TCP_SNDQUEUELEN_OVERFLOW)) {
       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: queue too long %"U16_F" (%d)\n",
                   queuelen, (int)TCP_SND_QUEUELEN));
+#if USE_LIBOS
+      if (pos > 0) {
+        queuelen -= pbuf_clen(p);
+        break;
+      }
+#else
       pbuf_free(p);
+#endif
       goto memerr;
     }
 
     if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
+#if USE_LIBOS
+      if (pos > 0) {
+        queuelen -= pbuf_clen(p);
+        break;
+      }
+#endif
       goto memerr;
     }
 #if TCP_OVERSIZE_DBGCHECK
@@ -714,6 +727,9 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
                 lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
 
     pos += seglen;
+#if USE_LIBOS
+    write_lwip_over((struct lwip_sock*)arg, 1);
+#endif
   }
 
   /*
@@ -825,12 +841,14 @@ memerr:
   tcp_set_flags(pcb, TF_NAGLEMEMERR);
   TCP_STATS_INC(tcp.memerr);
 
+#if !USE_LIBOS
   if (concat_p != NULL) {
     pbuf_free(concat_p);
   }
   if (queue != NULL) {
     tcp_segs_free(queue);
   }
+#endif
   if (pcb->snd_queuelen != 0) {
     LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
                 pcb->unsent != NULL);
diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
index 75d3c74..7459991 100644
--- a/src/include/lwipopts.h
+++ b/src/include/lwipopts.h
@@ -133,7 +133,7 @@
 
 #define USE_LIBOS 1
 
-#define LWIP_DEBUG 1
+//#define LWIP_DEBUG 1
 
 #define LWIP_PERF 1
 
-- 
2.27.0

