summaryrefslogtreecommitdiffstats
path: root/patches/02_awk-fix-handling-of-functions-with-empty-body.patch
blob: 102e128dc53852e3f30c4ec3a9f67c9b655e5bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Date: Wed, 06 Mar 2013 20:01:05 +0000
Date: Wed, 31 Jul 2013 13:29:20 +0000
Origin: upstream, http://git.busybox.net/busybox/commit/?id=b79a0fef
Origin: upstream, http://git.busybox.net/busybox/commit/?id=a060a1ad
Subject: awk: Fix handling of functions with empty body

This fixes the following error that occurs when building an unpatched GCC 4.7
with BusyBox awk:

    awk: [...]/gcc/config/i386/i386-builtin-types.awk:164: Call to undefined function

BusyBox awk throws this syntax error if the called function has no nodes in its
node chain (i.e. the function body is empty).

---
diff --git a/editors/awk.c b/editors/awk.c
index 3224788..77784df 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2661,7 +2661,8 @@ static var *evaluate(node *op, var *res)
 			var *vbeg, *v;
 			const char *sv_progname;
 
-			if (!op->r.f->body.first)
+			/* The body might be empty, still has to eval the args */
+			if (!op->r.n->info && !op->r.f->body.first)
 				syntax_error(EMSG_UNDEF_FUNC);
 
 			vbeg = v = nvalloc(op->r.f->nargs + 1);
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index f9c3b6b..6af6072 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -25,6 +25,53 @@ testing "awk if string == "    "awk 'BEGIN{if(\"a\"==\"ab\") print \"bar\"}'" ""
 
 # 4294967295 = 0xffffffff
 testing "awk bitwise op"  "awk '{ print or(4294967295,1) }'" "4.29497e+09\n" "" "\n"
+
+# we were testing for a non-empty body when deciding if a function was
+# defined or not. The testcase below caused:
+# awk: cmd. line:8: Call to undefined function
+prg='
+function empty_fun(count) {
+  # empty
+}
+END {
+  i=1
+  print "L" i "\n"
+  empty_fun(i + i + ++i)
+  print "L" i "\n"
+}'
+testing "awk handles empty function f(arg){}" \
+	"awk '$prg'" \
+	"L1\n\nL2\n\n" \
+	"" ""
+
+prg='
+function outer_fun() {
+  return 1
+}
+END {
+  i=1
+  print "L" i "\n"
+  i += outer_fun()
+  print "L" i "\n"
+}'
+testing "awk properly handles function from other scope" \
+	"awk '$prg'" \
+	"L1\n\nL2\n\n" \
+	"" ""
+
+prg='
+END {
+  i=1
+  print "L" i "\n"
+  i + trigger_error_fun()
+  print "L" i "\n"
+}'
+testing "awk properly handles undefined function" \
+	"awk '$prg' 2>&1" \
+	"L1\n\nawk: cmd. line:5: Call to undefined function\n" \
+	"" ""
+
+
 optional DESKTOP
 testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4.29497e+09\n" "" "\n"
 testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2.14748e+09\n" "" "\n"