Skip to content

Commit 42d194e

Browse files
peffgitster
authored andcommitted
config: properly range-check integer values
When we look at a config value as an integer using the git_config_int function, we carefully range-check the value we get and complain if it is out of our range. But the range we compare to is that of a "long", which we then cast to an "int" in the function's return value. This means that on systems where "int" and "long" have different sizes (e.g., LP64 systems), we may pass the range check, but then return nonsense by truncating the value as we cast it to an int. We can solve this by converting git_parse_long into git_parse_int, and range-checking the "int" range. Nobody actually cared that we used a "long" internally, since the result was truncated anyway. And the only other caller of git_parse_long is git_config_maybe_bool, which should be fine to just use int (though we will now forbid out-of-range nonsense like setting "merge.ff" to "10g" to mean "true", which is probably a good thing). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7192777 commit 42d194e

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

config.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,10 @@ int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
515515
return 0;
516516
}
517517

518-
static int git_parse_long(const char *value, long *ret)
518+
static int git_parse_int(const char *value, int *ret)
519519
{
520520
intmax_t tmp;
521-
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(long)))
521+
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
522522
return 0;
523523
*ret = tmp;
524524
return 1;
@@ -542,8 +542,8 @@ static void die_bad_config(const char *name)
542542

543543
int git_config_int(const char *name, const char *value)
544544
{
545-
long ret = 0;
546-
if (!git_parse_long(value, &ret))
545+
int ret;
546+
if (!git_parse_int(value, &ret))
547547
die_bad_config(name);
548548
return ret;
549549
}
@@ -575,10 +575,10 @@ static int git_config_maybe_bool_text(const char *name, const char *value)
575575

576576
int git_config_maybe_bool(const char *name, const char *value)
577577
{
578-
long v = git_config_maybe_bool_text(name, value);
578+
int v = git_config_maybe_bool_text(name, value);
579579
if (0 <= v)
580580
return v;
581-
if (git_parse_long(value, &v))
581+
if (git_parse_int(value, &v))
582582
return !!v;
583583
return -1;
584584
}

0 commit comments

Comments
 (0)