From: claudio Date: Thu, 13 Sep 2018 11:18:18 +0000 (+0000) Subject: Similar to as-set factor out the code to create a prefix-set into a function. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=95122ff7be114fd7c60003a8eff709cca4505049;p=openbsd 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. OK phessler@ --- diff --git a/usr.sbin/bgpd/parse.y b/usr.sbin/bgpd/parse.y index 28c6a7c7ed7..2a4fcd5595e 100644 --- a/usr.sbin/bgpd/parse.y +++ b/usr.sbin/bgpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.351 2018/09/13 11:16:21 claudio Exp $ */ +/* $OpenBSD: parse.y,v 1.352 2018/09/13 11:18:18 claudio Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -162,6 +162,7 @@ int parseextcommunity(struct filter_extcommunity *, char *, 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($1); } | 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; } @@ -4222,3 +4203,22 @@ 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; +}