Skip to contentSkip to conversation

parser_bison: simplify hook_spec rule

PATCH New Pablo Neira Ayuso 1 participant 3 messages Mar 15, 2016 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/3 parser_bison: simplify hook_spec rule

Consolidate this rule by introducing the dev_spec and prio_spec, we save 50 LOC with this patch.

---
 src/parser_bison.y | 66 +++++++++++++-----------------------------------------
 1 file changed, 15 insertions(+), 51 deletions(-)
src/parser_bison.y +15 -51
index 90978ab..24c0969 100644
@@ -423,7 +423,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %destructor { handle_free(&$$); } table_spec chain_spec chain_identifier ruleid_spec ruleset_spec
 %type <handle>			set_spec set_identifier
 %destructor { handle_free(&$$); } set_spec set_identifier
-%type <val>			handle_spec family_spec family_spec_explicit position_spec chain_policy
+%type <val>			handle_spec family_spec family_spec_explicit position_spec chain_policy prio_spec
+
+%type <string>			dev_spec
+%destructor { xfree($$); }	dev_spec
 
 %type <table>			table_block_alloc table_block
 %destructor { close_scope(state); table_free($$); }	table_block_alloc
@@ -1096,41 +1099,7 @@ type_identifier		:	STRING	{ $$ = $1; }
 			|	MARK	{ $$ = xstrdup("mark"); }
 			;
 
-hook_spec		:	TYPE		STRING		HOOK		STRING		PRIORITY	NUM
-			{
-				$<chain>0->type		= chain_type_name_lookup($2);
-				if ($<chain>0->type == NULL) {
-					erec_queue(error(&@2, "unknown chain type %s", $2),
-						   state->msgs);
-					YYERROR;
-				}
-				$<chain>0->hookstr	= chain_hookname_lookup($4);
-				if ($<chain>0->hookstr == NULL) {
-					erec_queue(error(&@4, "unknown chain hook %s", $4),
-						   state->msgs);
-					YYERROR;
-				}
-				$<chain>0->priority	= $6;
-				$<chain>0->flags	|= CHAIN_F_BASECHAIN;
-			}
-			|	TYPE		STRING		HOOK		STRING		PRIORITY	DASH	NUM
-			{
-				$<chain>0->type		= chain_type_name_lookup($2);
-				if ($<chain>0->type == NULL) {
-					erec_queue(error(&@2, "unknown type name %s", $2),
-						   state->msgs);
-					YYERROR;
-				}
-				$<chain>0->hookstr	= chain_hookname_lookup($4);
-				if ($<chain>0->hookstr == NULL) {
-					erec_queue(error(&@4, "unknown hook name %s", $4),
-						   state->msgs);
-					YYERROR;
-				}
-				$<chain>0->priority	= -$7;
-				$<chain>0->flags	|= CHAIN_F_BASECHAIN;
-			}
-			|	TYPE		STRING		HOOK		STRING		DEVICE	STRING	PRIORITY	NUM
+hook_spec		:	TYPE		STRING		HOOK		STRING		dev_spec	PRIORITY	prio_spec
 			{
 				$<chain>0->type		= chain_type_name_lookup($2);
 				if ($<chain>0->type == NULL) {
@@ -1144,25 +1113,20 @@ hook_spec		:	TYPE		STRING		HOOK		STRING		PRIORITY	NUM
 						   state->msgs);
 					YYERROR;
 				}
-				$<chain>0->dev		= $6;
-				$<chain>0->priority	= $8;
-				$<chain>0->flags	|= CHAIN_F_BASECHAIN;
-			}
-			|	TYPE		STRING		HOOK		STRING		DEVICE	STRING	PRIORITY	DASH	NUM
-			{
-				$<chain>0->type		= chain_type_name_lookup($2);
-				if ($<chain>0->type == NULL) {
-					erec_queue(error(&@2, "unknown type name %s", $2),
-						   state->msgs);
-					YYERROR;
-				}
-				$<chain>0->hookstr	= chain_hookname_lookup($4);
-				$<chain>0->dev		= $6;
-				$<chain>0->priority	= -$9;
+				$<chain>0->dev		= $5;
+				$<chain>0->priority	= $7;
 				$<chain>0->flags	|= CHAIN_F_BASECHAIN;
 			}
 			;
 
+prio_spec		:	NUM			{ $$ = $1; }
+			|	DASH	NUM		{ $$ = -$2; }
+			;
+
+dev_spec		:	DEVICE	STRING		{ $$ = $2; }
+			|	/* empty */		{ $$ = NULL; }
+			;
+
 policy_spec		:	POLICY		chain_policy
 			{
 				if ($<chain>0->policy != -1) {
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 2/3 parser_bison: duplicate string returned by chain_type_name_lookup()

This chain type string is released via chain_free() since b7cb6915a88f, so duplicate it so we don't try to release statically allocated memory.

Fixes b7cb6915a88f ("rule: Remove memory leak")
---
 src/parser_bison.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/parser_bison.y +1 -1
index 24c0969..0592b68 100644
@@ -1101,7 +1101,7 @@ type_identifier		:	STRING	{ $$ = $1; }
 
 hook_spec		:	TYPE		STRING		HOOK		STRING		dev_spec	PRIORITY	prio_spec
 			{
-				$<chain>0->type		= chain_type_name_lookup($2);
+				$<chain>0->type		= xstrdup(chain_type_name_lookup($2));
 				if ($<chain>0->type == NULL) {
 					erec_queue(error(&@2, "unknown chain type %s", $2),
 						   state->msgs);
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 3/3 parser_bison: release parsed type and hook name strings

In reply to Pablo Neira Ayuso

The scanner allocates memory for this, so release them given that we don't attach them to any object.

==6277== 42 bytes in 6 blocks are definitely lost in loss record 2 of 4

==6277==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==6277==    by 0x57AC9D9: strdup (strdup.c:42)
==6277==    by 0x41B82D: xstrdup (utils.c:64)
==6277==    by 0x41F510: nft_lex (scanner.l:511)
==6277==    by 0x427FD1: nft_parse (parser_bison.c:3690)
==6277==    by 0x4063AC: nft_run (main.c:231)
==6277==    by 0x40600C: main (main.c:361)
---
 src/parser_bison.y | 4 ++++
 1 file changed, 4 insertions(+)
src/parser_bison.y +4 -0
index 0592b68..9e86f26 100644
@@ -1107,12 +1107,16 @@ hook_spec		:	TYPE		STRING		HOOK		STRING		dev_spec	PRIORITY	prio_spec
 						   state->msgs);
 					YYERROR;
 				}
+				xfree($2);
+
 				$<chain>0->hookstr	= chain_hookname_lookup($4);
 				if ($<chain>0->hookstr == NULL) {
 					erec_queue(error(&@4, "unknown chain hook %s", $4),
 						   state->msgs);
 					YYERROR;
 				}
+				xfree($4);
+
 				$<chain>0->dev		= $5;
 				$<chain>0->priority	= $7;
 				$<chain>0->flags	|= CHAIN_F_BASECHAIN;
Signature · 1 line
2.1.4