From 11c976ec713b975568594cf57d9118f58ba8c511 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 17:46:44 +0000 Subject: One reader for every file, one voice for every finding Seven places opened a file, ran a getline loop, freed the line buffer and closed the handle, and five of them also rewrote the same skip-the-blank- and-comment preamble. authz_read_lines() does it once, and keeps the distinction policy_load needed: a file that would not give an answer is not an empty file. lines_t moves to util.c with it, so edit.c's private copy goes too. verr, vwarn and vok were one function with three tags. cmd_authorize now frees what it read on one path instead of four. --- src/policy.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'src/policy.c') diff --git a/src/policy.c b/src/policy.c index 47a64f1..657e0c9 100644 --- a/src/policy.c +++ b/src/policy.c @@ -10,6 +10,7 @@ #include "authz.h" #include +#include #include #include #include @@ -290,25 +291,30 @@ void policy_free(policy_t *p) int policy_load(policy_t *p) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int lineno = 0; + int rc; - f = fopen(p->path, "r"); - if (f == NULL) { + // A read that stopped because the file would not give an answer is not an + // empty policy: a directory, an I/O error halfway, a mount pulled out. The + // lines read so far are not the file, so this is the broken-host answer (3), + // not the empty-policy answer (0). + + rc = authz_read_lines(p->path, &lines); + if (rc != 0) { p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); + fprintf(stderr, "%s: cannot read the policy: %s\n", p->path, + strerror(errno)); + lines_free(&lines); return -2; } - while ((len = getline(&line, &cap, f)) != -1) { + for (i = 0; i < lines.n; i++) { char *hash; char *text; lineno++; - text = authz_trim(line); + text = authz_trim(lines.v[i]); hash = strchr(text, '#'); if (hash != NULL) { *hash = '\0'; @@ -322,19 +328,7 @@ int policy_load(policy_t *p) else parse_rule(p, text, lineno); } - free(line); - // A read that stopped because the file would not give an answer is not an - // empty policy: a directory, an I/O error halfway, a mount pulled out. The - // lines read so far are not the file, so this is the broken-host answer (3), - // not the empty-policy answer (0). - if (ferror(f)) { - fclose(f); - p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); - return -2; - } - fclose(f); + lines_free(&lines); return p->errors > 0 ? -1 : 0; } -- cgit v1.2.3