Issue 24557 - File.readln does not properly handle case where last character in terminator is repeated
Summary: File.readln does not properly handle case where last character in terminator ...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-05-20 14:01 UTC by zopsicle
Modified: 2024-12-01 16:42 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description zopsicle 2024-05-20 14:01:52 UTC
Consider the following program:

-----
import std.stdio : readln, writefln;

void main()
{
    char[] buf;
    readln(buf, "ABA");
    writefln!"%(%s%)"([buf]);
}
-----

Compiled and run as follows:

-----
$ dmd example.d
$ printf 'XABAY' | ./example
-----

Expected output: "XABA"
Actual output: "XABAY"

readln does not properly handle the case where the last character of the terminator (in this case A) appears multiple times in the terminator (in this case, A also appears at the front of the terminator).
Comment 1 Steven Schveighoffer 2024-05-20 17:38:51 UTC
readln does use the last character, but it is also validating the "line" ends in the full termination sequence.

However, the bug is that if the last character is repeated in the termination sequence, it checks just the new data that it has read for the termination sequence.

So for example, if you look for `ging` as the termination sequence, the following algorithm results (assume input is `bringing home the bacon`):

1. result = "", readln('g') => "bring". Does "bring" end in "ging"? no
2. result = "bring", readln('g') => "ing". Does "ing" end in "ging"? no
3. result = "bringing", readln('g') => " home the bacon". readln returned no delim found, so result is "bringing home the bacon"

Instead, readln should check the *concatenated* result (but obviously, only the result that has been appended since the readln call was started.
Comment 2 dlangBugzillaToGithub 2024-12-01 16:42:34 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10551

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB