From 2848045aed5ccfd7b582808288f7fcc18d319952 Mon Sep 17 00:00:00 2001
From: "Arnold D. Robbins" <arnold@skeeve.com>
Date: Wed, 14 Sep 2022 22:33:39 +0300
Subject: Additional fix for Node_elem_new.

Reference:https://git.savannah.gnu.org/cgit/gawk.git/patch/?id=2848045aed5ccfd7b582808288f7fcc18d319952
Conflict:tailoring commit on ChangeLog
---
 interpret.h       | 14 ++++++++++----
 pc/ChangeLog      |  4 ++++
 pc/Makefile.tst   | 18 +++++++++++++++++-
 test/ChangeLog    |  5 +++--
 test/Makefile.am  |  7 ++++++-
 test/Makefile.in  | 17 ++++++++++++++++-
 test/Maketests    | 10 ++++++++++
 test/elemnew2.awk |  6 ++++++
 test/elemnew2.ok  |  2 ++
 test/elemnew3.awk |  8 ++++++++
 test/elemnew3.ok  |  0
 11 files changed, 81 insertions(+), 9 deletions(-)
 create mode 100644 test/elemnew2.awk
 create mode 100644 test/elemnew2.ok
 create mode 100644 test/elemnew3.awk
 create mode 100644 test/elemnew3.ok

diff --git a/interpret.h b/interpret.h
index 268655b..26010ad 100644
--- a/interpret.h
+++ b/interpret.h
@@ -213,7 +213,7 @@ top:
 			case Node_var_new:
 uninitialized_scalar:
 				if (op != Op_push_arg_untyped) {
-					/* convert untyped to scalar */
+					/* convert very original untyped to scalar */
 					m->type = Node_var;
 					m->var_value = dupnode(Nnull_string);
 				}
@@ -222,6 +222,7 @@ uninitialized_scalar:
 						_("reference to uninitialized argument `%s'") :
 						_("reference to uninitialized variable `%s'"),
 								save_symbol->vname);
+				// set up local param by value
 				if (op != Op_push_arg_untyped)
 					m = dupnode(Nnull_string);
 				UPREF(m);
@@ -230,15 +231,20 @@ uninitialized_scalar:
 
 			case Node_elem_new:
 				if (op != Op_push_arg_untyped) {
-					/* convert untyped to scalar */
-					m = elem_new_to_scalar(m);
+					/* convert very original untyped to scalar */
+					m->type = Node_var;
+					m->var_value = dupnode(Nnull_string);
 				}
 				if (do_lint)
 					lintwarn(isparam ?
 						_("reference to uninitialized argument `%s'") :
 						_("reference to uninitialized variable `%s'"),
 								save_symbol->vname);
-				UPREF(m);
+				// set up local param by value
+				if (op != Op_push_arg_untyped) {
+					m = elem_new_to_scalar(m);
+				}
+
 				PUSH(m);
 				break;
 
diff --git a/pc/ChangeLog b/pc/ChangeLog
index a78ccfc..67ee4d2 100644
--- a/pc/ChangeLog
+++ b/pc/ChangeLog
@@ -1,3 +1,7 @@
+2022-09-14         Arnold D. Robbins     <arnold@skeeve.com>
+
+	* Makefile.tst: Regenerated.
+
 2022-09-04         Arnold D. Robbins     <arnold@skeeve.com>
 
 	* 5.2.0: Release tar ball made.
diff --git a/pc/Makefile.tst b/pc/Makefile.tst
index 269ec8a..9e40cd2 100644
--- a/pc/Makefile.tst
+++ b/pc/Makefile.tst
@@ -190,7 +190,8 @@ GAWK_EXT_TESTS = \
 	binmode1 charasbytes clos1way clos1way2 clos1way3 clos1way4 \
 	clos1way5 clos1way6 colonwarn commas crlf dbugeval dbugeval2 \
 	dbugeval3 dbugtypedre1 dbugtypedre2 delsub devfd devfd1 devfd2 \
-	dfacheck1 dumpvars errno exit fieldwdth forcenum fpat1 fpat2 \
+	dfacheck1 dumpvars elemnew1 elemnew2 elemnew3 \
+	errno exit fieldwdth forcenum fpat1 fpat2 \
 	fpat3 fpat4 fpat5 fpat6 fpat7 fpat8 fpat9 fpatnull fsfwfs functab1 \
 	functab2 functab3 functab6 funlen fwtest fwtest2 fwtest3 fwtest4 \
 	fwtest5 fwtest6 fwtest7 fwtest8 genpot gensub gensub2 gensub3 \
@@ -2735,6 +2736,21 @@ dfacheck1:
 	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  < "$(srcdir)"/$@.in >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
 	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
 
+elemnew1:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
+elemnew2:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
+elemnew3:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
 exit:
 	@echo $@
 	@-$(LOCALES) AWK="$(AWKPROG)" "$(srcdir)"/$@.sh  > _$@ 2>&1 || echo EXIT CODE: $$? >>_$@
diff --git a/test/ChangeLog b/test/ChangeLog
index ea08ede..434cbd9 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,7 +1,8 @@
 2022-09-14         Arnold D. Robbins     <arnold@skeeve.com>
 
-	* Makefile.am (EXTRA_DIST): New test: elemnew1.
-	* elemnew1.awk, elemnew1.ok: New files.
+	* Makefile.am (EXTRA_DIST): New tests: elemnew1, elemnew2, elemnew3.
+	* elemnew1.awk, elemnew1.ok, elemnew2.awk, elemnew2.ok,
+	elemnew3.awk, elemnew3.ok: New files.
 
 2022-09-04         Arnold D. Robbins     <arnold@skeeve.com>
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 8a5c3c4..8b0a24e 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -284,6 +284,10 @@ EXTRA_DIST = \
 	dynlj.ok \
 	elemnew1.awk \
 	elemnew1.ok \
+	elemnew2.awk \
+	elemnew2.ok \
+	elemnew3.awk \
+	elemnew3.ok \
 	escapebrace.awk \
 	escapebrace.in \
 	escapebrace.ok \
@@ -1482,7 +1486,8 @@ GAWK_EXT_TESTS = \
 	binmode1 charasbytes clos1way clos1way2 clos1way3 clos1way4 \
 	clos1way5 clos1way6 colonwarn commas crlf dbugeval dbugeval2 \
 	dbugeval3 dbugtypedre1 dbugtypedre2 delsub devfd devfd1 devfd2 \
-	dfacheck1 dumpvars elemnew1 errno exit fieldwdth forcenum fpat1 fpat2 \
+	dfacheck1 dumpvars elemnew1 elemnew2 elemnew3 \
+	errno exit fieldwdth forcenum fpat1 fpat2 \
 	fpat3 fpat4 fpat5 fpat6 fpat7 fpat8 fpat9 fpatnull fsfwfs functab1 \
 	functab2 functab3 functab6 funlen fwtest fwtest2 fwtest3 fwtest4 \
 	fwtest5 fwtest6 fwtest7 fwtest8 genpot gensub gensub2 gensub3 \
diff --git a/test/Makefile.in b/test/Makefile.in
index a4fb1cc..9240a42 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -552,6 +552,10 @@ EXTRA_DIST = \
 	dynlj.ok \
 	elemnew1.awk \
 	elemnew1.ok \
+	elemnew2.awk \
+	elemnew2.ok \
+	elemnew3.awk \
+	elemnew3.ok \
 	escapebrace.awk \
 	escapebrace.in \
 	escapebrace.ok \
@@ -1750,7 +1754,8 @@ GAWK_EXT_TESTS = \
 	binmode1 charasbytes clos1way clos1way2 clos1way3 clos1way4 \
 	clos1way5 clos1way6 colonwarn commas crlf dbugeval dbugeval2 \
 	dbugeval3 dbugtypedre1 dbugtypedre2 delsub devfd devfd1 devfd2 \
-	dfacheck1 dumpvars elemnew1 errno exit fieldwdth forcenum fpat1 fpat2 \
+	dfacheck1 dumpvars elemnew1 elemnew2 elemnew3 \
+	errno exit fieldwdth forcenum fpat1 fpat2 \
 	fpat3 fpat4 fpat5 fpat6 fpat7 fpat8 fpat9 fpatnull fsfwfs functab1 \
 	functab2 functab3 functab6 funlen fwtest fwtest2 fwtest3 fwtest4 \
 	fwtest5 fwtest6 fwtest7 fwtest8 genpot gensub gensub2 gensub3 \
@@ -4483,6 +4488,16 @@ elemnew1:
 	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
 	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
 
+elemnew2:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
+elemnew3:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
 exit:
 	@echo $@
 	@-$(LOCALES) AWK="$(AWKPROG)" "$(srcdir)"/$@.sh  > _$@ 2>&1 || echo EXIT CODE: $$? >>_$@
diff --git a/test/Maketests b/test/Maketests
index 90828e7..f7e749c 100644
--- a/test/Maketests
+++ b/test/Maketests
@@ -1437,6 +1437,16 @@ elemnew1:
 	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
 	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
 
+elemnew2:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
+elemnew3:
+	@echo $@
+	@-AWKPATH="$(srcdir)" $(AWK) -f $@.awk  >_$@ 2>&1 || echo EXIT CODE: $$? >>_$@
+	@-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@
+
 exit:
 	@echo $@
 	@-$(LOCALES) AWK="$(AWKPROG)" "$(srcdir)"/$@.sh  > _$@ 2>&1 || echo EXIT CODE: $$? >>_$@
diff --git a/test/elemnew2.awk b/test/elemnew2.awk
new file mode 100644
index 0000000..08e7239
--- /dev/null
+++ b/test/elemnew2.awk
@@ -0,0 +1,6 @@
+function set_val(old) {
+	old[1] = 42
+}
+BEGIN {
+  a[0] = set_val(a[0])
+}
diff --git a/test/elemnew2.ok b/test/elemnew2.ok
new file mode 100644
index 0000000..d6f2d78
--- /dev/null
+++ b/test/elemnew2.ok
@@ -0,0 +1,2 @@
+gawk: elemnew2.awk:5: fatal: attempt to use array `a["0"]' in a scalar context
+EXIT CODE: 2
diff --git a/test/elemnew3.awk b/test/elemnew3.awk
new file mode 100644
index 0000000..39ac9a8
--- /dev/null
+++ b/test/elemnew3.awk
@@ -0,0 +1,8 @@
+function foo(x)
+{
+        if (x == int(x))
+                return (int(x) != 0)
+}
+BEGIN {
+        foo(P["bar"])
+}
diff --git a/test/elemnew3.ok b/test/elemnew3.ok
new file mode 100644
index 0000000..e69de29
-- 
