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 <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-10-19 07:33:45 +09:00
parent f5f0b9ac51
commit fec629309f
2 changed files with 14 additions and 1 deletions
+12
View File
@@ -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);
+2 -1
View File
@@ -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')