summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2019-11-18 08:34:56 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2019-11-18 08:34:56 +0000
commit0db5515f43d078189cdc4a0dbe14817385ebea00 (patch)
tree8b8c37f14be730c8bf04b92185bfb2f7a7fcebe3 /usr.sbin
parent9ac55649285c6dc069b00608af10eca042177f8d (diff)
tal_read_file() should error out instead of returning a NULL buffer.
This can happen with an empty file or one consisting only of comments. Do some additional cleanup of comments, remove a unused variable and change a zero into a NUL for clarity. From kristaps@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/rpki-client/tal.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/usr.sbin/rpki-client/tal.c b/usr.sbin/rpki-client/tal.c
index 921a11b09dc..948e64b0a07 100644
--- a/usr.sbin/rpki-client/tal.c
+++ b/usr.sbin/rpki-client/tal.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tal.c,v 1.13 2019/11/06 08:29:03 claudio Exp $ */
+/* $OpenBSD: tal.c,v 1.14 2019/11/18 08:34:55 claudio Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -40,7 +40,6 @@ tal_parse_buffer(const char *fn, char *buf)
char *nl, *line;
unsigned char *b64 = NULL;
size_t sz;
- ssize_t linelen;
int rc = 0, b64sz;
struct tal *tal = NULL;
enum rtype rp;
@@ -134,10 +133,9 @@ out:
}
/*
- * Parse a TAL from a file conformant to RFC 7730.
- * Returns the encoded data or NULL on failure.
- * Failure can be any number of things: failure to open file, allocate
- * memory, bad syntax, etc.
+ * Parse a TAL from "buf" conformant to RFC 7730 originally from a file
+ * named "fn".
+ * Returns the encoded data or NULL on syntax failure.
*/
struct tal *
tal_parse(const char *fn, char *buf)
@@ -160,11 +158,19 @@ tal_parse(const char *fn, char *buf)
if ((p->descr = malloc(dlen + 1)) == NULL)
err(EXIT_FAILURE, NULL);
memcpy(p->descr, d, dlen);
- p->descr[dlen] = 0;
+ p->descr[dlen] = '\0';
return p;
}
+/*
+ * Read the file named "file" into a returned, NUL-terminated buffer.
+ * This replaces CRLF terminators with plain LF, if found, and also
+ * elides document-leading comment lines starting with "#".
+ * Files may not exceeds 4096 bytes.
+ * This function exits on failure, so it always returns a buffer with
+ * TAL data.
+ */
char *
tal_read_file(const char *file)
{
@@ -222,7 +228,8 @@ tal_read_file(const char *file)
if (ferror(in))
err(EXIT_FAILURE, "getline: %s", file);
fclose(in);
-
+ if (buf == NULL)
+ errx(EXIT_FAILURE, "%s: no data", file);
return buf;
}