mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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);
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user