CBL-Mariner/SPECS/nftables/0008-scanner-move-the-file-...

112 строки
3.4 KiB
Diff

From 1af5611a69b9d7e62018fbcbcadd35f5d8eca050 Mon Sep 17 00:00:00 2001
From: Laurent Fasnacht <fasnacht@protonmail.ch>
Date: Mon, 10 Feb 2020 10:17:21 +0000
Subject: [PATCH] scanner: move the file descriptor to be in the
input_descriptor structure
This prevents a static allocation of file descriptors array, thus allows
more flexibility.
Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
(cherry picked from commit 209c4d901e90e46faa14d1f38cb000f79514b3b2)
Signed-off-by: Phil Sutter <psutter@redhat.com>
---
include/nftables.h | 3 ++-
src/scanner.l | 18 +++++++++---------
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/include/nftables.h b/include/nftables.h
index 90d331960ef29..07726e4dd5a40 100644
--- a/include/nftables.h
+++ b/include/nftables.h
@@ -122,7 +122,6 @@ struct nft_ctx {
void *scanner;
struct scope *top_scope;
void *json_root;
- FILE *f[MAX_INCLUDE_DEPTH];
};
enum nftables_exit_codes {
@@ -176,6 +175,7 @@ enum input_descriptor_types {
* struct input_descriptor
*
* @location: location, used for include statements
+ * @f: file descriptor
* @type: input descriptor type
* @name: name describing the input
* @union: buffer or file descriptor, depending on type
@@ -186,6 +186,7 @@ enum input_descriptor_types {
*/
struct input_descriptor {
struct list_head list;
+ FILE *f;
struct location location;
enum input_descriptor_types type;
const char *name;
diff --git a/src/scanner.l b/src/scanner.l
index 25db4d3f24eec..d1d1154a8c811 100644
--- a/src/scanner.l
+++ b/src/scanner.l
@@ -690,13 +690,14 @@ static void scanner_pop_buffer(yyscan_t scanner)
}
static void scanner_push_file(struct nft_ctx *nft, void *scanner,
- const char *filename, const struct location *loc)
+ 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(nft->f[state->indesc_idx], YY_BUF_SIZE, scanner);
+ b = yy_create_buffer(f, YY_BUF_SIZE, scanner);
yypush_buffer_state(b, scanner);
indesc = xzalloc(sizeof(struct input_descriptor));
@@ -705,6 +706,7 @@ static void scanner_push_file(struct nft_ctx *nft, void *scanner,
indesc->location = *loc;
indesc->type = INDESC_FILE;
indesc->name = xstrdup(filename);
+ indesc->f = f;
init_pos(indesc);
scanner_push_indesc(state, indesc);
@@ -730,8 +732,7 @@ static int include_file(struct nft_ctx *nft, void *scanner,
filename, strerror(errno));
goto err;
}
- nft->f[state->indesc_idx] = f;
- scanner_push_file(nft, scanner, filename, loc);
+ scanner_push_file(nft, scanner, f, filename, loc);
return 0;
err:
erec_queue(erec, state->msgs);
@@ -943,6 +944,10 @@ static void input_descriptor_list_destroy(struct parser_state *state)
struct input_descriptor *indesc, *next;
list_for_each_entry_safe(indesc, next, &state->indesc_list, list) {
+ if (indesc->f) {
+ fclose(indesc->f);
+ indesc->f = NULL;
+ }
list_del(&indesc->list);
input_descriptor_destroy(indesc);
}
@@ -954,11 +959,6 @@ void scanner_destroy(struct nft_ctx *nft)
do {
yypop_buffer_state(nft->scanner);
-
- if (nft->f[state->indesc_idx]) {
- fclose(nft->f[state->indesc_idx]);
- nft->f[state->indesc_idx] = NULL;
- }
} while (state->indesc_idx--);
input_descriptor_list_destroy(state);
--
2.28.0