From nobody Fri Apr 19 21:52:44 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VLpKT0g2zz5HqBM; Fri, 19 Apr 2024 21:52:45 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VLpKS5X87z493S; Fri, 19 Apr 2024 21:52:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4vWXn2wMSdhPiL/CydSeFN80pa7we3GS9op3w3Ioy4c=; b=bRSkeDh/Vk3nrE1x+JSY/PrFvoyrOptXIshtBAFODqd2j0G8dHF0HH79Mks3HyaKlBBtP8 0lMxGaA2O4cQq1uF5xvbbkoG2PDwmkQ88LQIxFxqOtTUEtM2P8/dLZallvP3BzwFuBGx18 DwA6tuk4/w9ze4sJrE5JiZvmCp5JVe/ttQILHl4h+uUsjCdDD0cXQ88WlX0GTlUmWrza7S 4ABJ36b88NbkNQ4MuQEyV58+V05gLli7FE1YsQzxw9MNSsKgY8JsDcI6s7t5X0Njkqe7/a T3C9OvvF13LPIeKjIBu3BuGY31YOBgAsRM7940C+yItaHC+3iud4Fk489z/M1g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713563564; a=rsa-sha256; cv=none; b=TNt36xX0+CG6xBbn2fGSsEalbRhK3ruKqdZxBc9c8IF3sema5yaXRun1pn3fPErr/hsZ6b GZXw/hMrBuWntDP56BiImBQe7RSsZXiYswaedoA/2Br87Ja9PeTU9wIFjI+eVOqt8Xrf25 CVkcS8IKrUYNv6plyuWdisi3eckiT50uyHqYM2TVFoiTLYCFv0psnm6Ca8lrkwLNTtr+gv YZ57V0adaPqms1LPfZpBNeA66LMyAQDyZlodnssAQs/IQVc+emalHrfo9YJg1oB7KRR52+ +QMx0GMsgFkWrO5vR9BvxzjNpYRuWCKV+w8+ANNrUg1PLndOOnJr8L8QOXyjYw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4vWXn2wMSdhPiL/CydSeFN80pa7we3GS9op3w3Ioy4c=; b=eiIXjtx5cOmBsIGGkw3uSDzlOFLlsmJEZe3yldRnl4zOzwlnbWCd/T22B6x+2I6YdZVsH/ tmbge8ycO+7krtvjpsebRe308ko5+SPBfgi59zSLl84KVyohJ2SiPul6/jI9XVo6oHoLTZ l885pb7vVY749BP8j8mpEj/3n2jLn7Le89WfQ4cNylFg7xNxN0oh+3N0vSNz1s+ziC0Squ uyrWUuSzXHsSHsfGyCck4fwcXWZ0DPopEOWh8rI3dGXI0v6DvXTwdEm6PGom9h+bYRxh4d jXWj+2tPs7jTWqqETR1OBfowp4smAAKWqYq9AhqAoi7wBk0EDOqdOpw3bp3I+Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VLpKS57tzz18tq; Fri, 19 Apr 2024 21:52:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43JLqiYI023931; Fri, 19 Apr 2024 21:52:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43JLqiZ9023928; Fri, 19 Apr 2024 21:52:44 GMT (envelope-from git) Date: Fri, 19 Apr 2024 21:52:44 GMT Message-Id: <202404192152.43JLqiZ9023928@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 25696725b65b - main - patch: use getline() instead of fgetln() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 25696725b65b651814799574fdfadb1b1a5e0b02 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=25696725b65b651814799574fdfadb1b1a5e0b02 commit 25696725b65b651814799574fdfadb1b1a5e0b02 Author: Martin Tournoij AuthorDate: 2024-04-19 21:11:30 +0000 Commit: Warner Losh CommitDate: 2024-04-19 21:52:22 +0000 patch: use getline() instead of fgetln() This replaces fgetln() with getline(). The main reason for this is portability, making things easier for people who want to compile these tools on non-FreeBSD systems. I appreciate that's probably not the top concern for FreeBSD base tools, but fgetln() is impossible to port to most platforms, as concurrent access is essentially impossible to implement fully correct without the line buffer on the FILE struct. Other than this, many generic FreeBSD tools compile fairly cleanly on Linux with a few small changes. Most uses of fgetln() pre-date getline() support (added in 2009 with 69099ba2ec8b), and there's been some previous patches (ee3ca711a898 8c98e6b1a7f3 1a2a4fc8ce1b) for other tools. Obtained from: https://github.com/dcantrell/bsdutils and https://github.com/chimera-linux/chimerautils Signed-off-by: Martin Tournoij Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/893 --- usr.bin/patch/inp.c | 19 +++++++------------ usr.bin/patch/pch.c | 17 ++++++++++------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index 24fdcac1b7c6..ccc9a39b117b 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -283,8 +283,9 @@ static void plan_b(const char *filename) { FILE *ifp; - size_t i, j, len, maxlen; - char *lbuf = NULL, *p; + size_t i = 0, j, blen = 0, maxlen = 1; + ssize_t len; + char *p = NULL; bool found_revision = (revision == NULL); using_plan_a = false; @@ -295,26 +296,20 @@ plan_b(const char *filename) pfatal("can't open file %s", TMPINNAME); len = 0; maxlen = 1; - while ((p = fgetln(ifp, &len)) != NULL) { + while ((len = getline(&p, &blen, ifp)) >= 0) { if (p[len - 1] == '\n') p[len - 1] = '\0'; else { - /* EOF without EOL, copy and add the NUL */ - if ((lbuf = malloc(len + 1)) == NULL) - fatal("out of memory\n"); - memcpy(lbuf, p, len); - lbuf[len] = '\0'; - p = lbuf; - + /* EOF without EOL */ last_line_missing_eol = true; len++; } if (revision != NULL && !found_revision && rev_in_string(p)) found_revision = true; - if (len > maxlen) + if ((size_t)len > maxlen) maxlen = len; /* find longest line */ } - free(lbuf); + free(p); if (ferror(ifp)) pfatal("can't read file %s", filename); diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index fb53ff86f9ef..71f73125a8cb 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1213,14 +1213,15 @@ hunk_done: size_t pgets(bool do_indent) { - char *line; - size_t len = 0; + char *line = NULL; + ssize_t len = 0; + size_t buflen = 0; int indent = 0, skipped = 0; - line = fgetln(pfp, &len); - if (line != NULL) { - if (len + 1 > buf_size) { - while (len + 1 > buf_size) + if ((len = getline(&line, &buflen, pfp)) >= 0) { + char *linep = line; + if ((size_t)(len + 1) > buf_size) { + while ((size_t)(len + 1) > buf_size) buf_size *= 2; free(buf); buf = malloc(buf_size); @@ -1239,8 +1240,10 @@ pgets(bool do_indent) } memcpy(buf, line, len - skipped); buf[len - skipped] = '\0'; + line = linep; } - return len; + free(line); + return (len > 0) ? len : 0; }