From 3c082012b7a513bda260dd4bd6be597fa5b1c69a Mon Sep 17 00:00:00 2001 From: dave caruso Date: Tue, 7 May 2024 23:51:10 -0700 Subject: [PATCH] fix %i formatter with missing argument (#10910) --- src/bun.js/ConsoleObject.zig | 28 +++++++++----------- test/js/web/console/console-log.expected.txt | 5 ++++ test/js/web/console/console-log.js | 6 +++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/bun.js/ConsoleObject.zig b/src/bun.js/ConsoleObject.zig index 92d4f345c9..2a702d5c73 100644 --- a/src/bun.js/ConsoleObject.zig +++ b/src/bun.js/ConsoleObject.zig @@ -1245,7 +1245,6 @@ pub const Formatter = struct { var slice = slice_; var i: u32 = 0; var len: u32 = @as(u32, @truncate(slice.len)); - var any_non_ascii = false; var hit_percent = false; while (i < len) : (i += 1) { if (hit_percent) { @@ -1259,6 +1258,9 @@ pub const Formatter = struct { if (i >= len) break; + if (this.remaining_values.len == 0) + break; + const token: PercentTag = switch (slice[i]) { 's' => .s, 'f' => .f, @@ -1266,16 +1268,21 @@ pub const Formatter = struct { 'O' => .O, 'd', 'i' => .i, 'c' => .c, + '%' => { + // print up to and including the first % + const end = slice[0..i]; + writer.writeAll(end); + // then skip the second % so we dont hit it again + slice = slice[@min(slice.len, i + 1)..]; + i = 0; + continue; + }, else => continue, }; // Flush everything up to the % const end = slice[0 .. i - 1]; - if (!any_non_ascii) - writer.writeAll(end) - else - writer.writeAll(end); - any_non_ascii = false; + writer.writeAll(end); slice = slice[@min(slice.len, i + 1)..]; i = 0; hit_percent = true; @@ -1422,15 +1429,6 @@ pub const Formatter = struct { } if (this.remaining_values.len == 0) break; }, - '\\' => { - i += 1; - if (i >= len) - break; - if (slice[i] == '%') i += 2; - }, - 128...255 => { - any_non_ascii = true; - }, else => {}, } } diff --git a/test/js/web/console/console-log.expected.txt b/test/js/web/console/console-log.expected.txt index 32db26e8ce..167512e7bd 100644 --- a/test/js/web/console/console-log.expected.txt +++ b/test/js/web/console/console-log.expected.txt @@ -279,3 +279,8 @@ custom inspect 0.30000000000000004 Hello World 123 Hello %vWorld 123 +Hello NaN %i +Hello NaN % 1 +Hello NaN %j 1 +Hello \5 6, +Hello %i 5 6 diff --git a/test/js/web/console/console-log.js b/test/js/web/console/console-log.js index b2128c29ee..46357f219d 100644 --- a/test/js/web/console/console-log.js +++ b/test/js/web/console/console-log.js @@ -255,3 +255,9 @@ console.log("%f", 0.30000000000000004); console.log("Hello %cWorld", "color: red", 123); console.log("Hello %vWorld", 123); + +console.log("Hello %i %i", [1, 2, 3, 4]); +console.log("Hello %i %", [1, 2, 3, 4], 1); +console.log("Hello %i %j", [1, 2, 3, 4], 1); +console.log("Hello \\%i %i,", 5, 6); +console.log("Hello %%i %i", 5, 6);