If tal_read_file() returns a NULL buffer, which can happen with an empty
file or one containing only comments, the NULL value eventually
propogates to tal_parse_buffer() where it crashes on strchr().

This patch also adds/fixes documentation to the some functions,
specifies a NUL terminator instead of zero, and removes an unused variable.
Index: tal.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/tal.c,v
retrieving revision 1.13
diff -u -p -u -r1.13 tal.c
--- tal.c	6 Nov 2019 08:29:03 -0000	1.13
+++ tal.c	17 Nov 2019 21:02:07 -0000
@@ -40,7 +40,6 @@ tal_parse_buffer(const char *fn, char *b
 	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;
 }
 

Reply via email to