Use strlcat not strncat in load_cfg and some other trivial tidying from

Tiago Cunha.
pull/1/head
Nicholas Marriott 2012-12-06 13:06:05 +00:00
parent 8378be03d1
commit 8600fe054b
1 changed files with 15 additions and 13 deletions

28
cfg.c
View File

@ -79,7 +79,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
FILE *f; FILE *f;
u_int n; u_int n;
char *buf, *line, *cause; char *buf, *line, *cause;
size_t len; size_t len, newlen;
struct cmd_list *cmdlist; struct cmd_list *cmdlist;
struct cmd_ctx ctx; struct cmd_ctx ctx;
enum cmd_retval retval; 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)); cfg_add_cause(causes, "%s: %s", path, strerror(errno));
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
n = 0;
cfg_references++; cfg_references++;
n = 0;
line = NULL; line = NULL;
retval = CMD_RETURN_NORMAL; retval = CMD_RETURN_NORMAL;
while ((buf = fgetln(f, &len))) { while ((buf = fgetln(f, &len))) {
if (buf[len - 1] == '\n') if (buf[len - 1] == '\n')
len--; len--;
if (line != NULL) /* Current line is the continuation of the previous one. */
line = xrealloc(line, 1, strlen(line) + len + 1); if (line != NULL) {
else { newlen = strlen(line) + len + 1;
line = xmalloc(len + 1); line = xrealloc(line, 1, newlen);
} else {
newlen = len + 1;
line = xmalloc(newlen);
*line = '\0'; *line = '\0';
} }
/* Append buffer to line. strncat will terminate. */ /* Append current line to the previous. */
strncat(line, buf, len); strlcat(line, buf, newlen);
n++; n++;
/* Continuation: get next line? */ /* Continuation: get next line? */
len = strlen(line); len = strlen(line);
if (len > 0 && line[len - 1] == '\\') { if (len > 0 && line[len - 1] == '\\') {
line[len - 1] = '\0'; line[len - 1] = '\0';
/* Ignore escaped backslash at EOL. */ /* Ignore escaped backslash at EOL. */
if (len > 1 && line[len - 2] != '\\') if (len > 1 && line[len - 2] != '\\')
continue; 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); cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
free(cause); free(cause);
continue; continue;
} else }
free(buf); free(buf);
if (cmdlist == NULL) if (cmdlist == NULL)
continue; continue;
cfg_cause = NULL;
if (ctxin == NULL) { if (ctxin == NULL) {
ctx.msgdata = NULL; ctx.msgdata = NULL;
@ -162,8 +165,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
} }
cmd_list_free(cmdlist); cmd_list_free(cmdlist);
if (cfg_cause != NULL) { if (cfg_cause != NULL) {
cfg_add_cause( cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
causes, "%s: %d: %s", path, n, cfg_cause);
free(cfg_cause); free(cfg_cause);
} }
} }