Skip to content

Incorrect %*s handling by formattedRead() #10598

Closed
@mrKirushko

Description

@mrKirushko

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueIssue is easy to fix, good for new contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions