Use a well-known error string for out-of-bound function arguments

* src/function.c (parse_numeric): Check for empty value and error.
If we find ERANGE just print our own error, not strerror.
(func_word): Use a generic "not good" error message.
(func_wordlist): Ditto
(func_intcmp): Ditto
* tests/run_make_tests.pl: Remove code to find strerror(ERANGE)
* tests/scrips/functions/intcmp: Update the error message.
* tests/scrips/functions/word: Ditto.
This commit is contained in:
Paul Smith 2021-12-18 18:11:30 -05:00
parent 55b993ae09
commit 9230bfb9ae
4 changed files with 24 additions and 29 deletions

View file

@ -774,10 +774,13 @@ parse_numeric (const char *s, const char *msg)
long long num;
strip_whitespace (&beg, &end);
if (beg > end)
OS (fatal, *expanding_var, _("%s: empty value"), msg);
errno = 0;
num = strtoll (beg, &endp, 10);
if (errno == ERANGE)
OSS (fatal, *expanding_var, "%s: '%s'", strerror (errno), s);
OSS (fatal, *expanding_var, _("%s: '%s' out of range"), msg, s);
else if (endp == beg || endp <= end)
/* Empty or non-numeric input */
OSS (fatal, *expanding_var, "%s: '%s'", msg, s);
@ -793,7 +796,7 @@ func_word (char *o, char **argv, const char *funcname UNUSED)
long long i;
i = parse_numeric (argv[0],
_("non-numeric first argument to 'word' function"));
_("invalid first argument to 'word' function"));
if (i < 1)
O (fatal, *expanding_var,
_("first argument to 'word' function must be greater than 0"));
@ -815,9 +818,9 @@ func_wordlist (char *o, char **argv, const char *funcname UNUSED)
long long start, stop, count;
start = parse_numeric (argv[0],
_("non-numeric first argument to 'wordlist' function"));
_("invalid first argument to 'wordlist' function"));
stop = parse_numeric (argv[1],
_("non-numeric second argument to 'wordlist' function"));
_("invalid second argument to 'wordlist' function"));
if (start < 1)
ON (fatal, *expanding_var,
@ -1295,9 +1298,9 @@ func_intcmp (char *o, char **argv, const char *funcname UNUSED)
long long lhs, rhs;
lhs = parse_numeric (lhs_str,
_("non-numeric first argument to 'intcmp' function"));
_("invalid first argument to 'intcmp' function"));
rhs = parse_numeric (rhs_str,
_("non-numeric second argument to 'intcmp' function"));
_("invalid second argument to 'intcmp' function"));
free (lhs_str);
free (rhs_str);

View file

@ -108,7 +108,6 @@ $ERR_read_only_file = undef;
$ERR_unreadable_file = undef;
$ERR_nonexe_file = undef;
$ERR_exe_dir = undef;
$ERR_out_of_range = undef;
{
use locale;
@ -122,9 +121,6 @@ $ERR_out_of_range = undef;
# See set_defaults() as this doesn't work right on Windows :(
$! = &POSIX::ERANGE;
$ERR_out_of_range = "$!";
} else {
$ERR_out_of_range = 'Numerical result out of range';
}
if (open(my $F, '<', 'file.none')) {
@ -464,10 +460,6 @@ sub set_defaults
$scriptsuffix = '.com';
} else {
$scriptsuffix = '.bat';
# Frustratingly, Perl reports the wrong strerror string for ERANGE.
# It's weird because Python gets it right. Not sure what's going on.
$ERR_out_of_range = 'Result too large';
}
}

View file

@ -42,17 +42,17 @@ intcmp-e3: ; @echo $(intcmp -1,9999999999999999999,foo)
intcmp-e4: ; @echo $(intcmp -1)
intcmp-e5: ; @echo $(intcmp ,55)',
'intcmp-e1',
"#MAKEFILE#:2: *** non-numeric first argument to 'intcmp' function: '12a'. Stop.",
"#MAKEFILE#:2: *** invalid first argument to 'intcmp' function: '12a'. Stop.",
512);
run_make_test(undef,
'intcmp-e2',
"#MAKEFILE#:3: *** non-numeric second argument to 'intcmp' function: ''. Stop.",
"#MAKEFILE#:3: *** invalid second argument to 'intcmp' function: empty value. Stop.",
512);
run_make_test(undef,
'intcmp-e3',
"#MAKEFILE#:4: *** $ERR_out_of_range: '9999999999999999999'. Stop.",
"#MAKEFILE#:4: *** invalid second argument to 'intcmp' function: '9999999999999999999' out of range. Stop.",
512);

View file

@ -56,37 +56,37 @@ wordlist-e1: ; @echo $(wordlist ,,$(FOO))
wordlist-e2: ; @echo $(wordlist abc ,,$(FOO))
wordlist-e3: ; @echo $(wordlist 1, 12a ,$(FOO))',
'word-e1',
"#MAKEFILE#:3: *** non-numeric first argument to 'word' function: ''. Stop.",
"#MAKEFILE#:3: *** invalid first argument to 'word' function: empty value. Stop.",
512);
run_make_test(undef,
'word-e2',
"#MAKEFILE#:4: *** non-numeric first argument to 'word' function: 'abc '. Stop.",
"#MAKEFILE#:4: *** invalid first argument to 'word' function: 'abc '. Stop.",
512);
run_make_test(undef,
'word-e3',
"#MAKEFILE#:5: *** non-numeric first argument to 'word' function: '1a'. Stop.",
"#MAKEFILE#:5: *** invalid first argument to 'word' function: '1a'. Stop.",
512);
run_make_test(undef,
'word-e4',
"#MAKEFILE#:6: *** $ERR_out_of_range: '9999999999999999999'. Stop.",
"#MAKEFILE#:6: *** invalid first argument to 'word' function: '9999999999999999999' out of range. Stop.",
512);
run_make_test(undef,
'wordlist-e1',
"#MAKEFILE#:8: *** non-numeric first argument to 'wordlist' function: ''. Stop.",
"#MAKEFILE#:8: *** invalid first argument to 'wordlist' function: empty value. Stop.",
512);
run_make_test(undef,
'wordlist-e2',
"#MAKEFILE#:9: *** non-numeric first argument to 'wordlist' function: 'abc '. Stop.",
"#MAKEFILE#:9: *** invalid first argument to 'wordlist' function: 'abc '. Stop.",
512);
run_make_test(undef,
'wordlist-e3',
"#MAKEFILE#:10: *** non-numeric second argument to 'wordlist' function: ' 12a '. Stop.",
"#MAKEFILE#:10: *** invalid second argument to 'wordlist' function: ' 12a '. Stop.",
512);
# Test error conditions again, but this time in a variable reference
@ -99,12 +99,12 @@ WL = $(wordlist $s,$e,$(FOO))
word-e: ; @echo $(W)
wordlist-e: ; @echo $(WL)',
'word-e x=',
"#MAKEFILE#:3: *** non-numeric first argument to 'word' function: ''. Stop.",
"#MAKEFILE#:3: *** invalid first argument to 'word' function: empty value. Stop.",
512);
run_make_test(undef,
'word-e x=abc',
"#MAKEFILE#:3: *** non-numeric first argument to 'word' function: 'abc'. Stop.",
"#MAKEFILE#:3: *** invalid first argument to 'word' function: 'abc'. Stop.",
512);
run_make_test(undef,
@ -114,17 +114,17 @@ run_make_test(undef,
run_make_test(undef,
'wordlist-e s= e=',
"#MAKEFILE#:4: *** non-numeric first argument to 'wordlist' function: ''. Stop.",
"#MAKEFILE#:4: *** invalid first argument to 'wordlist' function: empty value. Stop.",
512);
run_make_test(undef,
'wordlist-e s=abc e=',
"#MAKEFILE#:4: *** non-numeric first argument to 'wordlist' function: 'abc'. Stop.",
"#MAKEFILE#:4: *** invalid first argument to 'wordlist' function: 'abc'. Stop.",
512);
run_make_test(undef,
'wordlist-e s=4 e=12a',
"#MAKEFILE#:4: *** non-numeric second argument to 'wordlist' function: '12a'. Stop.",
"#MAKEFILE#:4: *** invalid second argument to 'wordlist' function: '12a'. Stop.",
512);
run_make_test(undef,