From 0adf68291907c76b0f7e7470f3a9afb00e6fa4d2 Mon Sep 17 00:00:00 2001 From: espie Date: Mon, 6 Jan 2014 12:08:18 +0000 Subject: [PATCH] fix error messages, avoid dereferencing null pointers. - the code becomes too indented, pull it into a separate function - add an extra hint, the current_gnode. - specifically, variables may be expanded during target: prereq solving in Suff_FindDeps, this is after parsing, not during command execution, and the only actual indication with have is that we're resolving a prereq of. (this ought to fix mk35, and partially solve mk34) --- usr.bin/make/suff.c | 4 +++- usr.bin/make/var.c | 55 +++++++++++++++++++++++++++++---------------- usr.bin/make/var.h | 3 ++- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/usr.bin/make/suff.c b/usr.bin/make/suff.c index 618c64febb6..e26eb652970 100644 --- a/usr.bin/make/suff.c +++ b/usr.bin/make/suff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: suff.c,v 1.86 2013/11/22 15:47:35 espie Exp $ */ +/* $OpenBSD: suff.c,v 1.87 2014/01/06 12:08:18 espie Exp $ */ /* $NetBSD: suff.c,v 1.13 1996/11/06 17:59:25 christos Exp $ */ /* @@ -1668,10 +1668,12 @@ SuffFindDeps(GNode *gn, Lst slst) if (DEBUG(SUFF)) printf("SuffFindDeps (%s)\n", gn->name); + current_node = gn; if (gn->type & OP_ARCHV) SuffFindArchiveDeps(gn, slst); else SuffFindNormalDeps(gn, slst); + current_node = NULL; } /*- diff --git a/usr.bin/make/var.c b/usr.bin/make/var.c index 6d3317f74de..48e343658ad 100644 --- a/usr.bin/make/var.c +++ b/usr.bin/make/var.c @@ -1,4 +1,4 @@ -/* $OpenBSD: var.c,v 1.94 2013/04/23 14:32:53 espie Exp $ */ +/* $OpenBSD: var.c,v 1.95 2014/01/06 12:08:18 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* @@ -93,6 +93,7 @@ */ char var_Error[] = ""; +GNode *current_node = NULL; /* * Similar to var_Error, but returned when the 'err' flag for Var_Parse is * set false. Why not just use a constant? Well, gcc likes to condense @@ -927,6 +928,39 @@ get_expanded_value(const char *name, const char *ename, int idx, uint32_t k, return val; } +#define ERRMSG1 "Using $< in a non-suffix rule context is a GNUmake idiom " +#define ERRMSG2 "Using undefined dynamic variable $%s " +static void +bad_dynamic_variable(int idx) +{ + Location origin; + + Parse_FillLocation(&origin); + if (idx >= LOCAL_SIZE) + idx = EXTENDED2SIMPLE(idx); + switch(idx) { + case IMPSRC_INDEX: + if (origin.fname) + Fatal(ERRMSG1 "(%s:%lu)", + origin.lineno, origin.fname); + else if (current_node) + Fatal(ERRMSG1 "(prereq of %s)", current_node->name); + else + Fatal(ERRMSG1 "(?)"); + break; + default: + if (origin.fname) + Error(ERRMSG2 "(%s:%lu)", varnames[idx], + origin.lineno, origin.fname); + else if (current_node) + Error(ERRMSG2 "(prereq of %s)", varnames[idx], + current_node->name); + else + Error(ERRMSG2 "(?)", varnames[idx]); + break; + } +} + char * Var_Parse(const char *str, /* The string to parse */ SymTable *ctxt, /* The context for the variable */ @@ -968,24 +1002,7 @@ Var_Parse(const char *str, /* The string to parse */ *freePtr = true; val = Str_dupi(str, tstr); } else { - Location origin; - - Parse_FillLocation(&origin); - if (idx >= LOCAL_SIZE) - idx = EXTENDED2SIMPLE(idx); - switch(idx) { - case IMPSRC_INDEX: - Fatal( -"Using $< in a non-suffix rule context is a GNUmake idiom (line %lu of %s)", - origin.lineno, origin.fname); - break; - default: - Error( -"Using undefined dynamic variable $%s (line %lu of %s)", - varnames[idx], origin.lineno, - origin.fname); - break; - } + bad_dynamic_variable(idx); } } } diff --git a/usr.bin/make/var.h b/usr.bin/make/var.h index 77b2f6820d7..cdc15757395 100644 --- a/usr.bin/make/var.h +++ b/usr.bin/make/var.h @@ -1,6 +1,6 @@ #ifndef VAR_H #define VAR_H -/* $OpenBSD: var.h,v 1.16 2012/10/09 19:39:59 espie Exp $ */ +/* $OpenBSD: var.h,v 1.17 2014/01/06 12:08:18 espie Exp $ */ /* * Copyright (c) 2001 Marc Espie. * @@ -26,6 +26,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +extern GNode *current_node; extern void Var_Init(void); extern void Var_setCheckEnvFirst(bool); -- 2.20.1