net/colo-compare.c: Fix memory leak and code style issue.
PATCH v2 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) +1 ); +} +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 18 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@@ -127,6 +127,17 @@ static int compare_chr_send(CompareState *s,uint32_t vnet_hdr_len, bool notify_remote_frame);
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 10 lines
net/colo-compare.c +9
-0+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) +1 );
This assumes buf is NULL terminated (you pass notify_rs->buf) which is not correct I think?
Thanks
Quoted 37 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 33 lines
-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Tuesday, July 9, 2019 10:48 PM
To: Zhang, Chen <[email protected]>; Li Zhijian <[email protected]>; Peter Maydell <[email protected]>; qemu-dev <qemu- [email protected]>
Cc: Zhang Chen <[email protected]>
Subject: Re: [Qemu-devel] [PATCH V2]net/colo-compare.c: Fix memory leak and code style issue.Quoted 19 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..ed349f5f6a100644net/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 12 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +8
-0+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) +1 );This assumes buf is NULL terminated (you pass
notify_rs->buf) which is not correct I think?
Yes, you are right.
How about this:
static bool packet_matches_str(const char *str,
uint8_t *buf,
uint32_t packet_len) {
if (packet_len != strlen(str) || !buf) {
return false;
}
return !memcmp(str, buf, strlen(str));
}Thanks
Zhang Chen
Quoted 44 lines
Thanks
net/colo-compare.c static void
Quoted 6 lines
net/colo-compare.c +2
-0+} +static void notify_remote_frame (CompareState *s) {char msg[] ="DO_CHECKPOINT" ;@@ -1008,21 +1019,24 @@ static void
compare_notify_rs_finalize(SocketReadState *notify_rs)Quoted 15 lines
net/colo-compare.c +4
-4{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);net/colo-compare.c static void compare_notify_rs_finalize(SocketReadState *notify_rs)
Quoted 10 lines
net/colo-compare.c +3
-3if (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 */
Quoted 7 lines
net/colo-compare.c +3
-0g_queue_foreach (&s->conn_list, colo_flush_packets, s); + }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } }
Quoted 46 lines · Chen
Quoted 32 lines
-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Tuesday, July 9, 2019 10:48 PM
To: Zhang, Chen <[email protected]>; Li Zhijian <[email protected]>; Peter Maydell <[email protected]>; qemu-dev <qemu- [email protected]>
Cc: Zhang Chen <[email protected]>
Subject: Re: [Qemu-devel] [PATCH V2]net/colo-compare.c: Fix memory leak and code style issue.Quoted 19 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..ed349f5f6a100644net/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 11 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +8
-0+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) +1 );This assumes buf is NULL terminated (you pass
notify_rs->buf) which is not correct I think?Yes, you are right.
How about this:
Quoted 6 lines
static bool packet_matches_str(const char *str,
uint8_t *buf, uint32_t packet_len) { if (packet_len != strlen(str) || !buf) { return false; }
When can we hit !buf?
Thanks
Quoted 51 lines
return !memcmp(str, buf, strlen(str)); }Thanks
Zhang ChenQuoted 43 lines
Thanks
net/colo-compare.c static void
Quoted 6 lines
net/colo-compare.c +2
-0+} +static void notify_remote_frame (CompareState *s) {char msg[] ="DO_CHECKPOINT" ;@@ -1008,21 +1019,24 @@ static void
compare_notify_rs_finalize(SocketReadState *notify_rs)Quoted 15 lines
net/colo-compare.c +4
-4{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);net/colo-compare.c static void compare_notify_rs_finalize(SocketReadState *notify_rs)
Quoted 10 lines
net/colo-compare.c +3
-3if (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 */
Quoted 7 lines
net/colo-compare.c +3
-0g_queue_foreach (&s->conn_list, colo_flush_packets, s); + }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } }
Quoted 67 lines
-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Thursday, July 11, 2019 4:18 AM
To: Zhang, Chen <[email protected]>; Li Zhijian <[email protected]>; Peter Maydell <[email protected]>; qemu-dev <qemu- [email protected]>
Cc: Zhang Chen <[email protected]>
Subject: Re: [Qemu-devel] [PATCH V2]net/colo-compare.c: Fix memory leak and code style issue.Quoted 46 lines · Chen
Quoted 32 lines
-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Tuesday, July 9, 2019 10:48 PM
To: Zhang, Chen <[email protected]>; Li Zhijian <[email protected]>; Peter Maydell <[email protected]>; qemu-dev <qemu- [email protected]>
Cc: Zhang Chen <[email protected]>
Subject: Re: [Qemu-devel] [PATCH V2]net/colo-compare.c: Fix memory leak and code style issue.Quoted 19 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..ed349f5f6a100644net/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 11 lines
net/colo-compare.c static int compare_chr_send(CompareState *s, · line 138
Quoted 8 lines
net/colo-compare.c +8
-0+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) +1 );This assumes buf is NULL terminated (you pass
notify_rs->buf) which is not correct I think?Yes, you are right.
How about this:Quoted 6 lines
static bool packet_matches_str(const char *str,
uint8_t *buf, uint32_t packet_len) { if (packet_len != strlen(str) || !buf) { return false; }
Quoted 1 line
When can we hit !buf?
I re-checked the code, looks the "net_fill_rstate()" can ensure buf is not empty, I will remove the !buf to send next version.
Thanks
Zhang Chen
Quoted 55 lines
Thanks
Quoted 51 lines
return !memcmp(str, buf, strlen(str)); }Thanks
Zhang ChenQuoted 44 lines
Thanks
Quoted 6 lines
net/colo-compare.c +2
-0+} +static void notify_remote_frame (CompareState *s) { char msg[] = "DO_CHECKPOINT"; @@ -1008,21 +1019,24 @@ staticvoid
compare_notify_rs_finalize(SocketReadState *notify_rs)Quoted 25 lines
{
CompareState *s = container_of(notify_rs, CompareState,notify_rs);net/colo-compare.c +7
-7-/* 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 Quoted 1 line
remove sec packet
*/
Quoted 7 lines
g_queue_foreach(&s->conn_list, colo_flush_packets, s);net/colo-compare.c +3
-0+ }else { +error_report ("COLO compare got unsupported instruction '%s'" , + (char *)notify_rs->buf); } }