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

Incorrect %*s handling by formattedRead() #10598

Open
mrKirushko opened this issue Dec 28, 2024 · 0 comments
Open

Incorrect %*s handling by formattedRead() #10598

mrKirushko opened this issue Dec 28, 2024 · 0 comments
Labels
Good First Issue Issue is easy to fix, good for new contributors

Comments

@mrKirushko
Copy link

mrKirushko commented Dec 28, 2024

Recently found a strange problem with formatted string parsing in Phobos.
dmd -run ... for the program:

import std.format;
import std.stdio;
void main()
{
    string str = "you freaking monkey";
    string you, monkey;
    str.formattedRead("%s %*s %s", you, monkey);
    writeln("Success, ", you , " ", monkey, "!");
}

outputs this:

core.exception.AssertError@/home/mrk/dlang/dmd-2.109.1/linux/bin32/../../src/phobos/std/format/internal/read.d(41): Format specifier not understood: %s":
----------------
??:? _d_assert_msg [0x506469]
??:? pure @safe void std.format.internal.read.skipData!(immutable(char)[], char).skipData(ref immutable(char)[], scope ref const(std.format.spec.FormatSpec!(char).FormatSpec)) [0x4eb926]
??:? pure @safe void std.format.read.formattedRead!(immutable(char)[], char, immutable(char)[]).formattedRead(ref immutable(char)[], const(char)[], ref immutable(char)[]).skipUnstoredFields() [0x4f43e0]
??:? pure @safe uint std.format.read.formattedRead!(immutable(char)[], char, immutable(char)[]).formattedRead(ref immutable(char)[], const(char)[], ref immutable(char)[]) [0x4f4320]
??:? pure @safe uint std.format.read.formattedRead!(immutable(char)[], char, immutable(char)[], immutable(char)[]).formattedRead(ref immutable(char)[], const(char)[], ref immutable(char)[], ref immutable(char)[]) [0x4e4ae9]
??:? _Dmain [0x4e49af]

I have checked that if there are no ignored parameters in the format string ("%s %s %s") or if the ignored parameter is the last one in the sequence ("%s %s %*s") then formattedRead() works correctly and does not throw any exceptions. The problem can be reproduced at least on DMD32 v2.109.1 for Linux, DMD32 v2.099.1 for Linux and DMD64 v2.102.2 for Windows.

The equivalent C code compiled by GCC 8.3.0 of course works as expected:

#include <stdio.h>
int main(void)
{
    char* str = "you freaking monkey";
    char you[32] = {0}, monkey[32] = {0};
    if (2 != sscanf(str, "%s %*s %s", you, monkey)) {
        printf("Error???\n"); return 1;
    }
    printf("Success, %s %s!\n", you, monkey);
    return 0;
}
$ gcc test.c -o test; ./test
Success, you monkey!

It is not a huge problem since there is an obvious workaround of providing a dummy string but I still hope it will get fixed someday.

@0xEAB 0xEAB added the Good First Issue Issue is easy to fix, good for new contributors label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Issue is easy to fix, good for new contributors
Projects
None yet
Development

No branches or pull requests

2 participants