On 24/08/2025 20:15, Collin Funk wrote:
Pádraig Brady <[email protected]> writes:
I don't think getline() is a generic enough interface,
especially for a utility used to introduce new lines.
Consider for example you want 80x10 lines of "y",
you could do this previously with:
$ yes | tr -d '\n' | fold -w 80 | head -n 10
While now fold will just consume all of memory.
Instead you might fread() to an IO_BUFSIZE buffer?
Good point.
Using fread is also faster since it doesn't check for the delimiter upon
reading a character, and doesn't need to lock the stream each call
(since we use fread_unlocked).
Will push the attached in a bit.
Cool, that was quick!
You might include something like the following test.
cheers!
Padraig
diff --git a/tests/fold/fold-characters.sh b/tests/fold/fold-characters.shindex
0b22aad6b..a6cc39b73 100755
--- a/tests/fold/fold-characters.sh
+++ b/tests/fold/fold-characters.sh
@@ -58,4 +58,10 @@ compare column-exp2 column-out2 || fail=1
fold --characters -w 10 input2 > character-out2 || fail=1
compare character-exp2 character-out2 || fail=1
+# Ensure bounded memory operation
+vm=$(get_min_ulimit_v_ fold /dev/null) && {
+ yes | tr -d '\n' | (ulimit -v $(($vm+8000)) && fold 2>err) | head -n10 ||
fail=1
+ compare /dev/null err || fail=1
+}
+
Exit $fail