Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Sep 2020 14:50:15 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r365302 - head/lib/libc/tests/resolv
Message-ID:  <202009031450.083EoFFV071556@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Thu Sep  3 14:50:15 2020
New Revision: 365302
URL: https://svnweb.freebsd.org/changeset/base/365302

Log:
  Various fixes to the load() function.
  
  - Use getline() instead of fgetln().  This ensures the returned string
    is always null-terminated without losing the last character if the
    last line in a file doesn't have a newline.  Also, while fgetln says
    the returned buffer can be modified, that doesn't actually seem safe
    as the current implementation means you are modifying stdio's
    internal buffer.
  
  - Remove a spurious if before an ATF_REQUIRE that was clearly supposed
    to be non-optional.
  
  - Remove a pointless compare of 'ptr' against '\0' (really NULL) that
    duplicated the middle condition in the for().
  
  - Once a comment is found, skip the rest of the line, not just the
    current word.
  
  Reviewed by:	kevans
  Obtained from:	CheriBSD
  Sponsored by:	DARPA
  Differential Revision:	https://reviews.freebsd.org/D26278

Modified:
  head/lib/libc/tests/resolv/resolv_test.c

Modified: head/lib/libc/tests/resolv/resolv_test.c
==============================================================================
--- head/lib/libc/tests/resolv/resolv_test.c	Thu Sep  3 13:57:20 2020	(r365301)
+++ head/lib/libc/tests/resolv/resolv_test.c	Thu Sep  3 14:50:15 2020	(r365302)
@@ -70,22 +70,23 @@ static void
 load(const char *fname)
 {
 	FILE *fp;
-	size_t len;
+	size_t linecap;
 	char *line;
 
-	if ((fp = fopen(fname, "r")) == NULL)
+	fp = fopen(fname, "r");
 	ATF_REQUIRE(fp != NULL);
-	while ((line = fgetln(fp, &len)) != NULL) {
-		char c = line[len - 1];
+	line = NULL;
+	linecap = 0;
+	while (getline(&line, &linecap, fp) >= 0) {
 		char *ptr;
-		line[len - 1] = '\0';
+
 		for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) {
-			if (ptr == '\0' || ptr[0] == '#')
-				continue;
+			if (ptr[0] == '#')
+				break;
 			sl_add(hosts, strdup(ptr));
 		}
-		line[len - 1] = c;
 	}
+	free(line);
 
 	(void)fclose(fp);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202009031450.083EoFFV071556>