Similar to as-set factor out the code to create a prefix-set into a function.
Makes all a bit nicer and as an added bonus fixes a memory leak.
--
:wq Claudio
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
retrieving revision 1.350
diff -u -p -r1.350 parse.y
--- parse.y 10 Sep 2018 11:09:25 -0000 1.350
+++ parse.y 13 Sep 2018 09:48:54 -0000
@@ -162,6 +162,7 @@ int parseextcommunity(struct filter_ex
static int new_as_set(char *);
static void add_as_set(u_int32_t);
static void done_as_set(void);
+static int new_prefix_set(char *);
typedef struct {
union {
@@ -418,41 +419,21 @@ as_set_l : as4number_any { add_as_set(
| as_set_l comma as4number_any { add_as_set($3); }
prefixset : PREFIXSET STRING '{' optnl {
- if (find_prefixset($2, conf->prefixsets) != NULL) {
- yyerror("duplicate prefixset %s", $2);
+ if (new_prefix_set($2) != 0) {
free($2);
YYERROR;
}
- if ((curpset = calloc(1, sizeof(*curpset))) == NULL)
- fatal("prefixset");
- if (strlcpy(curpset->name, $2, sizeof(curpset->name)) >=
- sizeof(curpset->name)) {
- yyerror("prefix-set \"%s\" too long: max %zu",
- $2, sizeof(curpset->name) - 1);
- free($2);
- YYERROR;
- }
- SIMPLEQ_INIT(&curpset->psitems);
+ free($2);
} prefixset_l optnl '}' {
SIMPLEQ_INSERT_TAIL(conf->prefixsets, curpset, entry);
curpset = NULL;
}
| PREFIXSET STRING '{' optnl '}' {
- if (find_prefixset($2, conf->prefixsets) != NULL) {
- yyerror("duplicate prefixset %s", $2);
+ if (new_prefix_set($2) != 0) {
free($2);
YYERROR;
}
- if ((curpset = calloc(1, sizeof(*curpset))) == NULL)
- fatal("prefixset");
- if (strlcpy(curpset->name, $2, sizeof(curpset->name)) >=
- sizeof(curpset->name)) {
- yyerror("prefix-set \"%s\" too long: max %zu",
- $2, sizeof(curpset->name) - 1);
- free($2);
- YYERROR;
- }
- SIMPLEQ_INIT(&curpset->psitems);
+ free($2);
SIMPLEQ_INSERT_TAIL(conf->prefixsets, curpset, entry);
curpset = NULL;
}
@@ -4209,4 +4190,23 @@ static void
done_as_set(void)
{
curaset = NULL;
+}
+
+static int
+new_prefix_set(char *name)
+{
+ if (find_prefixset(name, conf->prefixsets) != NULL) {
+ yyerror("prefix-set \"%s\" already exists", name);
+ return -1;
+ }
+ if ((curpset = calloc(1, sizeof(*curpset))) == NULL)
+ fatal("prefixset");
+ if (strlcpy(curpset->name, name, sizeof(curpset->name)) >=
+ sizeof(curpset->name)) {
+ yyerror("prefix-set \"%s\" too long: max %zu",
+ name, sizeof(curpset->name) - 1);
+ return -1;
+ }
+ SIMPLEQ_INIT(&curpset->psitems);
+ return 0;
}