strtoul_ui: reject negative values
PATCH 1/1 strtoul_ui: reject negative values
strtoul_ui uses strtoul to get a long unsigned, then checks that casting to unsigned does not lose information and return the casted value.
On 64 bits architecture, checking that the cast does not change the value catches most errors, but when sizeof(int) == sizeof(long) (e.g. i386), the check does nothing. Unfortunately, strtoul silently accepts negative values, and as a result strtoul_ui("-1", ...) raised no error.
This patch catches negative values before it's too late, i.e. before calling strtoul. We still silently accept very large integers that wrap to a valid "unsigned int".
---
So, here's a proper patch (I mean, a band-aid patch, but properly send ;-) ).
It should be merged before Kartik's series (or inserted at the start of the series) so that we get the fix before the test breakage.
git-compat-util.h | 3 +++ 1 file changed, 3 insertions(+)
git-compat-util.h +3 -0
@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p; errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;
Signature · 1 line
2.5.0.402.g8854c44
Quoted 29 lines · Matthieu Moy
strtoul_uiuses strtoul to get a long unsigned, then checks that casting to unsigned does not lose information and return the casted value.On 64 bits architecture, checking that the cast does not change the value catches most errors, but when sizeof(int) == sizeof(long) (e.g. i386), the check does nothing. Unfortunately, strtoul silently accepts negative values, and as a result
strtoul_ui("-1", ...)raised no error.This patch catches negative values before it's too late, i.e. before calling strtoul. We still silently accept very large integers that wrap to a valid "unsigned int".
Reported-by Max Kirillov <[email protected]>Signed-off-by Matthieu Moy <[email protected]>---
So, here's a proper patch (I mean, a band-aid patch, but properly send ;-) ).It should be merged before Kartik's series (or inserted at the start of the series) so that we get the fix before the test breakage.
git-compat-util.h | 3 +++ 1 file changed, 3 insertions(+)git-compat-util.h +0
-0
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 819
Quoted 6 lines
git-compat-util.h +3
-0@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p; errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ;
I think this is broken, in that it doesn't match strtoul's normal behaviour, for strings like "1234-5678", no?
The test also doesn't work if the string has leading whitespace (" -5").
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 822
Quoted 3 lines
git-compat-util.h +0
-0ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;
Hmm, but we check *p here, so IIUC it's an error if the string has any trailing non-digits. Weird.
M.
Quoted 5 lines · Marc Branchaud
Quoted 5 lines
git-compat-util.h +0
-0@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p;
Quoted 7 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 819
Quoted 4 lines
git-compat-util.h +3
-0errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ;I think this is broken, in that it doesn't match strtoul's normal behaviour, for strings like "1234-5678", no?
The goal here is just to read a positive integer value. Rejecting "1234-5678" is indeed a good thing. We already rejected it before my patch by checking for p (AKA endptr for strtoul), as you noted below.
Quoted 2 lines
The test also doesn't work if the string has leading whitespace (" -5").
Why? It rejects any string that contain the character '-', regardless of trailing spaces.
Quoted 6 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 822
Quoted 3 lines
git-compat-util.h +0
-0ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;Hmm, but we check *p here, so IIUC it's an error if the string has any trailing non-digits. Weird.
strtoul_ui is more defensive than strtoul, by design.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 19 lines · Matthieu Moy
Quoted 5 lines · Marc Branchaud
Quoted 5 lines
git-compat-util.h +0
-0@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p;Quoted 7 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 819
Quoted 4 lines
git-compat-util.h +3
-0errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ;I think this is broken, in that it doesn't match strtoul's normal behaviour, for strings like "1234-5678", no?
The goal here is just to read a positive integer value. Rejecting "1234-5678" is indeed a good thing. We already rejected it before my patch by checking for p (AKA endptr for strtoul), as you noted below.
Quoted 5 lines
Quoted 2 lines
The test also doesn't work if the string has leading whitespace (" -5").
Why? It rejects any string that contain the character '-', regardless of trailing spaces.
Right, sorry.
Quoted 8 lines
Quoted 6 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 822
Quoted 3 lines
git-compat-util.h +0
-0ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;Hmm, but we check *p here, so IIUC it's an error if the string has any trailing non-digits. Weird.
strtoul_uiis more defensive than strtoul, by design.
Fair enough, just not what I expected from a function with that name.
M.
Quoted 3 lines · Matthieu Moy
This patch catches negative values before it's too late, i.e. before calling strtoul. We still silently accept very large integers that wrap to a valid "unsigned int".
Is the last statement correct? A very large long uint that wrap to uint would not fit in long uint and you would get ERANGE, no?
Quoted 2 lines
So, here's a proper patch (I mean, a band-aid patch, but properly send ;-) ).
Yup.
Quoted 2 lines
It should be merged before Kartik's series (or inserted at the start of the series) so that we get the fix before the test breakage.
Which one of his series?
Quoted 18 lines
git-compat-util.h | 3 +++ 1 file changed, 3 insertions(+)git-compat-util.h +3
-0@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p; errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;
PATCH v2 1/1 strtoul_ui: reject negative values
strtoul_ui uses strtoul to get a long unsigned, then checks that casting to unsigned does not lose information and return the casted value.
On 64 bits architecture, checking that the cast does not change the value catches most errors, but when sizeof(int) == sizeof(long) (e.g. i386), the check does nothing. Unfortunately, strtoul silently accepts negative values, and as a result strtoul_ui("-1", ...) raised no error.
This patch catches negative values before it's too late, i.e. before calling strtoul.
---
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Matthieu Moy
This patch catches negative values before it's too late, i.e. before calling strtoul. We still silently accept very large integers that wrap to a valid "unsigned int".
Is the last statement correct? A very large long uint that wrap to uint would not fit in long uint and you would get ERANGE, no?
Indeed. strtoul happily accepts negative values, but not overly large ones.
I removed the sentence from the message. Actually, I think we are now accepting exactly the right interval of values.
Quoted 4 lines
Quoted 2 lines
It should be merged before Kartik's series (or inserted at the start of the series) so that we get the fix before the test breakage.
Which one of his series?
kn/for-each-tag, which uses strtoul_ui for align:<num> and content:lines=<num>.
git-compat-util.h | 3 +++ 1 file changed, 3 insertions(+)
git-compat-util.h +3 -0
@@ -814,6 +814,9 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)char *p; errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; ul =strtoul (s, &p, base);if (errno || *p || p == s || (unsigned int ) ul != ul)return -1 ;
Signature · 1 line
2.5.0.402.g8854c44