From owner-svn-src-all@freebsd.org Thu Sep 3 14:50:16 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 393F43C0824; Thu, 3 Sep 2020 14:50:16 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bj3d40prkz4Xc9; Thu, 3 Sep 2020 14:50:16 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id F35AD1D510; Thu, 3 Sep 2020 14:50:15 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 083EoFFY071557; Thu, 3 Sep 2020 14:50:15 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 083EoFFV071556; Thu, 3 Sep 2020 14:50:15 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202009031450.083EoFFV071556@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Thu, 3 Sep 2020 14:50:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365302 - head/lib/libc/tests/resolv X-SVN-Group: head X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: head/lib/libc/tests/resolv X-SVN-Commit-Revision: 365302 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Sep 2020 14:50:16 -0000 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); }