aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-06-30 12:52:51 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-06-30 12:52:51 +0200
commitd7354df169603807fe2ac4f8a0f9f72c9703184f (patch)
treef93f4b2564b617420d4c4b3b7032b0e557e999ea
parentca9278ee5855a91a5521960d3743809f47ed27b8 (diff)
downloadbusybox-w32-d7354df169603807fe2ac4f8a0f9f72c9703184f.tar.gz
busybox-w32-d7354df169603807fe2ac4f8a0f9f72c9703184f.tar.bz2
busybox-w32-d7354df169603807fe2ac4f8a0f9f72c9703184f.zip
awk: evaluate all, even superfluous function args
function old new delta evaluate 3128 3135 +7 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/awk.c19
-rwxr-xr-xtestsuite/awk.tests8
2 files changed, 19 insertions, 8 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 0fbca0433..47bbc10a6 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2910,7 +2910,7 @@ static var *evaluate(node *op, var *res)
2910 case XC( OC_FUNC ): { 2910 case XC( OC_FUNC ): {
2911 var *tv, *sv_fnargs; 2911 var *tv, *sv_fnargs;
2912 const char *sv_progname; 2912 const char *sv_progname;
2913 int nargs1, i; 2913 int nargs, i;
2914 2914
2915 debug_printf_eval("FUNC\n"); 2915 debug_printf_eval("FUNC\n");
2916 2916
@@ -2918,17 +2918,22 @@ static var *evaluate(node *op, var *res)
2918 syntax_error(EMSG_UNDEF_FUNC); 2918 syntax_error(EMSG_UNDEF_FUNC);
2919 2919
2920 /* The body might be empty, still has to eval the args */ 2920 /* The body might be empty, still has to eval the args */
2921 nargs1 = op->r.f->nargs + 1; 2921 nargs = op->r.f->nargs;
2922 tv = nvalloc(nargs1); 2922 tv = nvalloc(nargs);
2923 i = 0; 2923 i = 0;
2924 while (op1) { 2924 while (op1) {
2925//TODO: explain why one iteration is done even for the case p->r.f->nargs == 0
2926 var *arg = evaluate(nextarg(&op1), v1); 2925 var *arg = evaluate(nextarg(&op1), v1);
2926 if (i == nargs) {
2927 /* call with more arguments than function takes.
2928 * (gawk warns: "warning: function 'f' called with more arguments than declared").
2929 * They are still evaluated, but discarded: */
2930 clrvar(arg);
2931 continue;
2932 }
2927 copyvar(&tv[i], arg); 2933 copyvar(&tv[i], arg);
2928 tv[i].type |= VF_CHILD; 2934 tv[i].type |= VF_CHILD;
2929 tv[i].x.parent = arg; 2935 tv[i].x.parent = arg;
2930 if (++i >= op->r.f->nargs) 2936 i++;
2931 break;
2932 } 2937 }
2933 2938
2934 sv_fnargs = fnargs; 2939 sv_fnargs = fnargs;
@@ -2936,7 +2941,7 @@ static var *evaluate(node *op, var *res)
2936 2941
2937 fnargs = tv; 2942 fnargs = tv;
2938 res = evaluate(op->r.f->body.first, res); 2943 res = evaluate(op->r.f->body.first, res);
2939 nvfree(fnargs, nargs1); 2944 nvfree(fnargs, nargs);
2940 2945
2941 g_progname = sv_progname; 2946 g_progname = sv_progname;
2942 fnargs = sv_fnargs; 2947 fnargs = sv_fnargs;
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index 873cc3680..3c230393f 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -87,11 +87,17 @@ BEGIN {
87 a=2 87 a=2
88 print v (a) 88 print v (a)
89}' 89}'
90testing "'v (a)' is not a function call, it is a concatenation" \ 90testing "awk 'v (a)' is not a function call, it is a concatenation" \
91 "awk '$prg' 2>&1" \ 91 "awk '$prg' 2>&1" \
92 "12\n" \ 92 "12\n" \
93 "" "" 93 "" ""
94 94
95prg='func f(){print"F"};func g(){print"G"};BEGIN{f(g(),g())}'
96testing "awk unused function args are evaluated" \
97 "awk '$prg' 2>&1" \
98 "G\nG\nF\n" \
99 "" ""
100
95 101
96optional DESKTOP 102optional DESKTOP
97testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n" 103testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n"