Skip to content

Commit e39888b

Browse files
committed
Merge branch 'na/strtoimax' into maint
* na/strtoimax: Support sizes >=2G in various config options accepting 'g' sizes. Compatibility: declare strtoimax() under NO_STRTOUMAX Add strtoimax() compatibility function.
2 parents 786a961 + ebaa1bd commit e39888b

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ all::
5757
#
5858
# Define NO_STRLCPY if you don't have strlcpy.
5959
#
60-
# Define NO_STRTOUMAX if you don't have strtoumax in the C library.
61-
# If your compiler also does not support long long or does not have
60+
# Define NO_STRTOUMAX if you don't have both strtoimax and strtoumax in the
61+
# C library. If your compiler also does not support long long or does not have
6262
# strtoull, define NO_STRTOULL.
6363
#
6464
# Define NO_SETENV if you don't have setenv in the C library.
@@ -1478,7 +1478,7 @@ ifdef NO_STRLCPY
14781478
endif
14791479
ifdef NO_STRTOUMAX
14801480
COMPAT_CFLAGS += -DNO_STRTOUMAX
1481-
COMPAT_OBJS += compat/strtoumax.o
1481+
COMPAT_OBJS += compat/strtoumax.o compat/strtoimax.o
14821482
endif
14831483
ifdef NO_STRTOULL
14841484
COMPAT_CFLAGS += -DNO_STRTOULL

compat/strtoimax.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "../git-compat-util.h"
2+
3+
intmax_t gitstrtoimax (const char *nptr, char **endptr, int base)
4+
{
5+
#if defined(NO_STRTOULL)
6+
return strtol(nptr, endptr, base);
7+
#else
8+
return strtoll(nptr, endptr, base);
9+
#endif
10+
}

config.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static int git_parse_file(config_fn_t fn, void *data)
333333
die("bad config file line %d in %s", cf->linenr, cf->name);
334334
}
335335

336-
static int parse_unit_factor(const char *end, unsigned long *val)
336+
static int parse_unit_factor(const char *end, uintmax_t *val)
337337
{
338338
if (!*end)
339339
return 1;
@@ -356,11 +356,23 @@ static int git_parse_long(const char *value, long *ret)
356356
{
357357
if (value && *value) {
358358
char *end;
359-
long val = strtol(value, &end, 0);
360-
unsigned long factor = 1;
359+
intmax_t val;
360+
uintmax_t uval;
361+
uintmax_t factor = 1;
362+
363+
errno = 0;
364+
val = strtoimax(value, &end, 0);
365+
if (errno == ERANGE)
366+
return 0;
361367
if (!parse_unit_factor(end, &factor))
362368
return 0;
363-
*ret = val * factor;
369+
uval = abs(val);
370+
uval *= factor;
371+
if ((uval > maximum_signed_value_of_type(long)) ||
372+
(abs(val) > uval))
373+
return 0;
374+
val *= factor;
375+
*ret = val;
364376
return 1;
365377
}
366378
return 0;
@@ -370,9 +382,19 @@ int git_parse_ulong(const char *value, unsigned long *ret)
370382
{
371383
if (value && *value) {
372384
char *end;
373-
unsigned long val = strtoul(value, &end, 0);
385+
uintmax_t val;
386+
uintmax_t oldval;
387+
388+
errno = 0;
389+
val = strtoumax(value, &end, 0);
390+
if (errno == ERANGE)
391+
return 0;
392+
oldval = val;
374393
if (!parse_unit_factor(end, &val))
375394
return 0;
395+
if ((val > maximum_unsigned_value_of_type(long)) ||
396+
(oldval > val))
397+
return 0;
376398
*ret = val;
377399
return 1;
378400
}
@@ -553,7 +575,7 @@ static int git_default_core_config(const char *var, const char *value)
553575

554576
if (!strcmp(var, "core.packedgitwindowsize")) {
555577
int pgsz_x2 = getpagesize() * 2;
556-
packed_git_window_size = git_config_int(var, value);
578+
packed_git_window_size = git_config_ulong(var, value);
557579

558580
/* This value must be multiple of (pagesize * 2) */
559581
packed_git_window_size /= pgsz_x2;
@@ -564,18 +586,17 @@ static int git_default_core_config(const char *var, const char *value)
564586
}
565587

566588
if (!strcmp(var, "core.bigfilethreshold")) {
567-
long n = git_config_int(var, value);
568-
big_file_threshold = 0 < n ? n : 0;
589+
big_file_threshold = git_config_ulong(var, value);
569590
return 0;
570591
}
571592

572593
if (!strcmp(var, "core.packedgitlimit")) {
573-
packed_git_limit = git_config_int(var, value);
594+
packed_git_limit = git_config_ulong(var, value);
574595
return 0;
575596
}
576597

577598
if (!strcmp(var, "core.deltabasecachelimit")) {
578-
delta_base_cache_limit = git_config_int(var, value);
599+
delta_base_cache_limit = git_config_ulong(var, value);
579600
return 0;
580601
}
581602

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ extern size_t gitstrlcpy(char *, const char *, size_t);
351351
#ifdef NO_STRTOUMAX
352352
#define strtoumax gitstrtoumax
353353
extern uintmax_t gitstrtoumax(const char *, char **, int);
354+
#define strtoimax gitstrtoimax
355+
extern intmax_t gitstrtoimax(const char *, char **, int);
354356
#endif
355357

356358
#ifdef NO_STRTOK_R

0 commit comments

Comments
 (0)