diff options
author | Jeff King <peff@peff.net> | 2013-09-08 04:29:27 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-09-09 11:04:28 -0700 |
commit | 7192777d226c5b8653c65112a309c2ebd3d6d5c5 (patch) | |
tree | 82c4f56d785f743d59e50f449a10390a91f7c5b7 /config.c | |
parent | 4d06473928ee574910accbde05c19ef2263abdf6 (diff) | |
download | git-7192777d226c5b8653c65112a309c2ebd3d6d5c5.tar.gz |
config: factor out integer parsing from range checks
When we are parsing integers for config, we use an intmax_t
(or uintmax_t) internally, and then check against the size
of our result type at the end. We can parameterize the
maximum representable value, which will let us re-use the
parsing code for a variety of range checks.
Unfortunately, we cannot combine the signed and unsigned
parsing functions easily, as we have to rely on the signed
and unsigned C types internally.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -468,7 +468,7 @@ static int parse_unit_factor(const char *end, uintmax_t *val) return 0; } -static int git_parse_long(const char *value, long *ret) +static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) { if (value && *value) { char *end; @@ -484,8 +484,7 @@ static int git_parse_long(const char *value, long *ret) return 0; uval = abs(val); uval *= factor; - if ((uval > maximum_signed_value_of_type(long)) || - (abs(val) > uval)) + if (uval > max || abs(val) > uval) return 0; val *= factor; *ret = val; @@ -494,7 +493,7 @@ static int git_parse_long(const char *value, long *ret) return 0; } -int git_parse_ulong(const char *value, unsigned long *ret) +int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) { if (value && *value) { char *end; @@ -508,8 +507,7 @@ int git_parse_ulong(const char *value, unsigned long *ret) oldval = val; if (!parse_unit_factor(end, &val)) return 0; - if ((val > maximum_unsigned_value_of_type(long)) || - (oldval > val)) + if (val > max || oldval > val) return 0; *ret = val; return 1; @@ -517,6 +515,24 @@ int git_parse_ulong(const char *value, unsigned long *ret) return 0; } +static int git_parse_long(const char *value, long *ret) +{ + intmax_t tmp; + if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(long))) + return 0; + *ret = tmp; + return 1; +} + +int git_parse_ulong(const char *value, unsigned long *ret) +{ + uintmax_t tmp; + if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) + return 0; + *ret = tmp; + return 1; +} + static void die_bad_config(const char *name) { if (cf && cf->name) |