Skip to content

Commit

Permalink
fold: infinite loop for -bs (#796)
Browse files Browse the repository at this point in the history
* fold: infinite loop for -bs

* For some input files, fold would get stuck in a loop printing "\n when run with -b and -s options
* In this case fold_file_byte() is called instead of fold_file()
* The regular expressions $soft and $hard are expected to modify the length of $_ if they match
* The while-loop controlled by $_ length never terminated if neither regex matched
* Adding a condition to break out of the loop for no-match prevents the infinite loop

* remove temporary variable
  • Loading branch information
mknos authored Nov 11, 2024
1 parent 892f6bc commit 707ad88
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions bin/fold
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,20 @@ sub fold_file_byte {
my $hard = "(.{$Width})(?=.)";
if ($Space_Break) {
while (<$input>) {
(s/$soft//o || s/$hard//o), print "$1\n" while length > $Width;
print;
APPLYRE: while (length > $Width) {
if (s/$soft//o || s/$hard//o) {
print "$1\n";
} else {
last APPLYRE;
}
}
print;
}
} else {
s/$hard/$1\n/go, print while <$input>; # SCREAM
while (<$input>) {
s/$hard/$1\n/go;
print;
}
}
return 0;
}
Expand Down

0 comments on commit 707ad88

Please sign in to comment.