Skip to content

std.format with position with omitted second number does not work #10699

Closed

Description

@bhaible

It's specified in https://dlang.org/library/std/format.html section "Position":

  • It is also possible to denote a series of arguments with two numbers separated by ':', that are all processed in the same way.
  • The second number can be omitted. In that case the series ends with the last argument.

The latter case, with an omitted second number, does not work: It throws an exception at runtime.

How to reproduce:

import std.stdio;
import std.format;

void main ()
{
  writeln (format ("%1$x %+2:9$d", 11, 22, 33, 44, 55, 66, 77, 88, 99));
  writeln (format ("%1$x %+2:$d", 11, 22, 33, 44, 55, 66, 77, 88, 99));
}

Run this program with dmd 2.110.

Expected output:

b +22+33+44+55+66+77+88+99
b +22+33+44+55+66+77+88+99

Actual output:

b +22+33+44+55+66+77+88+99
std.format.FormatException@/usr/include/dmd/phobos/std/format/write.d(651): Positional specifier %2$d index exceeds 9
----------------
??:? pure @safe uint std.format.write.formattedWrite!(std.array.Appender!(immutable(char)[]).Appender, char, int, int, int, int, int, int, int, int, int).formattedWrite(ref std.array.Appender!(immutable(char)[]).Appender, scope const(char[]), int, int, int, int, int, int, int, int, int) [0x63a71565a545]
??:? pure @safe immutable(char)[] std.format.format!(char, int, int, int, int, int, int, int, int, int).format(in char[], int, int, int, int, int, int, int, int, int) [0x63a7156599ad]
??:? _Dmain [0x63a715659930]

Activity

  1. rikkimax commented on Mar 25, 2025

    @rikkimax
    Contributor

    Hmm indexEnd is typed as ubyte, when you do this it sets the length to 255, and then write sees that.

    The format spec module does not know what the count of arguments, do so it has to use a sentinel value for this. It also won't work with arguments > 255, this needs to be thought about.

  2. bhaible commented on Mar 26, 2025

    @bhaible
    Author

    It also won't work with arguments > 255

    This shouldn't be a problem in practice. All programming language environments have limits in some kind. For example, ISO C 23 § 5.2.5.2 specifies a maximum of "127 arguments in one function call" for valid programs. Humans wouldn't write code with a function call with that many arguments, and it's hard to think of generated code to cross this limit.

  3. rikkimax commented on Mar 26, 2025

    @rikkimax
    Contributor

    It also won't work with arguments > 255

    This shouldn't be a problem in practice. All programming language environments have limits in some kind. For example, ISO C 23 § 5.2.5.2 specifies a maximum of "127 arguments in one function call" for valid programs. Humans wouldn't write code with a function call with that many arguments, and it's hard to think of generated code to cross this limit.

    You didn't read the footnote:

    Implementations are encouraged to avoid imposing fixed translation limits whenever possible

    Those were minimums specified, not maximums.

    I have verified that we support 512 arguments.

  4. added 2 commits that reference this issue on Mar 26, 2025
    c909b0a
    2c365af
  5. added a commit that references this issue on Mar 28, 2025
    205256a
  6. 0xEAB commented on Mar 28, 2025

    @0xEAB
    Member

    @rikkimax This can be closed now, can’t it?

  7. bhaible commented on Mar 28, 2025

    @bhaible
    Author

    This can be closed now, can’t it?

    Please give me 1 day of time, to verify that the fix works on my side. Thanks.

  8. rikkimax commented on Mar 28, 2025

    @rikkimax
    Contributor

    If it doesn't, reopen.

  9. bhaible commented on Mar 29, 2025

    @bhaible
    Author

    The fix does not work for me. I applied the patch to the GCC sources (on top of the fix of #10713), rebuilt GCC, and now the reproducer produces another exception:

    $ gdc foo.d
    $ ./a.out
    

    Actual output now:

    b +22+33+44+55+66+77+88+99
    std.format.FormatException@/usr/lib/gcc/x86_64-pc-linux-gnu/15.0.1/include/d/std/format/package.d(785): Orphan format arguments: args[65535..9]
    ----------------
    ??:? pure @safe noreturn std.exception.bailOut!(std.format.FormatException).bailOut(immutable(char)[], ulong, scope const(char)[]) [0x40f0bd]
    ??:? pure @safe bool std.exception.enforce!(std.format.FormatException).enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x408df0]
    ??:? pure @safe immutable(char)[] std.format.format!(char, int, int, int, int, int, int, int, int, int).format(in char[], int, int, int, int, int, int, int, int, int) [0x407caa]
    ??:? _Dmain [0x407a42]
    

    Can you please reopen the issue? (I don't have permissions to do that.)

  10. rikkimax commented on Mar 29, 2025

    @rikkimax
    Contributor

    Looks like it is coming afoul of: https://github.com/dlang/phobos/blob/stable/std/format/write.d#L609

    I was kinda expecting something like this to crop up.

    After the range specifier, did you add another?
    I.e. %1:$d %d

    The above isn't a bug.

  11. bhaible commented on Mar 29, 2025

    @bhaible
    Author

    After the range specifier, did you add another?
    I.e. %1:$d %d

    No, I'm running the exact same test program as in the description of this issue.

    And yes, I'm aware of this case: For a format string like "%1:$d %d", GNU gettext's format string checker will produce the error message "The directive number 2 references an argument after the last argument."

  12. rikkimax commented on Mar 29, 2025

    @rikkimax
    Contributor

    I copied your two cases from the original message and it passed on my end.

  13. bhaible commented on Mar 29, 2025

    @bhaible
    Author

    I copied your two cases from the original message and it passed on my end.

    I'll try to debug it on my side, then, in the next 2 days.

  14. added a commit that references this issue on Mar 30, 2025
    ba832a9
  15. bhaible commented on Mar 30, 2025

    @bhaible
    Author

    @rikkimax , @thewilsonator : Please, can you review my pull request #10723 ?

  16. added a commit that references this issue on Apr 1, 2025
  17. added a commit that references this issue on Apr 9, 2025
  18. added a commit that references this issue on Apr 9, 2025
  19. added a commit that references this issue on Jun 5, 2025
    a6eed54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions