From fec629309f7b7274d8654fcc5ebdc63727b51582 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 19 Oct 2025 07:33:45 +0900 Subject: [PATCH] mruby-strftime: prevent crash on msvc with unsupported %- flag add validation to detect gnu extension %- flag and raise argumenterror on msvc instead of crashing. update test to use portable %m format. Co-authored-by: Claude --- mrbgems/mruby-strftime/src/strftime.c | 12 ++++++++++++ mrbgems/mruby-strftime/test/strftime.rb | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mrbgems/mruby-strftime/src/strftime.c b/mrbgems/mruby-strftime/src/strftime.c index cef7ca9d7..1f4c37313 100644 --- a/mrbgems/mruby-strftime/src/strftime.c +++ b/mrbgems/mruby-strftime/src/strftime.c @@ -73,6 +73,18 @@ mrb_time_strftime(mrb_state *mrb, mrb_value self) memcpy(segment, fmt_ptr, (size_t)segment_len); segment[segment_len] = '\0'; +#ifdef _MSC_VER + /* Check for GNU extension %-flag which crashes on MSVC */ + /* Scan for %- patterns in the format string */ + for (const char *p = segment; *p != '\0'; p++) { + if (p[0] == '%' && p[1] == '-') { + mrb_free(mrb, segment); + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "strftime format flag '%-' not supported on this platform (use '%%#' on Windows)"); + } + } +#endif + /* Allocate buffer for formatted output */ buf_size = INITIAL_BUFFER_SIZE; buf = (char *)mrb_malloc(mrb, buf_size); diff --git a/mrbgems/mruby-strftime/test/strftime.rb b/mrbgems/mruby-strftime/test/strftime.rb index 601d528c9..6dade4ed9 100644 --- a/mrbgems/mruby-strftime/test/strftime.rb +++ b/mrbgems/mruby-strftime/test/strftime.rb @@ -129,7 +129,8 @@ end assert('Time#strftime with various time components') do t = Time.gm(2023, 6, 15, 14, 23, 7) - assert_equal '6', t.strftime('%-m') if t.strftime('%-m') != '' # Skip if platform doesn't support %- + # Use portable format specifiers that work on all platforms + assert_equal '06', t.strftime('%m') # Month with leading zero (portable) assert_equal '15', t.strftime('%d') assert_equal '14', t.strftime('%H') assert_equal '23', t.strftime('%M')