summaryrefslogtreecommitdiffstats
path: root/cfg.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2012-12-06 13:06:05 +0000
committerNicholas Marriott <nicm@openbsd.org>2012-12-06 13:06:05 +0000
commit8600fe054bd5db89f8e5758d657018f111be0610 (patch)
tree2c175dd26b548b333d4b3c6ca51ed70ec15569fa /cfg.c
parent8378be03d1dd260fda325c15fb047da8ddd12c1c (diff)
Use strlcat not strncat in load_cfg and some other trivial tidying from
Tiago Cunha.
Diffstat (limited to 'cfg.c')
-rw-r--r--cfg.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/cfg.c b/cfg.c
index b64a4d6a..5b9ef763 100644
--- a/cfg.c
+++ b/cfg.c
@@ -79,7 +79,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
FILE *f;
u_int n;
char *buf, *line, *cause;
- size_t len;
+ size_t len, newlen;
struct cmd_list *cmdlist;
struct cmd_ctx ctx;
enum cmd_retval retval;
@@ -88,31 +88,35 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
cfg_add_cause(causes, "%s: %s", path, strerror(errno));
return (CMD_RETURN_ERROR);
}
- n = 0;
cfg_references++;
+ n = 0;
line = NULL;
retval = CMD_RETURN_NORMAL;
while ((buf = fgetln(f, &len))) {
if (buf[len - 1] == '\n')
len--;
- if (line != NULL)
- line = xrealloc(line, 1, strlen(line) + len + 1);
- else {
- line = xmalloc(len + 1);
+ /* Current line is the continuation of the previous one. */
+ if (line != NULL) {
+ newlen = strlen(line) + len + 1;
+ line = xrealloc(line, 1, newlen);
+ } else {
+ newlen = len + 1;
+ line = xmalloc(newlen);
*line = '\0';
}
- /* Append buffer to line. strncat will terminate. */
- strncat(line, buf, len);
+ /* Append current line to the previous. */
+ strlcat(line, buf, newlen);
n++;
/* Continuation: get next line? */
len = strlen(line);
if (len > 0 && line[len - 1] == '\\') {
line[len - 1] = '\0';
+
/* Ignore escaped backslash at EOL. */
if (len > 1 && line[len - 2] != '\\')
continue;
@@ -127,11 +131,10 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
free(cause);
continue;
- } else
- free(buf);
+ }
+ free(buf);
if (cmdlist == NULL)
continue;
- cfg_cause = NULL;
if (ctxin == NULL) {
ctx.msgdata = NULL;
@@ -162,8 +165,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
}
cmd_list_free(cmdlist);
if (cfg_cause != NULL) {
- cfg_add_cause(
- causes, "%s: %d: %s", path, n, cfg_cause);
+ cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
free(cfg_cause);
}
}