Skip to contentSkip to conversation

glob and maximum number of includes

PATCH In review Pablo Neira Ayuso 1 participant 6 messages Feb 11, 2020 – Feb 12, 2020 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover glob and maximum number of includes

Hi Laurent,

This approach maintains an array of stacks per depth.

The initial three patches comes as a preparation. The last patch is aiming to fix the issue with glob and the maximum number of includes.

Thanks for your detailed feedback and explanations.

Pablo Neira Ayuso (4):
scanner: call scanner_push_file() after scanner_push_file()
scanner: add indesc_file_alloc() helper function
scanner: call scanner_push_indesc() after scanner_push_file()
scanner: multi-level input file stack for glob

 include/list.h     |  30 ++++++++++++++
 include/parser.h   |   3 +-
 src/parser_bison.y |   5 ++-
 src/scanner.l      | 120 +++++++++++++++++++++++++++++++++++++----------------
 4 files changed, 119 insertions(+), 39 deletions(-)
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 1/4 scanner: call scanner_push_file() after scanner_push_file()

Update include_file() to return FILE *.

---
 src/scanner.l | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
src/scanner.l +18 -8
index ecf2354e3c2f..fe70df5c18ec 100644
@@ -714,8 +714,8 @@ static void scanner_push_file(struct nft_ctx *nft, void *scanner,
 	scanner_push_indesc(state, indesc);
 }
 
-static int include_file(struct nft_ctx *nft, void *scanner,
-			const char *filename, const struct location *loc)
+static FILE *include_file(struct nft_ctx *nft, void *scanner,
+			  const char *filename, const struct location *loc)
 {
 	struct parser_state *state = yyget_extra(scanner);
 	struct error_record *erec;
@@ -733,11 +733,11 @@ static int include_file(struct nft_ctx *nft, void *scanner,
 			     filename, strerror(errno));
 		goto err;
 	}
-	scanner_push_file(nft, scanner, f, filename, loc);
-	return 0;
+
+	return f;
 err:
 	erec_queue(erec, state->msgs);
-	return -1;
+	return NULL;
 }
 
 static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
@@ -749,6 +749,7 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
 	glob_t glob_data;
 	unsigned int i;
 	int flags = 0;
+	FILE *f;
 	int ret;
 	char *p;
 
@@ -804,9 +805,11 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
 			if (len == 0 || path[len - 1] == '/')
 				continue;
 
-			ret = include_file(nft, scanner, path, loc);
-			if (ret != 0)
+			f = include_file(nft, scanner, path, loc);
+			if (!f)
 				goto err;
+
+			scanner_push_file(nft, scanner, f, path, loc);
 		}
 
 		globfree(&glob_data);
@@ -841,7 +844,14 @@ err:
 int scanner_read_file(struct nft_ctx *nft, const char *filename,
 		      const struct location *loc)
 {
-	return include_file(nft, nft->scanner, filename, loc);
+	FILE *f;
+
+	f = include_file(nft, nft->scanner, filename, loc);
+	if (!f)
+		return -1;
+
+	scanner_push_file(nft, nft->scanner, f, filename, loc);
+	return 0;
 }
 
 static bool search_in_include_path(const char *filename)
Signature · 1 line
2.11.0
author maintainer
#2 lore raw

PATCH 2/4 scanner: add indesc_file_alloc() helper function

In reply to Pablo Neira Ayuso

New helper function to allocate the file input_descriptor.

---
 src/scanner.l | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
src/scanner.l +17 -9
index fe70df5c18ec..56f6e9956791 100644
@@ -665,6 +665,22 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 %%
 
+static struct input_descriptor *
+indesc_file_alloc(FILE *f, const char *filename, const struct location *loc)
+{
+	struct input_descriptor *indesc;
+
+	indesc = xzalloc(sizeof(struct input_descriptor));
+	if (loc != NULL)
+		indesc->location = *loc;
+	indesc->type	= INDESC_FILE;
+	indesc->name	= xstrdup(filename);
+	indesc->f	= f;
+	init_pos(indesc);
+
+	return indesc;
+}
+
 static void scanner_push_indesc(struct parser_state *state,
 				struct input_descriptor *indesc)
 {
@@ -702,15 +718,7 @@ static void scanner_push_file(struct nft_ctx *nft, void *scanner,
 	b = yy_create_buffer(f, YY_BUF_SIZE, scanner);
 	yypush_buffer_state(b, scanner);
 
-	indesc = xzalloc(sizeof(struct input_descriptor));
-
-	if (loc != NULL)
-		indesc->location = *loc;
-	indesc->type	= INDESC_FILE;
-	indesc->name	= xstrdup(filename);
-	indesc->f	= f;
-	init_pos(indesc);
-
+	indesc = indesc_file_alloc(f, filename, loc);
 	scanner_push_indesc(state, indesc);
 }
 
Signature · 1 line
2.11.0
author maintainer
#3 lore raw

PATCH 3/4 scanner: call scanner_push_indesc() after scanner_push_file()

In reply to Pablo Neira Ayuso

Just a preparation patch.

---
 src/scanner.l | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
src/scanner.l +12 -9
index 56f6e9956791..9584f61c489c 100644
@@ -707,19 +707,16 @@ static void scanner_pop_buffer(yyscan_t scanner)
 	scanner_pop_indesc(state);
 }
 
-static void scanner_push_file(struct nft_ctx *nft, void *scanner,
-			      FILE *f, const char *filename,
-			      const struct location *loc)
+static struct input_descriptor *
+scanner_push_file(struct nft_ctx *nft, void *scanner, FILE *f,
+		  const char *filename, const struct location *loc)
 {
-	struct parser_state *state = yyget_extra(scanner);
-	struct input_descriptor *indesc;
 	YY_BUFFER_STATE b;
 
 	b = yy_create_buffer(f, YY_BUF_SIZE, scanner);
 	yypush_buffer_state(b, scanner);
 
-	indesc = indesc_file_alloc(f, filename, loc);
-	scanner_push_indesc(state, indesc);
+	return indesc_file_alloc(f, filename, loc);
 }
 
 static FILE *include_file(struct nft_ctx *nft, void *scanner,
@@ -805,6 +802,7 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
 
 		/* reverse alphabetical order due to stack */
 		for (i = glob_data.gl_pathc; i > 0; i--) {
+			struct input_descriptor *indesc;
 
 			path = glob_data.gl_pathv[i-1];
 
@@ -817,7 +815,8 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
 			if (!f)
 				goto err;
 
-			scanner_push_file(nft, scanner, f, path, loc);
+			indesc = scanner_push_file(nft, scanner, f, path, loc);
+			scanner_push_indesc(state, indesc);
 		}
 
 		globfree(&glob_data);
@@ -852,13 +851,17 @@ err:
 int scanner_read_file(struct nft_ctx *nft, const char *filename,
 		      const struct location *loc)
 {
+	struct parser_state *state = yyget_extra(nft->scanner);
+	struct input_descriptor *indesc;
 	FILE *f;
 
 	f = include_file(nft, nft->scanner, filename, loc);
 	if (!f)
 		return -1;
 
-	scanner_push_file(nft, nft->scanner, f, filename, loc);
+	indesc = scanner_push_file(nft, nft->scanner, f, filename, loc);
+	scanner_push_indesc(state, indesc);
+
 	return 0;
 }
 
Signature · 1 line
2.11.0
author maintainer
#4 lore raw

PATCH 4/4 scanner: multi-level input file stack for glob

In reply to Pablo Neira Ayuso

This patch updates indesc_list to become an array of stacks. Each stack represents the files that have been included at this depth.

The scanner_add_indesc() function adds the indesc to this depth, this is called in case the user specifies wildcards.

Otherwise, the scanner_push_indesc() function for regular inclusion.

---
 include/list.h     | 30 +++++++++++++++++++++++++++++
 include/parser.h   |  3 +--
 src/parser_bison.y |  5 ++++-
 src/scanner.l      | 55 ++++++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 76 insertions(+), 17 deletions(-)
include/list.h +30 -0
index 75d292124010..29f5ca739632 100644
@@ -22,6 +22,17 @@ struct list_head {
 	struct list_head *next, *prev;
 };
 
+/**
+ * list_is_first -- tests whether @list is the first entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_first(const struct list_head *list,
+				const struct list_head *head)
+{
+	return list->prev == head;
+}
+
 #define LIST_HEAD_INIT(name) { &(name), &(name) }
 
 #define LIST_HEAD(name) \
@@ -623,3 +634,22 @@ static inline void hlist_add_after(struct hlist_node *n,
 	     pos = n)
 
 #endif
+
+/**
+ * list_prev_entry - get the prev element in list
+ * @pos:        the type * to cursor
+ * @member:     the name of the list_head within the struct.
+ */
+#define list_prev_entry(pos, member) \
+	list_entry((pos)->member.prev, typeof(*(pos)), member)
+
+/**
+ * list_last_entry - get the last element from a list
+ * @ptr:        the list head to take the element from.
+ * @type:       the type of the struct this is embedded in.
+ * @member:     the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(ptr, type, member) \
+	list_entry((ptr)->prev, type, member)
include/parser.h +1 -2
index 949284d9466c..d24133059203 100644
@@ -15,9 +15,8 @@
 
 struct parser_state {
 	struct input_descriptor		*indesc;
-	struct input_descriptor		*indescs[MAX_INCLUDE_DEPTH];
 	unsigned int			indesc_idx;
-	struct list_head		indesc_list;
+	struct list_head		indesc_list[MAX_INCLUDE_DEPTH];
 
 	struct list_head		*msgs;
 	unsigned int			nerrs;
src/parser_bison.y +4 -1
index f5d7026a8574..216126472687 100644
@@ -45,11 +45,14 @@ void parser_init(struct nft_ctx *nft, struct parser_state *state,
 		 struct list_head *msgs, struct list_head *cmds,
 		 struct scope *top_scope)
 {
+	int i;
+
 	memset(state, 0, sizeof(*state));
 	state->msgs = msgs;
 	state->cmds = cmds;
 	state->scopes[0] = scope_init(top_scope, NULL);
-	init_list_head(&state->indesc_list);
+	for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
+		init_list_head(&state->indesc_list[i]);
 }
 
 static void yyerror(struct location *loc, struct nft_ctx *nft, void *scanner,
src/scanner.l +41 -14

85 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#5 lore raw
In reply to Pablo Neira Ayuso
Quoted 6 lines · Pablo Neira Ayuso

Hi Laurent,

This approach maintains an array of stacks per depth.

The initial three patches comes as a preparation. The last patch is aiming to fix the issue with glob and the maximum number of includes.

Hm, unfortunately, my patchset does not work. I'm going to toss this and go back to your approach.