From 9230bfb9aea55201d47afb666c104cc4542a1f39 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sat, 18 Dec 2021 18:11:30 -0500 Subject: [PATCH] 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. --- src/function.c | 15 +++++++++------ tests/run_make_tests.pl | 8 -------- tests/scripts/functions/intcmp | 6 +++--- tests/scripts/functions/word | 24 ++++++++++++------------ 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/function.c b/src/function.c index 0f9b6091..af6ad135 100644 --- a/src/function.c +++ b/src/function.c @@ -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); diff --git a/tests/run_make_tests.pl b/tests/run_make_tests.pl index ebf202a2..4cc375e5 100644 --- a/tests/run_make_tests.pl +++ b/tests/run_make_tests.pl @@ -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'; } } diff --git a/tests/scripts/functions/intcmp b/tests/scripts/functions/intcmp index eb58ae30..6f3c1500 100644 --- a/tests/scripts/functions/intcmp +++ b/tests/scripts/functions/intcmp @@ -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); diff --git a/tests/scripts/functions/word b/tests/scripts/functions/word index 1c87c4a1..217e6936 100644 --- a/tests/scripts/functions/word +++ b/tests/scripts/functions/word @@ -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,