Skip to contentSkip to conversation

arch_init: Simplify code for load_xbzrle()

PATCH In review Chen Gang 3 participants 5 messages May 10, 2014 – May 19, 2014

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 arch_init: Simplify code for load_xbzrle()

For xbzrle_decode_buffer(), when decoding contents will exceed writing buffer, it will return -1, so need not check the return value whether large than writing buffer.

And when failure occurs within load_xbzrle(), it always return -1 without any resources which need release.

So can remove the related checking statements, and also can remove 'rc' and 'ret' local variables,

---
 arch_init.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
arch_init.c +4 -10
index 60c975d..98ee5b6 100644
@@ -908,7 +908,6 @@ static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
 
 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
 {
-    int ret, rc = 0;
     unsigned int xh_len;
     int xh_flags;
 
@@ -933,18 +932,13 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
     qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
 
     /* decode RLE */
-    ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
-                               TARGET_PAGE_SIZE);
-    if (ret == -1) {
+    if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
+                             TARGET_PAGE_SIZE) == -1) {
         fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
-        rc = -1;
-    } else  if (ret > TARGET_PAGE_SIZE) {
-        fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
-                ret, TARGET_PAGE_SIZE);
-        abort();
+        return -1;
     }
 
-    return rc;
+    return 0;
 }
 
 static inline void *host_from_stream_offset(QEMUFile *f,
Signature · 1 line
1.7.11.7
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Chen Gang

For xbzrle_decode_buffer(), when decoding contents will exceed writing buffer, it will return -1, so need not check the return value whether large than writing buffer.

Quoted 8 lines

And when failure occurs within load_xbzrle(), it always return -1 without any resources which need release.

So can remove the related checking statements, and also can remove 'rc' and 'ret' local variables,

#2 lore raw
In reply to Chen Gang
Quoted 9 lines · Chen Gang

For xbzrle_decode_buffer(), when decoding contents will exceed writing buffer, it will return -1, so need not check the return value whether large than writing buffer.

And when failure occurs within load_xbzrle(), it always return -1 without any resources which need release.

So can remove the related checking statements, and also can remove 'rc' and 'ret' local variables,

Just one comment below.

arch_init.c static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) · line 936

Quoted 9 lines
arch_init.c +2 -3
@@ -933,18 +932,13 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
     qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
 
     /* decode RLE */
-    ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
-                               TARGET_PAGE_SIZE);
-    if (ret == -1) {
+    if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
+                             TARGET_PAGE_SIZE) == -1) {

Can we compare like '< 0' here, not like '== -1' ?
Are there any other possible return values from xbzrle_decode_buffer() which are less than zero but non-error?

To me, anything less than zero is always error (unless it is one of the possible non-error values, like offset for example which can be negative).

Especially having in mind that in the future, some function may extend its error return to include the actual error code (like -errno), in which case code which compares with -1 will not work anymore.

Is it okay to me to apply this with s/== -1/< 0/ ?

Thanks,

/mjt

author
#3 lore raw
Quoted 16 lines · Michael Tokarev
Quoted 9 lines · Chen Gang

For xbzrle_decode_buffer(), when decoding contents will exceed writing buffer, it will return -1, so need not check the return value whether large than writing buffer.

And when failure occurs within load_xbzrle(), it always return -1 without any resources which need release.

So can remove the related checking statements, and also can remove 'rc' and 'ret' local variables,

Just one comment below.

Quoted 3 lines
(quoted diff) +0 -0
@@ -933,18 +932,13 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
     qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
 
Quoted 8 lines

Review static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) · line 936

Quoted 6 lines
(quoted diff) +2 -3
     /* decode RLE */
-    ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
-                               TARGET_PAGE_SIZE);
-    if (ret == -1) {
+    if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
+                             TARGET_PAGE_SIZE) == -1) {

Can we compare like '< 0' here, not like '== -1' ?

That's fine to me.

Quoted 3 lines

Are there any other possible return values from xbzrle_decode_buffer() which are less than zero but non-error?

Although, at present, when it fails, it will only return -1.

Quoted 3 lines

To me, anything less than zero is always error (unless it is one of the possible non-error values, like offset for example which can be negative).

That sounds reasonable to me, too.

Quoted 4 lines

Especially having in mind that in the future, some function may extend its error return to include the actual error code (like -errno), in which case code which compares with -1 will not work anymore.

Yeah, in the future, it may do.

Quoted 2 lines

Is it okay to me to apply this with s/== -1/< 0/ ?

At least, it is OK to me.

BTW: the related test code for xbzrle_decode_buffer() may also need improved (although, after read through, I still don't known what it really want to do).

tests/test-xbzrle.c +1 -1
index db93b0a..c8b4e58 100644
@@ -162,7 +162,7 @@ static void encode_decode_range(void)
                                 PAGE_SIZE);
 
     rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
-    g_assert(rc < PAGE_SIZE);
+    g_assert(rc < PAGE_SIZE && rc >= 0);
     g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
 
     g_free(buffer);

Please help check when you have time. If necessary, I shall send related patch for it. (this fix may be still incorrect, if so, please help send related patch for it, and welcome to mark me as Reported-by for it).

Thanks.

Signature · 3 lines
Chen Gang

Open, share and attitude like air, water and life which God blessed
#4 lore raw

Meanwhile the original version has been merged from juanquintela/tags/migration/20140515 in the original form, so the point is moot already.

Juan, can you please indicate that you applied something, instead of just giving a Reviewed-by?

Thanks,

/mjt

Quoted 79 lines · Chen Gang
Quoted 16 lines · Michael Tokarev
Quoted 9 lines · Chen Gang

For xbzrle_decode_buffer(), when decoding contents will exceed writing buffer, it will return -1, so need not check the return value whether large than writing buffer.

And when failure occurs within load_xbzrle(), it always return -1 without any resources which need release.

So can remove the related checking statements, and also can remove 'rc' and 'ret' local variables,

Just one comment below.

Quoted 3 lines
(quoted diff) +0 -0
@@ -933,18 +932,13 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
     qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
 
Quoted 8 lines

Review static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) · line 936

Quoted 6 lines
(quoted diff) +2 -3
     /* decode RLE */
-    ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
-                               TARGET_PAGE_SIZE);
-    if (ret == -1) {
+    if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
+                             TARGET_PAGE_SIZE) == -1) {

Can we compare like '< 0' here, not like '== -1' ?

That's fine to me.

Quoted 3 lines

Are there any other possible return values from xbzrle_decode_buffer() which are less than zero but non-error?

Although, at present, when it fails, it will only return -1.

Quoted 3 lines

To me, anything less than zero is always error (unless it is one of the possible non-error values, like offset for example which can be negative).

That sounds reasonable to me, too.

Quoted 4 lines

Especially having in mind that in the future, some function may extend its error return to include the actual error code (like -errno), in which case code which compares with -1 will not work anymore.

Yeah, in the future, it may do.

Quoted 2 lines

Is it okay to me to apply this with s/== -1/< 0/ ?

At least, it is OK to me.

BTW: the related test code for xbzrle_decode_buffer() may also need improved (although, after read through, I still don't known what it really want to do).

tests/test-xbzrle.c +1 -1
index db93b0a..c8b4e58 100644
@@ -162,7 +162,7 @@ static void encode_decode_range(void)
                                 PAGE_SIZE);
 
     rc = xbzrle_decode_buffer(compressed, dlen, test, PAGE_SIZE);
-    g_assert(rc < PAGE_SIZE);
+    g_assert(rc < PAGE_SIZE && rc >= 0);
     g_assert(memcmp(test, buffer, PAGE_SIZE) == 0);
 
     g_free(buffer);

Please help check when you have time. If necessary, I shall send related patch for it. (this fix may be still incorrect, if so, please help send related patch for it, and welcome to mark me as Reported-by for it).

Thanks.