From unknown Fri Sep 05 08:41:14 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#78932 <78932@debbugs.gnu.org> To: bug#78932 <78932@debbugs.gnu.org> Subject: Status: Bug report: fold does not correctly handle last word in line with -s option Reply-To: bug#78932 <78932@debbugs.gnu.org> Date: Fri, 05 Sep 2025 15:41:14 +0000 retitle 78932 Bug report: fold does not correctly handle last word in line = with -s option reassign 78932 coreutils submitter 78932 Edoardo La Greca severity 78932 normal thanks From debbugs-submit-bounces@debbugs.gnu.org Mon Jun 30 15:02:20 2025 Received: (at submit) by debbugs.gnu.org; 30 Jun 2025 19:02:20 +0000 Received: from localhost ([127.0.0.1]:48471 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uWJlj-0007YV-O5 for submit@debbugs.gnu.org; Mon, 30 Jun 2025 15:02:20 -0400 Received: from lists.gnu.org ([2001:470:142::17]:58720) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uWElT-0008H0-BT for submit@debbugs.gnu.org; Mon, 30 Jun 2025 09:41:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uWElN-0007Cy-Ll for bug-coreutils@gnu.org; Mon, 30 Jun 2025 09:41:37 -0400 Received: from [95.179.251.129] (helo=edolg.net) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uWElL-0001l6-HL for bug-coreutils@gnu.org; Mon, 30 Jun 2025 09:41:37 -0400 Received: from [172.16.100.19] (93-37-243-225.ip67.fastwebnet.it [93.37.243.225]) by edolg.net (OpenSMTPD) with ESMTPSA id 57e07f55 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Mon, 30 Jun 2025 15:14:49 +0200 (CEST) Message-ID: Date: Mon, 30 Jun 2025 15:14:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Edoardo La Greca Subject: Bug report: fold does not correctly handle last word in line with -s option To: bug-coreutils@gnu.org Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 95.179.251.129 (failed) Received-SPF: pass client-ip=95.179.251.129; envelope-from=edo@edolg.net; helo=edolg.net X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.237, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.9 (/) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Mon, 30 Jun 2025 15:02:14 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.1 (/) Hi everyone! When the -s option is enabled (break at spaces), fold does not correctly handle the case in which the last character that should be on the folded line is a word's last character. In other words, the issue happens when the line's last character is a non-blank character, followed by a blank character. In that case, the whole last word, which would fit within the limit, is put on the next line. To clarify, I'll make an example. Let's suppose that: - the line width limit is set to 80 (default) - the -s option is enabled Let's take the following string, where the double quote characters simply mark the beginning and the end of the string and are not included in it. "This line is exactly exactly exactly exactly exactly exactly exactly 80 chars!!!" If you count the characters they are exactly 80, so it takes exactly a whole line. If you create an empty file and write only that line in it, fold correctly keeps the whole string in its line. However, if you add a space character and some other words, the word "chars!!!" gets folded to a new line. I had a quick look at the source code on GitHub (master branch) and the error seems to lie in line 174 (`--logical_end`). I have not tested the program but if there are no "special characters" in the line (e.g. control characters like '\r' or '\b'), then the fold condition (`column > width`) is triggered when `column = width + 1` and `offset_out = column + 1`. Once `logical_end` is declared and initialized to `offset_out`, the following while loop decrements it as first thing. This means that the space character can never be in the current character (with index `offset_out`), only from the previous character to the beginning of the line, which folds the last word to the new line. I also noticed that fold keeps the last space character at the end of the line when -s is enabled, instead of removing it. I don't know if this is the expected behavior, if it's not it might be worth to also fix this one.