Skip to contentSkip to conversation

erec: Make error messages in nft consistent

PATCH Superseded Elise Lennion 2 participants 3 messages Nov 15, 2016 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erec: Make error messages in nft consistent

Error messages in nft should start with "syntax error" to keep consistency. A new function add_syntax_error() was created to add this prefix when necessary.

---
 include/erec.h |  3 +++
 src/erec.c     | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+)
include/erec.h +3 -0
index 36e0efa..82754e0 100644
@@ -52,9 +52,12 @@ extern void erec_add_location(struct error_record *erec,
 #define warning(loc, fmt, args...) \
 	erec_create(EREC_WARNING, (loc), (fmt), ## args)
 
+extern void add_syntax_error(struct error_record *erec);
+
 static inline void erec_queue(struct error_record *erec,
 			      struct list_head *queue)
 {
+	add_syntax_error(erec);
 	list_add_tail(&erec->list, queue);
 }
 
src/erec.c +23 -0
index 3603216..e4d7aef 100644
@@ -194,3 +194,26 @@ int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx,
 	erec_queue(erec, ctx->msgs);
 	return -1;
 }
+
+void add_syntax_error(struct error_record *erec){
+	if (erec) {
+		const char *stxmsg = "syntax error, ";
+		int mlen = strlen(erec->msg);
+		int slen = strlen(stxmsg);
+		char *tmp;
+
+		if (!strncmp(erec->msg, stxmsg, slen - 2))
+			return;
+
+		if (!mlen)
+			slen -= 2;
+
+		tmp = realloc(erec->msg, slen + mlen + 1);
+		if (tmp) {
+			erec->msg = tmp;
+			memmove(&(erec->msg[slen]), erec->msg, mlen);
+			memcpy(erec->msg, stxmsg, slen);
+			erec->msg[slen + mlen] = '\0';
+		}
+	}
+}
Signature · 1 line
2.7.4
#1 lore raw
Quoted 3 lines · Elise Lennion

Error messages in nft should start with "syntax error" to keep consistency. A new function add_syntax_error() was created to add this prefix when necessary.

Probably you can just add EREC_SYNTAX_ERROR to enum error_record_types, then add:

#define syntax_error(loc, fmt, args...) \
       erec_create(EREC_SYNTA_ERROR, (loc), (fmt), ## args)

and use it from src/parser_bison.y

Thanks!

#2 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Elise Lennion

Error messages in nft should start with "syntax error" to keep consistency. A new function add_syntax_error() was created to add this prefix when necessary.

Probably you can just add EREC_SYNTAX_ERROR to enum error_record_types, then add:

#define syntax_error(loc, fmt, args...) \
       erec_create(EREC_SYNTA_ERROR, (loc), (fmt), ## args)

Actually, git grep "erec_queue(error(" tells me all:

        erec_queue(error(...

occur in parser_bison.y, so we can just update #define error in erec.h

You will have to define something like __error, which doesn't include the "syntax error" string, as bison is already including it in this spot below:

static void yyerror(struct location *loc, void *scanner,
struct parser_state *state, const char *s)

{
        erec_queue(__error(loc, "%s", s), state->msgs);
}

So it will be a simple patch in the end.

Thanks.