Project

General

Profile

Actions

Bug #20438

closed

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

Added by tompng (tomoya ishida) 7 months ago. Updated 6 months ago.

Status:
Closed
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 (0 open2 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 caseClosedActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) 7 months ago

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

Updated by nobu (Nobuyoshi Nakada) 7 months 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) 7 months ago

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

Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Status changed from Open to Closed

Applied in changeset git|31c9a3a1d330606493e5e70aec3cd1a36d8c61a0.


[Bug #20438] Disallow "%\n" and "%\0"

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0