stun

simple point to point tunnel
git clone git://git.2f30.org/stun.git
Log | Files | Refs | README

commit 5191f532ca6d75b5eaf45421363e7992ff8c2519
parent 302fc0ed720e06adbb1793ed172b0565b2807c53
Author: sin <sin@2f30.org>
Date:   Wed Apr 27 23:07:05 +0100

Free memory on config parsing failure

Diffstat:
parse.y | 53++++++++++++++++++++++++++++++-----------------------
stun.c | 6+++---
2 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/parse.y b/parse.y @@ -111,11 +111,11 @@ include : INCLUDE STRING { ; main : NETWORK STRING optnl networkopts_b { - struct netcfg *np; + struct netcfg *n; - TAILQ_FOREACH(np, &netcfgs, entry) { - if (strcmp(np->name, $2) == 0) { - yyerror("duplicate network block: %s", np->name); + TAILQ_FOREACH(n, &netcfgs, entry) { + if (strcmp(n->name, $2) == 0) { + yyerror("duplicate network block: %s", n->name); resetnetconfig(&curnet); free($2); YYERROR; @@ -138,10 +138,10 @@ main : NETWORK STRING optnl networkopts_b { } free($2); - if (!(np = calloc(1, sizeof(*np)))) + if (!(n = calloc(1, sizeof(*n)))) fatal("calloc"); - *np = curnet; - TAILQ_INSERT_TAIL(&netcfgs, np, entry); + *n = curnet; + TAILQ_INSERT_TAIL(&netcfgs, n, entry); resetnetconfig(&curnet); } ; @@ -576,21 +576,21 @@ popfile(void) } static void -resetnetconfig(struct netcfg *np) +resetnetconfig(struct netcfg *n) { - memset(np, 0, sizeof(*np)); - strlcpy(np->cipher, DEFCIPHER, sizeof(np->cipher)); - strlcpy(np->port, DEFPORT, sizeof(np->port)); - strlcpy(np->user, STUNUSER, sizeof(np->user)); - np->proto = AF_INET; - np->devtype = TUNDEV; - np->timeout = RECONNECTTIMEO; + memset(n, 0, sizeof(*n)); + strlcpy(n->cipher, DEFCIPHER, sizeof(n->cipher)); + strlcpy(n->port, DEFPORT, sizeof(n->port)); + strlcpy(n->user, STUNUSER, sizeof(n->user)); + n->proto = AF_INET; + n->devtype = TUNDEV; + n->timeout = RECONNECTTIMEO; } int parseconf(const char *filename) { - struct netcfg *np; + struct netcfg *n, *ntmp; int errors = 0; TAILQ_INIT(&netcfgs); @@ -612,23 +612,30 @@ parseconf(const char *filename) return -1; } - TAILQ_FOREACH(np, &netcfgs, entry) { - if (np->host[0] == '\0' && np->bindaddr[0] == '\0') { + TAILQ_FOREACH(n, &netcfgs, entry) { + if (n->host[0] == '\0' && n->bindaddr[0] == '\0') { logerrx("network %s: no connect or listen keyword", - np->name); + n->name); errors++; } - if (np->passwd[0] == '\0') { + if (n->passwd[0] == '\0') { logerrx("network %s: passwd not specified", - np->name); + n->name); errors++; } - if (np->ifname[0] == '\0') { + if (n->ifname[0] == '\0') { logerrx("network %s: interface not specified", - np->name); + n->name); errors++; } } + if (errors != 0) { + TAILQ_FOREACH_SAFE(n, &netcfgs, entry, ntmp) { + TAILQ_REMOVE(&netcfgs, n, entry); + free(n); + } + } + return errors != 0 ? -1 : 0; } diff --git a/stun.c b/stun.c @@ -107,7 +107,7 @@ int main(int argc, char *argv[]) { struct pident *pe, *petmp; - struct netcfg *np; + struct netcfg *n; struct rlimit rlim; char *confname = PATHCONFIG; int ret, status, checkonly = 0; @@ -153,9 +153,9 @@ main(int argc, char *argv[]) } ret = 0; - TAILQ_FOREACH(np, &netcfgs, entry) { + TAILQ_FOREACH(n, &netcfgs, entry) { /* global options per stun instance */ - netconfig = *np; + netconfig = *n; switch (pid = fork()) { case -1: ret = 1;