Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File.readln does not properly handle case where last character in terminator is repeated #10551

Open
dlangBugzillaToGithub opened this issue May 20, 2024 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

zopsicle reported this on 2024-05-20T14:01:52Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=24557

CC List

Description

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).
@dlangBugzillaToGithub
Copy link
Author

schveiguy (@schveiguy) commented on 2024-05-20T17:38:51Z

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.

@LightBender LightBender removed the P1 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants