Skip to contentSkip to conversation

cutils:change strtosz_suffix_unit function

PATCH Superseded liguang 3 participants 8 messages Dec 14, 2012 – Dec 17, 2012

v1 of 3 Superseded Latest is v3 →

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 |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
cutils.c +2 -1
index 4f0692f..da05c9e 100644
@@ -219,7 +219,7 @@ 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 = EINVAL;
     char *endptr;
     unsigned char c;
     int mul_required = 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 = ERANGE;
         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.

before change:
qemu-img: Invalid image size specified! You may use k, M, G or T suffixes for
qemu-img: kilobytes, megabytes, gigabytes and terabytes.

after change:
qemu-img: Image size must be less than 8 exabytes!

---
 qemu-img.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-img.c +6 -1
index e29e01b..1c3af67 100644
@@ -346,13 +346,18 @@ static int img_create(int argc, char **argv)
         int64_t sval;
         char *end;
         sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
-        if (sval < 0 || *end) {
+        if (sval == EINVAL || *end) {
             error_report("Invalid image size specified! You may use k, M, G or "
                   "T suffixes for ");
             error_report("kilobytes, megabytes, gigabytes and terabytes.");
             ret = -1;
             goto out;
         }
+        if (sval == ERANGE) {
+            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 29 lines · liguang

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

before change:
qemu-img: Invalid image size specified! You may use k, M, G or T suffixes for
qemu-img: kilobytes, megabytes, gigabytes and terabytes.

after change:
qemu-img: Image size must be less than 8 exabytes!

---
 qemu-img.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-img.c +1 -1
index e29e01b..1c3af67 100644
@@ -346,13 +346,18 @@ static int img_create(int argc, char **argv)
         int64_t sval;
         char *end;
         sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
-        if (sval < 0 || *end) {
+        if (sval == EINVAL || *end) {
             error_report("Invalid image size specified! You may use k, M, G or "
                   "T suffixes for ");
             error_report("kilobytes, megabytes, gigabytes and terabytes.");

qemu-img.c static int img_create(int argc, char **argv) · line 362

Quoted 10 lines
qemu-img.c +5 -0
             ret = -1;
             goto out;
         }
+        if (sval == ERANGE) {
+            error_report("Image size must be less than 8 exabytes!");
+            ret = -1;
+            goto out;
+        }
         img_size = (uint64_t)sval;
     }

If strtosz_suffix() ever acquires additional error codes, this caller will fail to detect the failure. Try something like

        if (sval < 0 || *end) {
            if (sval == -ERANGE) {
                error_report("Image size must be less than 8 exabytes!");
            } else {
                error_report("Invalid image size specified!"
                      " You may use k, M, G or T suffixes for");
                error_report("kilobytes, megabytes, gigabytes and terabytes.");
            }
            ret = -1;
            goto out;
        }

To be pedantically correct, "8 exabytes" should be "8 Exbibytes" or "8 EiB".

author
#3 lore raw

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

Quoted 57 lines · 在 2012-12-14五的 13:19 +0100,Markus Armbruster写道:
Quoted 29 lines · liguang

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

before change:
qemu-img: Invalid image size specified! You may use k, M, G or T suffixes for
qemu-img: kilobytes, megabytes, gigabytes and terabytes.

after change:
qemu-img: Image size must be less than 8 exabytes!

---
 qemu-img.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-img.c +1 -1
index e29e01b..1c3af67 100644
@@ -346,13 +346,18 @@ static int img_create(int argc, char **argv)
         int64_t sval;
         char *end;
         sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
-        if (sval < 0 || *end) {
+        if (sval == EINVAL || *end) {
             error_report("Invalid image size specified! You may use k, M, G or "
                   "T suffixes for ");
             error_report("kilobytes, megabytes, gigabytes and terabytes.");

qemu-img.c static int img_create(int argc, char **argv) · line 362

Quoted 10 lines
qemu-img.c +5 -0
             ret = -1;
             goto out;
         }
+        if (sval == ERANGE) {
+            error_report("Image size must be less than 8 exabytes!");
+            ret = -1;
+            goto out;
+        }
         img_size = (uint64_t)sval;
     }

If strtosz_suffix() ever acquires additional error codes, this caller will fail to detect the failure. Try something like

        if (sval < 0 || *end) {
            if (sval == -ERANGE) {
                error_report("Image size must be less than 8 exabytes!");
            } else {
                error_report("Invalid image size specified!"
                      " You may use k, M, G or T suffixes for");
                error_report("kilobytes, megabytes, gigabytes and terabytes.");
            }
            ret = -1;
            goto out;
        }
Quoted 2 lines

To be pedantically correct, "8 exabytes" should be "8 Exbibytes" or "8 EiB".

yes, it's better.
Thanks!

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

cutils.c

Quoted 13 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 |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
cutils.c +0 -0
index 4f0692f..da05c9e 100644

/*
* Convert string to bytes, allowing either B/b for bytes, K/k for KB,
* M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
* in *end, if not NULL. Return -1 on error.
*/

Quoted 10 lines
cutils.c +1 -1
@@ -219,7 +219,7 @@ 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 = EINVAL;
     char *endptr;
     unsigned char c;
     int mul_required = 0;

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

Quoted 7 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 = ERANGE;
         goto fail;
     }
     retval = val * mul;

Your error codes aren't negative, and you failed to update the function comment!

#5 lore raw
Quoted 11 lines · On Fri, 14 Dec 2012 13:09:17 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 9 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 |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
Quoted 9 lines

cutils.c

Quoted 4 lines
cutils.c +0 -0
index 4f0692f..da05c9e 100644

/*
* Convert string to bytes, allowing either B/b for bytes, K/k for KB,
* M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
* in *end, if not NULL. Return -1 on error.
*/

Size is not the only user of it, this function is used to convert KHz,... in target-i386/cpu.c, i.e. unit might be something else than 1024. That's why there is question about generalizing strtosz_suffix_unit() in future instead of duplicating suffixed int parsing. And specialization for size with unit=1024 and enforcing limits could be made in strtosz_suffix() which is used by current size users.

Quoted 12 lines
Quoted 12 lines
cutils.c +1 -1
@@ -219,7 +219,7 @@ 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 = EINVAL;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
Quoted 8 lines

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

Quoted 5 lines
cutils.c +1 -0
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = ERANGE;
         goto fail;
     }
     retval = val * mul;

Your error codes aren't negative, and you failed to update the function comment!

Returning negative here is fine for now form target-i386/cpu.c pov.

Signature · 2 lines
Regards,
  Igor
author
#6 lore raw
Quoted 51 lines · 在 2012-12-14五的 14:45 +0100,Igor Mammedov写道:
Quoted 11 lines · On Fri, 14 Dec 2012 13:09:17 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 9 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 |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
Quoted 9 lines

cutils.c

Quoted 4 lines
cutils.c +0 -0
index 4f0692f..da05c9e 100644

/*
* Convert string to bytes, allowing either B/b for bytes, K/k for KB,
* M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
* in *end, if not NULL. Return -1 on error.
*/

Size is not the only user of it, this function is used to convert KHz,... in target-i386/cpu.c, i.e. unit might be something else than 1024. That's why there is question about generalizing strtosz_suffix_unit() in future instead of duplicating suffixed int parsing. And specialization for size with unit=1024 and enforcing limits could be made in strtosz_suffix() which is used by current size users.

Quoted 12 lines
Quoted 12 lines
cutils.c +1 -1
@@ -219,7 +219,7 @@ 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 = EINVAL;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
Quoted 8 lines

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

Quoted 5 lines
cutils.c +1 -0
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = ERANGE;
         goto fail;
     }
     retval = val * mul;

Your error codes aren't negative, and you failed to update the function comment!

Quoted 1 line

Returning negative here is fine for now form target-i386/cpu.c pov.

OK

Signature · 2 lines
regards!
li guang
author
#7 lore raw
In reply to Markus Armbruster
Quoted 32 lines · 在 2012-12-14五的 13:09 +0100,Markus Armbruster写道:

cutils.c

Quoted 13 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 |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
cutils.c +0 -0
index 4f0692f..da05c9e 100644

/*
* Convert string to bytes, allowing either B/b for bytes, K/k for KB,
* M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
* in *end, if not NULL. Return -1 on error.
*/

Quoted 12 lines
cutils.c +1 -1
@@ -219,7 +219,7 @@ 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 = EINVAL;
     char *endptr;
     unsigned char c;
     int mul_required = 0;
@@ -246,6 +246,7 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
         goto fail;
     }
Quoted 8 lines

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

Quoted 5 lines
cutils.c +1 -0
     if ((val * mul >= INT64_MAX) || val < 0) {
+        retval = ERANGE;
         goto fail;
     }
     retval = val * mul;

Your error codes aren't negative, and you failed to update the function comment!

OK, will fix.

Signature · 2 lines
regards!
li guang