diff mbox

[V4] colo-proxy: fix memory leak

Message ID 1476696239-21655-2-git-send-email-zhangchen.fnst@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Zhang Chen Oct. 17, 2016, 9:23 a.m. UTC
Fix memory leak in colo-compare.c and filter-rewriter.c
Report by Coverity and add some comments.

Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
---
 net/colo-compare.c    | 34 +++++++++++++++-------------------
 net/filter-rewriter.c | 17 +++++------------
 net/trace-events      |  1 +
 3 files changed, 21 insertions(+), 31 deletions(-)

Comments

Zhang Chen Oct. 24, 2016, 1:58 a.m. UTC | #1
Anyone have some comments?

this bug report by paolo

ping...


On 10/17/2016 05:23 PM, Zhang Chen wrote:
> Fix memory leak in colo-compare.c and filter-rewriter.c
> Report by Coverity and add some comments.
>
> Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
> ---
>   net/colo-compare.c    | 34 +++++++++++++++-------------------
>   net/filter-rewriter.c | 17 +++++------------
>   net/trace-events      |  1 +
>   3 files changed, 21 insertions(+), 31 deletions(-)
>
> diff --git a/net/colo-compare.c b/net/colo-compare.c
> index 47703c5..834b1f5 100644
> --- a/net/colo-compare.c
> +++ b/net/colo-compare.c
> @@ -188,7 +188,6 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
>   {
>       struct tcphdr *ptcp, *stcp;
>       int res;
> -    char *sdebug, *ddebug;
>   
>       trace_colo_compare_main("compare tcp");
>       if (ppkt->size != spkt->size) {
> @@ -219,24 +218,21 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
>                   (spkt->size - ETH_HLEN));
>   
>       if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
> -        sdebug = strdup(inet_ntoa(ppkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(ppkt->ip->ip_dst));
> -        fprintf(stderr, "%s: src/dst: %s/%s p: seq/ack=%u/%u"
> -                " s: seq/ack=%u/%u res=%d flags=%x/%x\n",
> -                __func__, sdebug, ddebug,
> -                (unsigned int)ntohl(ptcp->th_seq),
> -                (unsigned int)ntohl(ptcp->th_ack),
> -                (unsigned int)ntohl(stcp->th_seq),
> -                (unsigned int)ntohl(stcp->th_ack),
> -                res, ptcp->th_flags, stcp->th_flags);
> -
> -        fprintf(stderr, "Primary len = %d\n", ppkt->size);
> -        qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
> -        fprintf(stderr, "Secondary len = %d\n", spkt->size);
> -        qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
> -
> -        g_free(sdebug);
> -        g_free(ddebug);
> +        trace_colo_compare_pkt_info(inet_ntoa(ppkt->ip->ip_src),
> +                                    inet_ntoa(ppkt->ip->ip_dst),
> +                                    ntohl(ptcp->th_seq),
> +                                    ntohl(ptcp->th_ack),
> +                                    ntohl(stcp->th_seq),
> +                                    ntohl(stcp->th_ack),
> +                                    res, ptcp->th_flags,
> +                                    stcp->th_flags,
> +                                    ppkt->size,
> +                                    spkt->size);
> +
> +        qemu_hexdump((char *)ppkt->data, stderr,
> +                     "colo-compare ppkt", ppkt->size);
> +        qemu_hexdump((char *)spkt->data, stderr,
> +                     "colo-compare spkt", spkt->size);
>       }
>   
>       return res;
> diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c
> index 89abe72..c4ab91c 100644
> --- a/net/filter-rewriter.c
> +++ b/net/filter-rewriter.c
> @@ -68,15 +68,11 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
>   
>       tcp_pkt = (struct tcphdr *)pkt->transport_header;
>       if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
> -        char *sdebug, *ddebug;
> -        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
> -        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
> +        trace_colo_filter_rewriter_pkt_info(__func__,
> +                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
>                       ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
>                       tcp_pkt->th_flags);
>           trace_colo_filter_rewriter_conn_offset(conn->offset);
> -        g_free(sdebug);
> -        g_free(ddebug);
>       }
>   
>       if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
> @@ -116,15 +112,11 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
>       tcp_pkt = (struct tcphdr *)pkt->transport_header;
>   
>       if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
> -        char *sdebug, *ddebug;
> -        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
> -        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
> +        trace_colo_filter_rewriter_pkt_info(__func__,
> +                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
>                       ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
>                       tcp_pkt->th_flags);
>           trace_colo_filter_rewriter_conn_offset(conn->offset);
> -        g_free(sdebug);
> -        g_free(ddebug);
>       }
>   
>       if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
> @@ -162,6 +154,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
>   
>       iov_to_buf(iov, iovcnt, 0, buf, size);
>       pkt = packet_new(buf, size);
> +    g_free(buf);
>   
>       /*
>        * if we get tcp packet
> diff --git a/net/trace-events b/net/trace-events
> index d67f048..b1913a6 100644
> --- a/net/trace-events
> +++ b/net/trace-events
> @@ -13,6 +13,7 @@ colo_compare_icmp_miscompare(const char *sta, int size) ": %s = %d"
>   colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s"
>   colo_old_packet_check_found(int64_t old_time) "%" PRId64
>   colo_compare_miscompare(void) ""
> +colo_compare_pkt_info(const char *src, const char *dst, uint32_t pseq, uint32_t pack, uint32_t sseq, uint32_t sack, int res, uint32_t pflag, uint32_t sflag, int psize, int ssize) "src/dst: %s/%s p: seq/ack=%u/%u   s: seq/ack=%u/%u res=%d flags=%x/%x ppkt_size: %d spkt_size: %d\n"
>   
>   # net/filter-rewriter.c
>   colo_filter_rewriter_debug(void) ""
Zhanghailiang Oct. 24, 2016, 3:08 a.m. UTC | #2
On 2016/10/17 17:23, Zhang Chen wrote:
> Fix memory leak in colo-compare.c and filter-rewriter.c
> Report by Coverity and add some comments.
>
> Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
> ---

Reviewed-by: zhanghailiang <zhang.zhanghailiang@huawei.com>

>   net/colo-compare.c    | 34 +++++++++++++++-------------------
>   net/filter-rewriter.c | 17 +++++------------
>   net/trace-events      |  1 +
>   3 files changed, 21 insertions(+), 31 deletions(-)
>
> diff --git a/net/colo-compare.c b/net/colo-compare.c
> index 47703c5..834b1f5 100644
> --- a/net/colo-compare.c
> +++ b/net/colo-compare.c
> @@ -188,7 +188,6 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
>   {
>       struct tcphdr *ptcp, *stcp;
>       int res;
> -    char *sdebug, *ddebug;
>
>       trace_colo_compare_main("compare tcp");
>       if (ppkt->size != spkt->size) {
> @@ -219,24 +218,21 @@ static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
>                   (spkt->size - ETH_HLEN));
>
>       if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
> -        sdebug = strdup(inet_ntoa(ppkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(ppkt->ip->ip_dst));
> -        fprintf(stderr, "%s: src/dst: %s/%s p: seq/ack=%u/%u"
> -                " s: seq/ack=%u/%u res=%d flags=%x/%x\n",
> -                __func__, sdebug, ddebug,
> -                (unsigned int)ntohl(ptcp->th_seq),
> -                (unsigned int)ntohl(ptcp->th_ack),
> -                (unsigned int)ntohl(stcp->th_seq),
> -                (unsigned int)ntohl(stcp->th_ack),
> -                res, ptcp->th_flags, stcp->th_flags);
> -
> -        fprintf(stderr, "Primary len = %d\n", ppkt->size);
> -        qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
> -        fprintf(stderr, "Secondary len = %d\n", spkt->size);
> -        qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
> -
> -        g_free(sdebug);
> -        g_free(ddebug);
> +        trace_colo_compare_pkt_info(inet_ntoa(ppkt->ip->ip_src),
> +                                    inet_ntoa(ppkt->ip->ip_dst),
> +                                    ntohl(ptcp->th_seq),
> +                                    ntohl(ptcp->th_ack),
> +                                    ntohl(stcp->th_seq),
> +                                    ntohl(stcp->th_ack),
> +                                    res, ptcp->th_flags,
> +                                    stcp->th_flags,
> +                                    ppkt->size,
> +                                    spkt->size);
> +
> +        qemu_hexdump((char *)ppkt->data, stderr,
> +                     "colo-compare ppkt", ppkt->size);
> +        qemu_hexdump((char *)spkt->data, stderr,
> +                     "colo-compare spkt", spkt->size);
>       }
>
>       return res;
> diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c
> index 89abe72..c4ab91c 100644
> --- a/net/filter-rewriter.c
> +++ b/net/filter-rewriter.c
> @@ -68,15 +68,11 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
>
>       tcp_pkt = (struct tcphdr *)pkt->transport_header;
>       if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
> -        char *sdebug, *ddebug;
> -        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
> -        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
> +        trace_colo_filter_rewriter_pkt_info(__func__,
> +                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
>                       ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
>                       tcp_pkt->th_flags);
>           trace_colo_filter_rewriter_conn_offset(conn->offset);
> -        g_free(sdebug);
> -        g_free(ddebug);
>       }
>
>       if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
> @@ -116,15 +112,11 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
>       tcp_pkt = (struct tcphdr *)pkt->transport_header;
>
>       if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
> -        char *sdebug, *ddebug;
> -        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
> -        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
> -        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
> +        trace_colo_filter_rewriter_pkt_info(__func__,
> +                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
>                       ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
>                       tcp_pkt->th_flags);
>           trace_colo_filter_rewriter_conn_offset(conn->offset);
> -        g_free(sdebug);
> -        g_free(ddebug);
>       }
>
>       if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
> @@ -162,6 +154,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
>
>       iov_to_buf(iov, iovcnt, 0, buf, size);
>       pkt = packet_new(buf, size);
> +    g_free(buf);
>
>       /*
>        * if we get tcp packet
> diff --git a/net/trace-events b/net/trace-events
> index d67f048..b1913a6 100644
> --- a/net/trace-events
> +++ b/net/trace-events
> @@ -13,6 +13,7 @@ colo_compare_icmp_miscompare(const char *sta, int size) ": %s = %d"
>   colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s"
>   colo_old_packet_check_found(int64_t old_time) "%" PRId64
>   colo_compare_miscompare(void) ""
> +colo_compare_pkt_info(const char *src, const char *dst, uint32_t pseq, uint32_t pack, uint32_t sseq, uint32_t sack, int res, uint32_t pflag, uint32_t sflag, int psize, int ssize) "src/dst: %s/%s p: seq/ack=%u/%u   s: seq/ack=%u/%u res=%d flags=%x/%x ppkt_size: %d spkt_size: %d\n"
>
>   # net/filter-rewriter.c
>   colo_filter_rewriter_debug(void) ""
>
Jason Wang Oct. 26, 2016, 1:54 a.m. UTC | #3
On 2016年10月24日 11:08, Hailiang Zhang wrote:
> On 2016/10/17 17:23, Zhang Chen wrote:
>> Fix memory leak in colo-compare.c and filter-rewriter.c
>> Report by Coverity and add some comments.
>>
>> Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
>> ---
>
> Reviewed-by: zhanghailiang <zhang.zhanghailiang@huawei.com>

Applied to -net.

Thanks.
diff mbox

Patch

diff --git a/net/colo-compare.c b/net/colo-compare.c
index 47703c5..834b1f5 100644
--- a/net/colo-compare.c
+++ b/net/colo-compare.c
@@ -188,7 +188,6 @@  static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
 {
     struct tcphdr *ptcp, *stcp;
     int res;
-    char *sdebug, *ddebug;
 
     trace_colo_compare_main("compare tcp");
     if (ppkt->size != spkt->size) {
@@ -219,24 +218,21 @@  static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
                 (spkt->size - ETH_HLEN));
 
     if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
-        sdebug = strdup(inet_ntoa(ppkt->ip->ip_src));
-        ddebug = strdup(inet_ntoa(ppkt->ip->ip_dst));
-        fprintf(stderr, "%s: src/dst: %s/%s p: seq/ack=%u/%u"
-                " s: seq/ack=%u/%u res=%d flags=%x/%x\n",
-                __func__, sdebug, ddebug,
-                (unsigned int)ntohl(ptcp->th_seq),
-                (unsigned int)ntohl(ptcp->th_ack),
-                (unsigned int)ntohl(stcp->th_seq),
-                (unsigned int)ntohl(stcp->th_ack),
-                res, ptcp->th_flags, stcp->th_flags);
-
-        fprintf(stderr, "Primary len = %d\n", ppkt->size);
-        qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
-        fprintf(stderr, "Secondary len = %d\n", spkt->size);
-        qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
-
-        g_free(sdebug);
-        g_free(ddebug);
+        trace_colo_compare_pkt_info(inet_ntoa(ppkt->ip->ip_src),
+                                    inet_ntoa(ppkt->ip->ip_dst),
+                                    ntohl(ptcp->th_seq),
+                                    ntohl(ptcp->th_ack),
+                                    ntohl(stcp->th_seq),
+                                    ntohl(stcp->th_ack),
+                                    res, ptcp->th_flags,
+                                    stcp->th_flags,
+                                    ppkt->size,
+                                    spkt->size);
+
+        qemu_hexdump((char *)ppkt->data, stderr,
+                     "colo-compare ppkt", ppkt->size);
+        qemu_hexdump((char *)spkt->data, stderr,
+                     "colo-compare spkt", spkt->size);
     }
 
     return res;
diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c
index 89abe72..c4ab91c 100644
--- a/net/filter-rewriter.c
+++ b/net/filter-rewriter.c
@@ -68,15 +68,11 @@  static int handle_primary_tcp_pkt(NetFilterState *nf,
 
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
     if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
-        char *sdebug, *ddebug;
-        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
-        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
-        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
+        trace_colo_filter_rewriter_pkt_info(__func__,
+                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
                     tcp_pkt->th_flags);
         trace_colo_filter_rewriter_conn_offset(conn->offset);
-        g_free(sdebug);
-        g_free(ddebug);
     }
 
     if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
@@ -116,15 +112,11 @@  static int handle_secondary_tcp_pkt(NetFilterState *nf,
     tcp_pkt = (struct tcphdr *)pkt->transport_header;
 
     if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
-        char *sdebug, *ddebug;
-        sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
-        ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
-        trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
+        trace_colo_filter_rewriter_pkt_info(__func__,
+                    inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
                     ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
                     tcp_pkt->th_flags);
         trace_colo_filter_rewriter_conn_offset(conn->offset);
-        g_free(sdebug);
-        g_free(ddebug);
     }
 
     if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
@@ -162,6 +154,7 @@  static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
 
     iov_to_buf(iov, iovcnt, 0, buf, size);
     pkt = packet_new(buf, size);
+    g_free(buf);
 
     /*
      * if we get tcp packet
diff --git a/net/trace-events b/net/trace-events
index d67f048..b1913a6 100644
--- a/net/trace-events
+++ b/net/trace-events
@@ -13,6 +13,7 @@  colo_compare_icmp_miscompare(const char *sta, int size) ": %s = %d"
 colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s"
 colo_old_packet_check_found(int64_t old_time) "%" PRId64
 colo_compare_miscompare(void) ""
+colo_compare_pkt_info(const char *src, const char *dst, uint32_t pseq, uint32_t pack, uint32_t sseq, uint32_t sack, int res, uint32_t pflag, uint32_t sflag, int psize, int ssize) "src/dst: %s/%s p: seq/ack=%u/%u   s: seq/ack=%u/%u res=%d flags=%x/%x ppkt_size: %d spkt_size: %d\n"
 
 # net/filter-rewriter.c
 colo_filter_rewriter_debug(void) ""