config.c: fix potential number truncation in git_parse_signed()
PATCH 1/1 config.c: fix potential number truncation in git_parse_signed()
clang -Wabsolute-value on IA-32 architecture complains that "absolute value function 'labs' given an argument of type 'intmax_t' (aka 'long long') but has parameter of type 'long' which may cause truncation of value". Very unlikely for this code though. Nevertheless, add an explicit check for truncation to shut clang up and error out.
--- config.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
config.c +8 -2
@@ -503,6 +503,7 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)intmax_t val;uintmax_t uval;uintmax_t factor =1 ; +long int lival; errno =0 ; val =strtoimax (value, &end,0 );
@@ -512,9 +513,14 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) errno =EINVAL ;return 0 ; } - uval =labs (val); + lival = (long int )val; +if (lival != val) { + errno =ERANGE ; +return 0 ; + } + uval =labs (lival); uval *= factor; -if (uval > max ||labs (val) > uval) { +if (uval > max ||labs (lival) > uval) { errno =ERANGE ;return 0 ; }
Signature · 1 line
2.8.2.532.g6dfa503.dirty
Quoted 5 lines · Nguyễn Thái Ngọc Duy
clang -Wabsolute-value on IA-32 architecture complains that "absolute value function 'labs' given an argument of type '
intmax_t' (aka 'long long') but has parameter of type 'long' which may cause truncation of value". Very unlikely for this code though. Nevertheless, add an explicit check for truncation to shut clang up and error out.
Thanks. It however makes me wonder if it is a better approach to avoid downcasting intmax_t to long by using imaxabs()?
Quoted 35 lines
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- config.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)config.c +8
-2@@ -503,6 +503,7 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)intmax_t val;uintmax_t uval;uintmax_t factor =1 ; +long int lival; errno =0 ; val =strtoimax (value, &end,0 );@@ -512,9 +513,14 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) errno =EINVAL ;return 0 ; } - uval =labs (val); + lival = (long int )val; +if (lival != val) { + errno =ERANGE ; +return 0 ; + } + uval =labs (lival); uval *= factor; -if (uval > max ||labs (val) > uval) { +if (uval > max ||labs (lival) > uval) { errno =ERANGE ;return 0 ; }
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Nguyễn Thái Ngọc Duy
clang -Wabsolute-value on IA-32 architecture complains that "absolute value function 'labs' given an argument of type '
intmax_t' (aka 'long long') but has parameter of type 'long' which may cause truncation of value". Very unlikely for this code though. Nevertheless, add an explicit check for truncation to shut clang up and error out.Thanks. It however makes me wonder if it is a better approach to avoid downcasting
intmax_tto long by usingimaxabs()?
I think the tricky part is detect overflow. WIth uval being a potentially signed number, i'm not sure how to catch it. The (uval > max || labs(val) > uval) was written with unsigned number in mind, I think.
Quoted 36 lines
Quoted 36 lines
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- config.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)config.c +8
-2@@ -503,6 +503,7 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)intmax_t val;uintmax_t uval;uintmax_t factor =1 ; +long int lival; errno =0 ; val =strtoimax (value, &end,0 );@@ -512,9 +513,14 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) errno =EINVAL ;return 0 ; } - uval =labs (val); + lival = (long int )val; +if (lival != val) { + errno =ERANGE ; +return 0 ; + } + uval =labs (lival); uval *= factor; -if (uval > max ||labs (val) > uval) { +if (uval > max ||labs (lival) > uval) { errno =ERANGE ;return 0 ; }
Signature · 1 line
Duy