net/colo-compare.c: Fix memory leak and code style issue.
PATCH v3 1/1 net/colo-compare.c: Fix memory leak and code style issue.
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue and add necessary check here.
Reported-by: Coverity (CID 1402785)
--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
net/colo-compare.c +21 -7
@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame); +static bool packet_matches_str (const char *str, +uint8_t *buf, +uint32_t packet_len) +{ +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str)); +} +static void notify_remote_frame (CompareState *s) {char msg[] ="DO_CHECKPOINT" ;
@@ -1008,21 +1019,24 @@ static void compare_notify_rs_finalize(SocketReadState *notify_rs) {CompareState *s =container_of (notify_rs, CompareState, notify_rs); -/* Get Xen colo-frame's notify and handle the message */ -char *data =g_memdup (notify_rs->buf, notify_rs->packet_len); -char msg[] ="COLO_COMPARE_GET_XEN_INIT" ; +const char msg[] ="COLO_COMPARE_GET_XEN_INIT" ;int ret; -if (!strcmp (data,"COLO_USERSPACE_PROXY_INIT" )) { +if (packet_matches_str ("COLO_USERSPACE_PROXY_INIT" , + notify_rs->buf, + notify_rs->packet_len)) { ret =compare_chr_send (s, (uint8_t *)msg,strlen (msg),0 , true);if (ret <0 ) {error_report ("Notify Xen COLO-frame INIT failed" ); } - } - -if (!strcmp (data,"COLO_CHECKPOINT" )) { + }else if (packet_matches_str ("COLO_CHECKPOINT" , + notify_rs->buf, + notify_rs->packet_len)) {/* colo-compare do checkpoint, flush pri packet and remove sec packet */ g_queue_foreach (&s->conn_list, colo_flush_packets, s); + }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } }
Signature · 1 line
2.17.GIT
Quoted 3 lines · Zhang Chen
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue
"memory"
Quoted 13 lines
and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c +0
-0
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 131
Quoted 5 lines
net/colo-compare.c +2
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame); +static bool packet_matches_str(const char *str, + uint8_t *buf,
You can use 'uint8_t *buf'.
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 7 lines
net/colo-compare.c +7
-0+ uint32_t packet_len) +{ +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str));
If you don't want to use a local variable to hold strlen(str), you can reuse packet_len since it is the same value:
return !memcmp(str, buf, packet_len);
However it makes the review harder, so I'd prefer using a str_len local var.
Quoted 38 lines
net/colo-compare.c +12
-7+} +static void notify_remote_frame (CompareState *s) {char msg[] ="DO_CHECKPOINT" ;@@ -1008,21 +1019,24 @@ static void compare_notify_rs_finalize(SocketReadState *notify_rs) {CompareState *s =container_of (notify_rs, CompareState, notify_rs); -/* Get Xen colo-frame's notify and handle the message */ -char *data =g_memdup (notify_rs->buf, notify_rs->packet_len); -char msg[] ="COLO_COMPARE_GET_XEN_INIT" ; +const char msg[] ="COLO_COMPARE_GET_XEN_INIT" ;int ret; -if (!strcmp (data,"COLO_USERSPACE_PROXY_INIT" )) { +if (packet_matches_str ("COLO_USERSPACE_PROXY_INIT" , + notify_rs->buf, + notify_rs->packet_len)) { ret =compare_chr_send (s, (uint8_t *)msg,strlen (msg),0 , true);if (ret <0 ) {error_report ("Notify Xen COLO-frame INIT failed" ); } - } - -if (!strcmp (data,"COLO_CHECKPOINT" )) { + }else if (packet_matches_str ("COLO_CHECKPOINT" , + notify_rs->buf, + notify_rs->packet_len)) {/* colo-compare do checkpoint, flush pri packet and remove sec packet */ g_queue_foreach (&s->conn_list, colo_flush_packets, s); + }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } }
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Zhang Chen
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue
"memory"
Quoted 16 lines
and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c +0
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame);
Quoted 4 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 131
Quoted 2 lines
net/colo-compare.c +2
-0+static bool packet_matches_str(const char *str, + uint8_t *buf,You can use '
uint8_t*buf'.
?? that seems to be the same as what is written...
Quoted 12 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +7
-0+ uint32_t packet_len) +{ +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str));If you don't want to use a local variable to hold strlen(str), you can reuse
packet_lensince it is the same value:
Quoted 3 lines
return !memcmp(str, buf, packet_len);However it makes the review harder, so I'd prefer using a
str_lenlocal var.
I'm pretty sure the compiler is going to optimise the strlen() away into a compile time constant anyway, so this is somewhat unnecessary micro-optimisation I think.
thanks
-- PMM
Quoted 25 lines · Peter Maydell
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Zhang Chen
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue
"memory"
Quoted 16 lines
and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c +0
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame);
Quoted 6 lines
Quoted 4 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 131
Quoted 2 lines
net/colo-compare.c +2
-0+static bool packet_matches_str(const char *str, + uint8_t *buf,You can use '
uint8_t*buf'.?? that seems to be the same as what is written...
Oops sorry, I copy/pasted and did not noticed I removed the 'const' word. So I meant: You can use 'const uint8_t *buf'
Quoted 12 lines
Quoted 12 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +7
-0+ uint32_t packet_len) +{ +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str));If you don't want to use a local variable to hold strlen(str), you can reuse
packet_lensince it is the same value:
Quoted 7 lines
Quoted 3 lines
return !memcmp(str, buf, packet_len);However it makes the review harder, so I'd prefer using a
str_lenlocal var.I'm pretty sure the compiler is going to optimise the
strlen()away into a compile time constant anyway, so this is somewhat unnecessary micro-optimisation I think.
I was not sure, I'm glad to learn that :)
Thanks,
Phil.
Quoted 12 lines
-----Original Message-----
From: Philippe Mathieu-Daudé [mailto:[email protected]]
Sent: Thursday, July 18, 2019 6:54 PM
To: Peter Maydell <[email protected]>
Cc: Zhang, Chen <[email protected]>; Li Zhijian <[email protected]>; Jason Wang <[email protected]>; qemu-dev <[email protected]>; Zhang Chen <[email protected]>
Subject: Re: [Qemu-devel] [PATCH V3]net/colo-compare.c: Fix memory leak and code style issue.
Quoted 11 lines
Quoted 1 line · Peter Maydell
On Thu, 18 Jul 2019 at 11:28, Philippe Mathieu-Daudé <[email protected]>
Quoted 8 lines · wrote:
Quoted 8 lines · Zhang Chen
Quoted 4 lines
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue
"memory"
Oh, I will fix this typo in next version.
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 14 lines
and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c index renamed from net/colo-compare.c +0
-0
909dd6c6eb..fcccb4c6f6100644net/colo-compare.c static int compare_chr_send(CompareState *s, · line 131
Quoted 5 lines
net/colo-compare.c +2
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame); +static bool packet_matches_str(const char *str, + uint8_t *buf,You can use '
uint8_t*buf'.
Quoted 4 lines
Quoted 1 line
?? that seems to be the same as what is written...
Oops sorry, I copy/pasted and did not noticed I removed the 'const' word. So I meant: You can use 'const
uint8_t*buf'
OK.
Thanks
Zhang Chen
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +6
-0+ uint32_t packet_len) { +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str));If you don't want to use a local variable to hold strlen(str), you can reuse
packet_lensince it is the same value:Quoted 7 lines
Quoted 3 lines
return !memcmp(str, buf, packet_len);However it makes the review harder, so I'd prefer using a
str_lenlocal var.I'm pretty sure the compiler is going to optimise the
strlen()away into a compile time constant anyway, so this is somewhat unnecessary micro-optimisation I think.I was not sure, I'm glad to learn that :)
Thanks,
Phil.
Quoted 17 lines · Zhang Chen
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c +0
-0
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 134
Quoted 8 lines
net/colo-compare.c +5
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame); +static bool packet_matches_str (const char *str, +uint8_t *buf, +uint32_t packet_len) +{ +if (packet_len !=strlen (str)) {
Is '!=' definitely correct? (ie the incoming packet must *not* contain a trailing '\0' or any other trailing data) ?
Is there a specification of the protocol somewhere? If so, that presumably should say one way or the other.
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 139
Quoted 5 lines
net/colo-compare.c +5
-0+return false; + } + +return !memcmp (str, buf,strlen (str)); +}
thanks
-- PMM
Quoted 31 lines
-----Original Message-----
From: Peter Maydell [mailto:[email protected]]
Sent: Thursday, July 18, 2019 9:42 PM
To: Zhang, Chen <[email protected]>
Cc: Li Zhijian <[email protected]>; Jason Wang <[email protected]>; qemu-dev <[email protected]>; Zhang Chen <[email protected]>
Subject: Re: [PATCH V3]net/colo-compare.c: Fix memory leak and code style issue.Quoted 20 lines · Zhang Chen
From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c index renamed from net/colo-compare.c +0
-0
909dd6c6eb..fcccb4c6f6100644net/colo-compare.c +0
-0@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame);
Quoted 7 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 134
Quoted 4 lines
net/colo-compare.c +4
-0+static bool packet_matches_str (const char *str, +uint8_t *buf, +uint32_t packet_len) { +if (packet_len !=strlen (str)) {Is '!=' definitely correct? (ie the incoming packet must *not* contain a trailing '\0' or any other trailing data) ?
Yes, the packet not contain a trail.
As Jason comments before, you can see the net/net.c "net_fill_rstate()". We just got the length and data.
Thanks
Zhang Chen
Quoted 11 lines
Is there a specification of the protocol somewhere? If so, that presumably should say one way or the other.
net/colo-compare.c static int compare_chr_send(CompareState *s,
Quoted 4 lines
net/colo-compare.c +4
-0+return false; + } + +return !memcmp (str, buf,strlen (str)); }thanks
-- PMM
Sorry, I re-sent the old version.
Please redirect to V4 patch.
Thanks
Zhang Chen
Quoted 73 lines
-----Original Message-----
From: Zhang, Chen
Sent: Thursday, July 18, 2019 5:22 PM
To: Li Zhijian <[email protected]>; Peter Maydell <[email protected]>; Jason Wang <[email protected]>; qemu-dev <[email protected]>
Cc: Zhang Chen <[email protected]>; Zhang, Chen <[email protected]>
Subject: [PATCH V3]net/colo-compare.c: Fix memory leak and code style issue.From: Zhang Chen <[email protected]>
This patch to fix the origin "char *data" menory leak, code style issue and add necessary check here.
Reported-by: Coverity (CID 1402785)Signed-off-by Zhang Chen <[email protected]>--- net/colo-compare.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)net/colo-compare.c index renamed from net/colo-compare.c +0
-0
909dd6c6eb..fcccb4c6f6100644net/colo-compare.c +19
-8@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame); +static bool packet_matches_str (const char *str, +uint8_t *buf, +uint32_t packet_len) { +if (packet_len !=strlen (str)) { +return false; + } + +return !memcmp (str, buf,strlen (str)); } +static void notify_remote_frame (CompareState *s) {char msg[] ="DO_CHECKPOINT" ;@@ -1008,21 +1019,24 @@ static void compare_notify_rs_finalize(SocketReadState *notify_rs) {CompareState *s =container_of (notify_rs, CompareState, notify_rs); -/* Get Xen colo-frame's notify and handle the message */ -char *data =g_memdup (notify_rs->buf, notify_rs->packet_len); -char msg[] ="COLO_COMPARE_GET_XEN_INIT" ; +const char msg[] ="COLO_COMPARE_GET_XEN_INIT" ;int ret; -if (!strcmp (data,"COLO_USERSPACE_PROXY_INIT" )) { +if (packet_matches_str ("COLO_USERSPACE_PROXY_INIT" , + notify_rs->buf, + notify_rs->packet_len)) { ret =compare_chr_send (s, (uint8_t *)msg,strlen (msg),0 , true);if (ret <0 ) {error_report ("Notify Xen COLO-frame INIT failed" ); } - } - -if (!strcmp (data,"COLO_CHECKPOINT" )) { + }else if (packet_matches_str ("COLO_CHECKPOINT" , + notify_rs->buf, + notify_rs->packet_len)) {/* colo-compare do checkpoint, flush pri packet and remove sec packet */ g_queue_foreach (&s->conn_list, colo_flush_packets, s); + }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } } --2.17.GIT