Skip to contentSkip to conversation

cutils:change strtosz_suffix_unit function

PATCH In review liguang 5 participants 12 messages Dec 7, 2012 – Dec 13, 2012

lore
Share & tools
lore

liguang

author
#0 lore raw

PATCH 1/2 cutils:change strtosz_suffix_unit function

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
cutils.c +3 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;
         goto fail;
     }
     retval = val * mul;
Signature · 1 line
1.7.2.5
author
#1 lore raw

PATCH 2/2 qemu-img:report size overflow error message

qemu-img will complain when qcow or qcow2
size overflow for 64 bits, report the right
message in this condition.

---
 qemu-img.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
qemu-img.c +5 -0
index e29e01b..f3209b4 100644
@@ -353,6 +353,11 @@ static int img_create(int argc, char **argv)
             ret = -1;
             goto out;
         }
+        if (sval == 0) {
+            error_report("Image size must be less than 8 exabytes!");
+            ret = -1;
+            goto out;
+        }
         img_size = (uint64_t)sval;
     }
 
Signature · 1 line
1.7.2.5
#2 lore raw

Re: [Qemu-devel] [ [PATCH 2/2] qemu-img:report size overflow error message

Quoted 3 lines · liguang

qemu-img will complain when qcow or qcow2
size overflow for 64 bits, report the right
message in this condition.

Recommend to show the reproducer with output before and after the patch in the commit message.

author
#3 lore raw

Re: [Qemu-devel] [ [PATCH 2/2] qemu-img:report size overflow error message

Quoted 8 lines · 在 2012-12-11二的 16:11 +0100,Markus Armbruster写道:
Quoted 3 lines · liguang

qemu-img will complain when qcow or qcow2
size overflow for 64 bits, report the right
message in this condition.

Recommend to show the reproducer with output before and after the patch in the commit message.

OK, reasonable.
Thanks!

Signature · 2 lines
regards!
li guang
#4 lore raw
In reply to liguang
Quoted 8 lines · liguang

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

I don't understand what this patch is supposed to do.

Why change the type of mul from double to int64_t?

Are you using retval = 0 as a special value to indicate overflow? Then the new return value should be documented. But it would be better to change the function to return -errno values instead of 0/-1 so the caller knows the reason for specific failure cases (e.g. -E2BIG).

Should the val < 0 case be checked earlier in the function? It seems like a different failure case then INT64_MAX overflow.

Quoted 30 lines
cutils.c +3 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;
         goto fail;
     }
     retval = val * mul;
Signature · 1 line
1.7.2.5
#5 lore raw
Quoted 10 lines · Stefan Hajnoczi
Quoted 8 lines · liguang

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
Quoted 6 lines

I don't understand what this patch is supposed to do.

Why change the type of mul from double to int64_t?

Are you using retval = 0 as a special value to indicate overflow? Then the new return value should be documented.

Zero is a legitimate value, so it cannot be used to indicate overflow.

Quoted 3 lines

But it would be better to
change the function to return -errno values instead of 0/-1 so the caller knows the reason for specific failure cases (e.g. -E2BIG).

The appropriate error code is ERANGE, following strtol() precedence.

Nitpick: function doesn't return zero on succes, it returns a non-negative value.

Quoted 2 lines

Should the val < 0 case be checked earlier in the function? It seems like a different failure case then INT64_MAX overflow.

Only if callers are interested in differentiating between under- and overflow. Which I doubt.

Quoted 30 lines
Quoted 30 lines
cutils.c +3 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;
         goto fail;
     }
     retval = val * mul;
Signature · 1 line
1.7.2.5
author
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · 在 2012-12-11二的 10:52 +0100,Stefan Hajnoczi写道:
Quoted 8 lines · liguang

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

I don't understand what this patch is supposed to do.

little change to let the caller know the translated value overflow

Quoted 2 lines

Why change the type of mul from double to int64_t?

mul is multiplier generated by suffix_mul(return value type int64_t) so double is incorrect.

Quoted 2 lines

Are you using retval = 0 as a special value to indicate overflow?

yes, just an indicator for caller, not so normal return value

Quoted 4 lines

Then
the new return value should be documented. But it would be better to change the function to return -errno values instead of 0/-1 so the caller knows the reason for specific failure cases (e.g. -E2BIG).

because of 'retval = 0' is only a hacking way to reflect an illegal translated value, does it have to act in such an 'official' way?

Quoted 3 lines

Should the val < 0 case be checked earlier in the function? It seems like a different failure case then INT64_MAX overflow.

maybe, but I think no one interested in difference between under-overflow

Quoted 31 lines
Quoted 31 lines
cutils.c +3 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;
         goto fail;
     }
     retval = val * mul;
Signature · 1 line
1.7.2.5
Signature · 2 lines
regards!
li guang
#7 lore raw
Quoted 6 lines · li guang
Quoted 5 lines · 在 2012-12-11二的 10:52 +0100,Stefan Hajnoczi写道:
Quoted 4 lines · liguang

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

I don't understand what this patch is supposed to do.

little change to let the caller know the translated value overflow

Using a negative errno will make the intent clear.

Quoted 5 lines
Quoted 2 lines

Why change the type of mul from double to int64_t?

mul is multiplier generated by suffix_mul(return value type int64_t) so double is incorrect.

Okay, thanks. I asked because I wondered whether it had something to do with overflow detection. Please put unrelated changes in separate patches - and if you feel it's not worth putting in a separate patch then maybe it's not worth changing at all.

Stefan

#8 lore raw
In reply to liguang
Quoted 28 lines · On Fri, 7 Dec 2012 11:49:49 +0800 liguang <[email protected]> wrote:

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
cutils.c +2 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
     errno = 0;
     val = strtod(nptr, &endptr);

cutils.c int64_t strtosz_suffix_unit(const char *nptr, char · line 249

Quoted 4 lines
cutils.c +1 -0
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char
 **end, goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;

Why not to add Error argument to return errors instead of using return value? That way function would be easier to generalize in future to handle whole INT64 range. And callers would check only returned error instead of return value or if 'end' == 0.

Quoted 3 lines
cutils.c +0 -0
         goto fail;
     }
     retval = val * mul;
#9 lore raw
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Fri, 7 Dec 2012 11:49:49 +0800 liguang <[email protected]> wrote:

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
cutils.c +2 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
Quoted 10 lines

cutils.c int64_t strtosz_suffix_unit(const char *nptr, char · line 249

Quoted 7 lines
cutils.c +1 -0
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char
 **end, goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;

Why not to add Error argument to return errors instead of using return value? That way function would be easier to generalize in future to handle whole INT64 range.

Generalize when you have a user, not earlier.

Quoted 2 lines

And callers would check only returned error instead of return
value or if 'end' == 0.

Checking the return value is sufficient now. What makes you think you have to check end, too?

#10 lore raw
Quoted 41 lines · On Thu, 13 Dec 2012 09:03:50 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Fri, 7 Dec 2012 11:49:49 +0800 liguang <[email protected]> wrote:

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
cutils.c +2 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
Quoted 10 lines

cutils.c int64_t strtosz_suffix_unit(const char *nptr, char · line 249

Quoted 7 lines
cutils.c +1 -0
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char
 **end, goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;

Why not to add Error argument to return errors instead of using return value? That way function would be easier to generalize in future to handle whole INT64 range.

Quoted 1 line

Generalize when you have a user, not earlier.

http://permalink.gmane.org/gmane.comp.emulators.qemu/183792

Quoted 6 lines
Quoted 3 lines

And callers would check only returned error instead of return
value or if 'end' == 0.

Checking the return value is sufficient now. What makes you think you have to check end, too?

I've meant *endptr == "\0" check in several callers:
opts_type_size(), img_create(), numa_add().

Signature · 2 lines
Regards,
  Igor
#11 lore raw
Quoted 33 lines · Igor Mammedov
Quoted 30 lines · On Thu, 13 Dec 2012 09:03:50 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Fri, 7 Dec 2012 11:49:49 +0800 liguang <[email protected]> wrote:

if value to be translated is larger than INT64_MAX, this function will not be convenient for caller to be aware of it, so change a little for this.

---
 cutils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
cutils.c +2 -2
index 4f0692f..8905b5e 100644
@@ -219,11 +219,11 @@ static int64_t suffix_mul(char suffix, int64_t unit)
 int64_t strtosz_suffix_unit(const char *nptr, char **end,
                             const char default_suffix, int64_t unit)
 {
-    int64_t retval = -1;
+    int64_t retval = -1, mul;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
-    double val, mul, integral, fraction;
+    double val, integral, fraction;
 
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

cutils.c int64_t strtosz_suffix_unit(const char *nptr, char · line 249

Quoted 7 lines
cutils.c +1 -0
     errno = 0;
     val = strtod(nptr, &endptr);
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char
 **end, goto fail;
     }
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = 0;

Why not to add Error argument to return errors instead of using return value?
That way function would be easier to generalize in future to handle whole INT64 range.

This function parses *sizes*. Necessarily non-negative. It returns them as int64_t. Fine for sizes up to 2^63-1 bytes.

Quoted 2 lines
Quoted 1 line

Generalize when you have a user, not earlier.

http://permalink.gmane.org/gmane.comp.emulators.qemu/183792

Why does that user require sizes in [2^63..2^64-1]?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

And callers would check only returned error instead of return
value or if 'end' == 0.

Checking the return value is sufficient now. What makes you think you have to check end, too?

I've meant *endptr == "\0" check in several callers:
opts_type_size(), img_create(), numa_add().

That check tests something else, namely "no junk follows the number". You cannot put that test into strtosz_suffix_unit(), because different callers accept numbers in different contexts.