strtosz() cleanups
v4 cover strtosz() cleanups
From: Jes Sorensen <[email protected]>
Hi,
Here is an updated version of the strtosz() fixes that were discussed earlier. Per discussing with Anthony on irc, he is ok with them going in like this.
This is a respin to fix a patch conflict in the block tree, and it pulls the two patch sets into one set of four patches instead. No functional change.
Cheers,
Jes
Jes Sorensen (4):
strtosz(): use unsigned char and switch to qemu_isspace() strtosz() use qemu_toupper() to simplify switch statement strtosz(): Fix name confusion in use of modf() strtosz(): Use suffix macros in switch() statement
cutils.c | 28 ++++++++++++---------------- 1 files changed, 12 insertions(+), 16 deletions(-)
Signature · 1 line
1.7.3.4
PATCH v4 1/4 strtosz(): use unsigned char and switch to qemu_isspace()
From: Jes Sorensen <[email protected]>
isspace() behavior is undefined for signed char.
Bug pointed out by Eric Blake, thanks!
--- cutils.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
cutils.c +4 -3
@@ -294,7 +294,8 @@ int fcntl_setfl(int fd, int flag)int64_t strtosz_suffix (const char *nptr,char **end,const char default_suffix) {int64_t retval =-1 ; -char *endptr, c, d; +char *endptr; +unsigned char c, d;int mul_required =0 ;double val, mul, integral, fraction;
@@ -314,7 +315,7 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)*/ c = *endptr; d = c; -if (isspace (c) || c =='\0' || c ==',' ) { +if (qemu_isspace (c) || c =='\0' || c ==',' ) { c =0 ;if (default_suffix) { d = default_suffix;
@@ -361,7 +362,7 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)*/ if (c !=0 ) { endptr++; -if (!isspace (*endptr) && *endptr !=',' && *endptr !=0 ) { +if (!qemu_isspace (*endptr) && *endptr !=',' && *endptr !=0 ) { goto fail; } }
Signature · 1 line
1.7.3.4
Re: [Qemu-devel] [PATCH 1/4] strtosz(): use unsigned char and switch to qemu_isspace()
Quoted 16 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
isspace()behavior is undefined for signed char.Bug pointed out by Eric Blake, thanks!
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)cutils.c +0
-0
cutils.c int fcntl_setfl(int fd, int flag) · line 301
Quoted 9 lines
cutils.c +2
-1@@ -294,7 +294,8 @@ int fcntl_setfl(int fd, int flag)int64_t strtosz_suffix (const char *nptr,char **end,const char default_suffix) {int64_t retval =-1 ; -char *endptr, c, d; +char *endptr; +unsigned char c, d;int mul_required =0 ;double val, mul, integral, fraction;
I doubt this hunk is still needed.
Quoted 18 lines
cutils.c +2
-2@@ -314,7 +315,7 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)*/ c = *endptr; d = c; -if (isspace (c) || c =='\0' || c ==',' ) { +if (qemu_isspace (c) || c =='\0' || c ==',' ) { c =0 ;if (default_suffix) { d = default_suffix;@@ -361,7 +362,7 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)*/ if (c !=0 ) { endptr++; -if (!isspace (*endptr) && *endptr !=',' && *endptr !=0 ) { +if (!qemu_isspace (*endptr) && *endptr !=',' && *endptr !=0 ) { goto fail; } }
Re: [Qemu-devel] [PATCH 1/4] strtosz(): use unsigned char and switch to qemu_isspace()
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
isspace()behavior is undefined for signed char.Bug pointed out by Eric Blake, thanks!
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
Quoted 16 lines
Quoted 5 lines
cutils.c +0
-0cutils.c int fcntl_setfl(int fd, int flag) · line 301
Quoted 9 lines
cutils.c +2
-1@@ -294,7 +294,8 @@ int fcntl_setfl(int fd, int flag)int64_t strtosz_suffix (const char *nptr,char **end,const char default_suffix) {int64_t retval =-1 ; -char *endptr, c, d; +char *endptr; +unsigned char c, d;int mul_required =0 ;double val, mul, integral, fraction;I doubt this hunk is still needed.
It isn't strictly due to qemu_toupper() but it is prettier to match the real behavior of pure toupper() anyway.
Jes
Re: [Qemu-devel] [PATCH 1/4] strtosz(): use unsigned char and switch to qemu_isspace()
Quoted 31 lines · Jes Sorensen
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
isspace()behavior is undefined for signed char.Bug pointed out by Eric Blake, thanks!
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)Quoted 16 lines
Quoted 5 lines
cutils.c +0
-0cutils.c int fcntl_setfl(int fd, int flag) · line 301
Quoted 9 lines
cutils.c +2
-1@@ -294,7 +294,8 @@ int fcntl_setfl(int fd, int flag)int64_t strtosz_suffix (const char *nptr,char **end,const char default_suffix) {int64_t retval =-1 ; -char *endptr, c, d; +char *endptr; +unsigned char c, d;int mul_required =0 ;double val, mul, integral, fraction;I doubt this hunk is still needed.
Quoted 2 lines
It isn't strictly due to
qemu_toupper()but it is prettier to match the real behavior of puretoupper()anyway.
Frequent casting between signed and unsigned has been shown to cause headaches in laboratory rats.
PATCH v4 2/4 strtosz() use qemu_toupper() to simplify switch statement
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- cutils.c | 7 +------ 1 files changed, 1 insertions(+), 6 deletions(-)
cutils.c +1 -6
@@ -323,16 +323,14 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) d = c; } } -switch (d) { +switch (qemu_toupper (d)) {case 'B' : -case 'b' : mul =1 ;if (mul_required) { goto fail; }break ;case 'K' : -case 'k' : mul =1 <<10 ;break ;case 0 :
@@ -340,15 +338,12 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) goto fail; }case 'M' : -case 'm' : mul =1ULL <<20 ;break ;case 'G' : -case 'g' : mul =1ULL <<30 ;break ;case 'T' : -case 't' : mul =1ULL <<40 ;break ;default :
Signature · 1 line
1.7.3.4
PATCH v4 3/4 strtosz(): Fix name confusion in use of modf()
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- cutils.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
cutils.c +2 -2
@@ -304,8 +304,8 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)if (isnan (val) || endptr == nptr || errno !=0 ) { goto fail; } - integral =modf (val, &fraction); -if (integral !=0 ) { + fraction =modf (val, &integral); +if (fraction !=0 ) { mul_required =1 ; }/*
Signature · 1 line
1.7.3.4
PATCH v4 4/4 strtosz(): Use suffix macros in switch() statement
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- cutils.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
cutils.c +5 -5
@@ -324,26 +324,26 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) } }switch (qemu_toupper (d)) { -case 'B' : +case STRTOSZ_DEFSUFFIX_B : mul =1 ;if (mul_required) { goto fail; }break ; -case 'K' : +case STRTOSZ_DEFSUFFIX_KB : mul =1 <<10 ;break ;case 0 :if (mul_required) { goto fail; } -case 'M' : +case STRTOSZ_DEFSUFFIX_MB : mul =1ULL <<20 ;break ; -case 'G' : +case STRTOSZ_DEFSUFFIX_GB : mul =1ULL <<30 ;break ; -case 'T' : +case STRTOSZ_DEFSUFFIX_TB : mul =1ULL <<40 ;break ;default :
Signature · 1 line
1.7.3.4
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 33 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)cutils.c +3
-3@@ -324,26 +324,26 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) } }switch (qemu_toupper (d)) { -case 'B' : +case STRTOSZ_DEFSUFFIX_B : mul =1 ;if (mul_required) { goto fail; }break ; -case 'K' : +case STRTOSZ_DEFSUFFIX_KB : mul =1 <<10 ;break ;case 0 :if (mul_required) { goto fail; } -case 'M' : +case STRTOSZ_DEFSUFFIX_MB : mul =1ULL <<20 ;
cutils.c int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) · line 349
Quoted 10 lines
cutils.c +2
-2break ; -case 'G' : +case STRTOSZ_DEFSUFFIX_GB : mul =1ULL <<30 ;break ; -case 'T' : +case STRTOSZ_DEFSUFFIX_TB : mul =1ULL <<40 ;break ;default :
Phony abstraction. And it leaks: code here assumes the STRTOSZ_DEFSUFFIX_T* are all upper case.
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 40 lines · Markus Armbruster
Quoted 38 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)cutils.c +4
-4@@ -324,26 +324,26 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) } }switch (qemu_toupper (d)) { -case 'B' : +case STRTOSZ_DEFSUFFIX_B : mul =1 ;if (mul_required) { goto fail; }break ; -case 'K' : +case STRTOSZ_DEFSUFFIX_KB : mul =1 <<10 ;break ;case 0 :if (mul_required) { goto fail; } -case 'M' : +case STRTOSZ_DEFSUFFIX_MB : mul =1ULL <<20 ;break ; -case 'G' : +case STRTOSZ_DEFSUFFIX_GB : mul =1ULL <<30 ;break ;
Quoted 8 lines
cutils.c int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) · line 349
Quoted 5 lines
cutils.c +1
-1-case 'T' : +case STRTOSZ_DEFSUFFIX_TB : mul =1ULL <<40 ;break ;default :Phony abstraction. And it leaks: code here assumes the STRTOSZ_DEFSUFFIX_T* are all upper case.
qemu_toupper() - whats the problem?
Jes
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 50 lines · Jes Sorensen
Quoted 40 lines · Markus Armbruster
Quoted 38 lines · [email protected] writes:
From: Jes Sorensen <[email protected]>
Signed-off-by Jes Sorensen <[email protected]>--- cutils.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)cutils.c +4
-4@@ -324,26 +324,26 @@ int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) } }switch (qemu_toupper (d)) { -case 'B' : +case STRTOSZ_DEFSUFFIX_B : mul =1 ;if (mul_required) { goto fail; }break ; -case 'K' : +case STRTOSZ_DEFSUFFIX_KB : mul =1 <<10 ;break ;case 0 :if (mul_required) { goto fail; } -case 'M' : +case STRTOSZ_DEFSUFFIX_MB : mul =1ULL <<20 ;break ; -case 'G' : +case STRTOSZ_DEFSUFFIX_GB : mul =1ULL <<30 ;break ;Quoted 8 lines
cutils.c int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) · line 349
Quoted 5 lines
cutils.c +1
-1-case 'T' : +case STRTOSZ_DEFSUFFIX_TB : mul =1ULL <<40 ;break ;default :Phony abstraction. And it leaks: code here assumes the STRTOSZ_DEFSUFFIX_T* are all upper case.
Quoted 1 line
qemu_toupper()- whats the problem?
If a STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 11 lines · Markus Armbruster
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
+ case
STRTOSZ_DEFSUFFIX_TB:Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
mul = 1ULL << 40; break; default:Phony abstraction. And it leaks: code here assumes the STRTOSZ_DEFSUFFIX_T* are all upper case.
qemu_toupper()- whats the problem?If a
STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.
Right, so one has to be careful when adding new suffix constants. However given that we already have all the likely to be used ones for the near future, that isn't exactly a big issue.
On the other hand forcing the use of the macros makes it less likely that someone specifies an unsupported constant by hitting 'y' instead of 't' or similar.
Jes
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 6 lines · Jes Sorensen
Quoted 5 lines · Markus Armbruster
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+ case
STRTOSZ_DEFSUFFIX_TB:Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
mul = 1ULL << 40; break; default:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Phony abstraction. And it leaks: code here assumes the STRTOSZ_DEFSUFFIX_T* are all upper case.
qemu_toupper()- whats the problem?If a
STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.Right, so one has to be careful when adding new suffix constants.
Calls for a comment right next to the definition of the STRTOSZ_DEFSUFFIX_T*.
I hate unstated restrictions that are hidden far away from the place where you can break them.
Quoted 6 lines
However given that we already have all the likely to be used ones for the near future, that isn't exactly a big issue.
On the other hand forcing the use of the macros makes it less likely that someone specifies an unsupported constant by hitting 'y' instead of 't' or similar.
Takes a combination of butterfingers, cross-eyedness, and near-total incompetence at basic smoke-testing.
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 5 lines · Markus Armbruster
Quoted 4 lines · Jes Sorensen
Quoted 3 lines
Quoted 1 line
Quoted 1 line
qemu_toupper()- whats the problem?If a
STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.
Quoted 7 lines
Quoted 1 line
Right, so one has to be careful when adding new suffix constants.
Calls for a comment right next to the definition of the STRTOSZ_DEFSUFFIX_T*.
I hate unstated restrictions that are hidden far away from the place where you can break them.
Well I am fine with a comment in the code.
Quoted 9 lines
Quoted 6 lines
However given that we already have all the likely to be used ones for the near future, that isn't exactly a big issue.
On the other hand forcing the use of the macros makes it less likely that someone specifies an unsupported constant by hitting 'y' instead of 't' or similar.
Takes a combination of butterfingers, cross-eyedness, and near-total incompetence at basic smoke-testing.
Not really, all it takes is someone writing a piece of code, not thinking about it, therefore only testing things where a suffix is specified as an argument and it gets missed.
Jes
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 8 lines · Jes Sorensen
Quoted 7 lines · Markus Armbruster
Quoted 5 lines · Jes Sorensen
Quoted 3 lines
Quoted 1 line
Quoted 1 line
qemu_toupper()- whats the problem?If a
STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.Right, so one has to be careful when adding new suffix constants.
Quoted 7 lines
Quoted 5 lines
Calls for a comment right next to the definition of the STRTOSZ_DEFSUFFIX_T*.
I hate unstated restrictions that are hidden far away from the place where you can break them.
Well I am fine with a comment in the code.
Such a comment improves it from "wrong" to merely "ugly". I can live with that.
Thanks.
[...]
Re: [Qemu-devel] [PATCH 4/4] strtosz(): Use suffix macros in switch() statement
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Jes Sorensen
Quoted 10 lines · Markus Armbruster
Quoted 5 lines · Jes Sorensen
Quoted 3 lines
Quoted 1 line
Quoted 1 line
qemu_toupper()- whats the problem?If a
STRTOSZ_DEFSUFFIX_T? expands to a lower case character, its case will not match any input.Right, so one has to be careful when adding new suffix constants.
Calls for a comment right next to the definition of the STRTOSZ_DEFSUFFIX_T*.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I hate unstated restrictions that are hidden far away from the place where you can break them.
Well I am fine with a comment in the code.
Such a comment improves it from "wrong" to merely "ugly". I can live with that.
I realize that you view it as such, in other eyes it goes from hackish to correct ... there is zero point in arguing over this, we can just agree to disagree.
Jes
Kevin Wolf
AppliedAm 24.01.2011 16:33, schrieb [email protected]:
Quoted 16 lines
From: Jes Sorensen <[email protected]>
Hi,
Here is an updated version of the
strtosz()fixes that were discussed earlier. Per discussing with Anthony on irc, he is ok with them going in like this.This is a respin to fix a patch conflict in the block tree, and it pulls the two patch sets into one set of four patches instead. No functional change.
Cheers,
Jes
Quoted 9 lines
Jes Sorensen (4):
strtosz(): use unsigned char and switch toqemu_isspace()strtosz()useqemu_toupper()to simplify switch statementstrtosz(): Fix name confusion in use ofmodf()strtosz(): Use suffix macros inswitch()statementcutils.c | 28 ++++++++++++---------------- 1 files changed, 12 insertions(+), 16 deletions(-)
Thanks, applied all to the block branch.
Kevin
Am 24.01.2011 16:45, schrieb Kevin Wolf:
Quoted 23 lines
Am 24.01.2011 16:33, schrieb [email protected]:
Quoted 22 lines
From: Jes Sorensen <[email protected]>
Hi,
Here is an updated version of the
strtosz()fixes that were discussed earlier. Per discussing with Anthony on irc, he is ok with them going in like this.This is a respin to fix a patch conflict in the block tree, and it pulls the two patch sets into one set of four patches instead. No functional change.
Cheers,
JesJes Sorensen (4):
strtosz(): use unsigned char and switch toqemu_isspace()strtosz()useqemu_toupper()to simplify switch statementstrtosz(): Fix name confusion in use ofmodf()strtosz(): Use suffix macros inswitch()statement
Quoted 7 lines
Quoted 3 lines
cutils.c| 28 ++++++++++++----------------1 files changed, 12 insertions(+), 16 deletions(-)Thanks, applied all to the block branch.
Kevin
There was some discussion regarding this patch set. I agree with Markus that part of the first patch should be removed: don't change char to unsigned char.
It's not necessary, and the result is, that now unsigned chars are assigned to chars which might raise future compiler warnings.
Stefan
Quoted 3 lines · Stefan Weil
There was some discussion regarding this patch set. I agree with Markus that part of the first patch should be removed: don't change char to unsigned char.
The unsigned char should definitely go in, leaving it as a signed char doesn't serve any purpose.
Quoted 3 lines
It's not necessary, and the result is, that now unsigned chars are assigned to chars which might raise future compiler warnings.
We already do that cast by calling qemu_toupper() and qemu_isspace() so that argument is void.
Jes
Quoted 7 lines · Jes Sorensen
Quoted 3 lines · Stefan Weil
There was some discussion regarding this patch set. I agree with Markus that part of the first patch should be removed: don't change char to unsigned char.
The unsigned char should definitely go in, leaving it as a signed char doesn't serve any purpose.
Leaving something as is doesn't need justification. Changing it does. The justification presented so far was "it is prettier to match the real behavior of pure toupper()". Which I don't buy. But without commit access, I'm not a buyer.
[...]
Quoted 7 lines · Markus Armbruster
Quoted 5 lines · Jes Sorensen
Quoted 3 lines · Stefan Weil
There was some discussion regarding this patch set. I agree with Markus that part of the first patch should be removed: don't change char to unsigned char.
Quoted 7 lines
Quoted 2 lines
The unsigned char should definitely go in, leaving it as a signed char doesn't serve any purpose.
Leaving something as is doesn't need justification. Changing it does. The justification presented so far was "it is prettier to match the real behavior of pure
toupper()". Which I don't buy. But without commit access, I'm not a buyer.
Well that is just too bad. qemu_toupper() is a hack around the fact that people often forget to use the right type, it is not an excuse for using the wrong type in the code.
Jes
Quoted 11 lines · Jes Sorensen
Quoted 10 lines · Markus Armbruster
Quoted 7 lines · Jes Sorensen
Quoted 3 lines · Stefan Weil
There was some discussion regarding this patch set. I agree with Markus that part of the first patch should be removed: don't change char to unsigned char.
The unsigned char should definitely go in, leaving it as a signed char doesn't serve any purpose.
Quoted 8 lines
Quoted 4 lines
Leaving something as is doesn't need justification. Changing it does. The justification presented so far was "it is prettier to match the real behavior of pure
toupper()". Which I don't buy. But without commit access, I'm not a buyer.Well that is just too bad.
qemu_toupper()is a hack around the fact that people often forget to use the right type, it is not an excuse for using the wrong type in the code.
qemu_toupper() is designed to work correctly for any character argument, be it signed or unsigned.
The fact that toupper() works only for unsigned character values is irrelevant for qemu_toupper().
The fact that qemu_toupper()'s implementation uses toupper() is an implementation detail.