Project

General

Profile

Actions

Bug #20438

open

String format "%\n" and "%\0" does not raise format error

Added by tompng (tomoya ishida) 13 days ago. Updated 10 days ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT +MN [arm64-darwin22]
[ruby-core:117619]

Description

"%" % 1 raises incomplete format specifier; use %% (double %) instead
"%=" % 1 raises malformed format string - %=.
But "%\n" % 1 "%\0" % 1 does not raise error.
In sprintf.c, \n and \0 are explicitly accepted. Is this expected?

Some other language are:
Perl: Warns Invalid conversion in printf. Just prints. "%d% " → "1% "
Python: Error ValueError: unsupported format character '?' (0xa) with print("%\n" % 123)
PHP: Error Unknown format specifier with sprintf("%\n", 3)
C, C++: Warns incomplete format specifier. "%\n" → "\n", "%" → "", "% " → ""

sprintf("%f%\n",x) and "%f%\n" % x is used in some codes https://github.com/search?q=language%3ARuby+%22f%25%5Cn%22&type=code


Related issues 2 (1 open1 closed)

Related to Ruby master - Bug #13315: Single "%" at the end of `printf` format string appears in the resultClosedEregon (Benoit Daloze)Actions
Has duplicate Ruby master - Bug #20439: Invalid string format with n$ flag is ignored in some caseOpenActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) 12 days ago

  • Has duplicate Bug #20439: Invalid string format with n$ flag is ignored in some case added

Updated by nobu (Nobuyoshi Nakada) 10 days ago · Edited

"%\n" has been treated as "%%" since 554b989ba162, probably Tue Aug 6 01:12:32 1996 according to the commit log.
I don't know the reason.

Actions #3

Updated by nobu (Nobuyoshi Nakada) 10 days ago

  • Related to Bug #13315: Single "%" at the end of `printf` format string appears in the result added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0