From 0db5515f43d078189cdc4a0dbe14817385ebea00 Mon Sep 17 00:00:00 2001 From: Claudio Jeker Date: Mon, 18 Nov 2019 08:34:56 +0000 Subject: 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@ --- usr.sbin/rpki-client/tal.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'usr.sbin') 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 * @@ -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; } -- cgit v1.2.3