From 8b1a5b7dbf993908f24c1a0a1ddebbf2b96c70dd Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 14 Jun 2020 11:18:15 +0300 Subject: Check for FUNCTAB and SYMTAB as destination in builtin functions. --- ChangeLog | 12 ++++++++++++ array.c | 9 +++------ awk.h | 1 + builtin.c | 14 ++++++++++++++ field.c | 8 ++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 83bf1a00..384bc6b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2020-06-14 Arnold D. Robbins + + Disallow SYMTAB and FUNCTAB as destination arguments to builtin + functions that clear arrays: + + * awk.h (check_symtab_functab): Add declaration. + * array.c (asort_actual): Call it in check for second argument. + * builtin.c (check_symtab_functab): New function. + (do_match): Call it in check for third argument. + * field.c (do_patsplit, do_split): Call it in checks for fourth and + second arguments. + 2020-06-12 Arnold D. Robbins * array.c (asort_actual): If SYMTAB or FUNCTAB is the second argument, diff --git a/array.c b/array.c index e3bbc835..c256620b 100644 --- a/array.c +++ b/array.c @@ -824,12 +824,9 @@ asort_actual(int nargs, sort_context_t ctxt) fatal(_("%s: second argument is not an array"), ctxt == ASORT ? "asort" : "asorti"); } - if (dest == symbol_table) - fatal(_("%s: SYMTAB cannot be used as second argument"), - ctxt == ASORT ? "asort" : "asorti"); - else if (dest == func_table) - fatal(_("%s: FUNCTAB cannot be used as second argument"), - ctxt == ASORT ? "asort" : "asorti"); + check_symtab_functab(dest, + ctxt == ASORT ? "asort" : "asorti", + _("%s: cannot use %s as second argument")); } array = POP_PARAM(); diff --git a/awk.h b/awk.h index 9005ff34..cccb8ce5 100644 --- a/awk.h +++ b/awk.h @@ -1498,6 +1498,7 @@ extern NODE *do_typeof(int nargs); extern int strncasecmpmbs(const unsigned char *, const unsigned char *, size_t); extern int sanitize_exit_status(int status); +extern void check_symtab_functab(NODE *dest, const char *fname, const char *msg); /* debug.c */ extern void init_debug(void); extern int debug_prog(INSTRUCTION *pc); diff --git a/builtin.c b/builtin.c index 7ef2acd8..9b5609fa 100644 --- a/builtin.c +++ b/builtin.c @@ -2678,6 +2678,8 @@ do_match(int nargs) dest = POP_PARAM(); if (dest->type != Node_var_array) fatal(_("match: third argument is not an array")); + check_symtab_functab(dest, "match", + _("%s: cannot use %s as third argument")); assoc_clear(dest); } tre = POP(); @@ -4328,3 +4330,15 @@ fmt: } return buf; } + + +/* check_symtab_functab --- check if dest is SYMTAB or FUNCTAB, fatal if so */ + +void +check_symtab_functab(NODE *dest, const char *fname, const char *msg) +{ + if (dest == symbol_table) + fatal(msg, fname, "SYMTAB"); + else if (dest == func_table) + fatal(msg, fname, "FUNCTAB"); +} diff --git a/field.c b/field.c index 45ab9f5f..0e84447a 100644 --- a/field.c +++ b/field.c @@ -980,6 +980,8 @@ do_split(int nargs) sep_arr = POP_PARAM(); if (sep_arr->type != Node_var_array) fatal(_("split: fourth argument is not an array")); + check_symtab_functab(sep_arr, "split", + _("%s: cannot use %s as fourth argument")); if ((do_lint_extensions || do_lint_old) && ! warned) { warned = true; lintwarn(_("split: fourth argument is a gawk extension")); @@ -990,6 +992,8 @@ do_split(int nargs) arr = POP_PARAM(); if (arr->type != Node_var_array) fatal(_("split: second argument is not an array")); + check_symtab_functab(arr, "split", + _("%s: cannot use %s as second argument")); if (sep_arr != NULL) { if (sep_arr == arr) @@ -1073,11 +1077,15 @@ do_patsplit(int nargs) sep_arr = POP_PARAM(); if (sep_arr->type != Node_var_array) fatal(_("patsplit: fourth argument is not an array")); + check_symtab_functab(sep_arr, "patsplit", + _("%s: cannot use %s as fourth argument")); } sep = POP(); arr = POP_PARAM(); if (arr->type != Node_var_array) fatal(_("patsplit: second argument is not an array")); + check_symtab_functab(arr, "patsplit", + _("%s: cannot use %s as second argument")); src = TOP_STRING(); -- cgit v1.2.3