aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--editors/awk.c3
-rwxr-xr-xtestsuite/awk.tests6
2 files changed, 9 insertions, 0 deletions
diff --git a/editors/awk.c b/editors/awk.c
index d54249bfd..bafc9ba1d 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -598,6 +598,7 @@ static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
598static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error"; 598static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
599static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function"; 599static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
600static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; 600static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
601static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field";
601 602
602static void zero_out_var(var *vp) 603static void zero_out_var(var *vp)
603{ 604{
@@ -2949,6 +2950,8 @@ static var *evaluate(node *op, var *res)
2949 2950
2950 case XC( OC_FIELD ): { 2951 case XC( OC_FIELD ): {
2951 int i = (int)getvar_i(R.v); 2952 int i = (int)getvar_i(R.v);
2953 if (i < 0)
2954 syntax_error(EMSG_NEGATIVE_FIELD);
2952 if (i == 0) { 2955 if (i == 0) {
2953 res = intvar[F0]; 2956 res = intvar[F0];
2954 } else { 2957 } else {
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index ad0583afb..3933fefc9 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -339,5 +339,11 @@ testing "awk handles invalid for loop" \
339 "awk '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" 339 "awk '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
340 340
341# testing "description" "command" "result" "infile" "stdin" 341# testing "description" "command" "result" "infile" "stdin"
342testing 'awk negative field access' \
343 'awk 2>&1 -- '\''{ $(-1) }'\' \
344 "awk: cmd. line:1: Access to negative field\n" \
345 '' \
346 'anything'
347
342 348
343exit $FAILCOUNT 349exit $FAILCOUNT