Move unsigned limits to a separate table/recognizer and display them

using the appropriate (unsigned) format specification.  This prevents
integer overflow when ULLONG_MAX and (on some architectures) ULONG_MAX
are used to initialize an intmax_t and then displayed as the signed
value -1.  (A different approach was suggested in the bug report,
which I did not use.)  If other limits are defined to be unsigned,
they could be moved here.

PR:		164049
Reported by:	Marcus Reid
This commit is contained in:
wollman 2018-05-23 02:51:56 +00:00
parent abeb3096ce
commit 29fa89bc66
4 changed files with 12 additions and 7 deletions

View File

@ -4,11 +4,12 @@
PROG= getconf
SRCS= confstr.c getconf.c limits.c pathconf.c progenv.c sysconf.c
SRCS= confstr.c getconf.c limits.c pathconf.c progenv.c sysconf.c \
unsigned_limits.c
CFLAGS+= -I${.CURDIR}
CLEANFILES+= confstr.c limits.c pathconf.c progenv.c sysconf.c \
confstr.names limits.names pathconf.names sysconf.names \
conflicting.names unique.names
conflicting.names unique.names unsigned_limits.names
.SUFFIXES: .gperf .names
.PHONY: conflicts

View File

@ -65,6 +65,7 @@ main(int argc, char **argv)
int c, key, valid;
const char *name, *vflag, *alt_path;
intmax_t limitval;
uintmax_t ulimitval;
aflag = false;
vflag = NULL;
@ -115,6 +116,13 @@ main(int argc, char **argv)
}
if (argv[optind + 1] == NULL) { /* confstr or sysconf */
if ((valid = find_unsigned_limit(name, &ulimitval)) != 0) {
if (valid > 0)
printf("%" PRIuMAX "\n", ulimitval);
else
printf("undefined\n");
return 0;
}
if ((valid = find_limit(name, &limitval)) != 0) {
if (valid > 0)
printf("%" PRIdMAX "\n", limitval);

View File

@ -37,6 +37,7 @@ typedef long long intmax_t;
#endif
int find_confstr(const char *name, int *key);
int find_unsigned_limit(const char *name, uintmax_t *value);
int find_limit(const char *name, intmax_t *value);
int find_pathconf(const char *name, int *key);
int find_progenv(const char *name, const char **alt_path);

View File

@ -86,11 +86,6 @@ SCHAR_MIN, SCHAR_MIN
SHRT_MAX, SHRT_MAX
SHRT_MIN, SHRT_MIN
SSIZE_MAX, SSIZE_MAX
UCHAR_MAX, UCHAR_MAX
UINT_MAX, UINT_MAX
ULLONG_MAX, ULLONG_MAX
ULONG_MAX, ULONG_MAX
USHRT_MAX, USHRT_MAX
WORD_BIT, WORD_BIT
CHARCLASS_NAME_MAX, CHARCLASS_NAME_MAX
NL_ARGMAX, NL_ARGMAX