21 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 478ada3bf4 fp_uscale.c: clamp precision in %g to avoid OOB in fixed_width
When sprintf is called with a precision larger than the double's
significand width (e.g. "%.51g"), fixed_width() indexed pow10 tables
out of bounds and produced a negative shift exponent. Cap the
internal digit count to 18 in the %g branch, matching the existing
%e and %f branches; downstream loops already zero-pad to the
caller's precision so visible output is unchanged.

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-12 11:19:20 +09:00
Yukihiro "Matz" Matsumoto 59552ecb8e mruby-sprintf: protect format string from mutation during callbacks
mrb_str_format captured raw C pointers (p, end) into the format
string's buffer before the main loop. The %s and %p specifiers call
to_s and inspect, which can invoke Ruby code that mutates the format
string via String#replace, freeing or reallocating its buffer. The
loop then continued iterating with dangling pointers, reading freed
memory and potentially leaking adjacent heap contents into the result.

Duplicate the format string with mrb_str_dup() before the loop. This
is O(1) because mrb_str_dup shares the underlying buffer; if the
original is later mutated via String#replace, str_replace decrements
the shared refcount, leaving our duplicate's buffer intact.

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-10 14:51:59 +09:00
Yukihiro "Matz" Matsumoto aea3e7aedd mruby-sprintf: raise ArgumentError for % at the bottom 2024-05-03 07:46:45 +09:00
Yukihiro "Matz" Matsumoto b9bde0c918 mruby-sprintf: reduce float digits to avoid test failure.
Too many digits to fit in single precision float numbers, so that tests
fail when `MRB_USE_FLOAT32` defined.
2021-05-22 14:46:37 +09:00
Yukihiro "Matz" Matsumoto 3252518a3c Fix tests to work with MRB_USE_FLOAT32; fix #5329 2021-02-07 14:55:45 +09:00
cremno b44326b561 Add tests for CRuby bug #9982
https://bugs.ruby-lang.org/issues/9982
2020-10-12 16:21:45 +09:00
Yukihiro "Matz" Matsumoto 03b0741994 Remove unnecessary sprintf test that fails since 91368c1. 2020-05-07 10:31:49 +09:00
dearblue 2ceab2d270 Supports some specifier flags with mrb_float_to_str()
Changed to understand `#`, `0`, `-`, ` ` and `+`.

Based on src/stdio/vfprintf.c in git://git.musl-libc.org/musl
2020-04-11 22:11:10 +09:00
Yukihiro "Matz" Matsumoto cca19532c5 Remove Kernel#class_defined? which is not available in CRuby; #3829 2019-01-03 11:34:35 +09:00
YAMAMOTO Masaya 5d781312ca Support MRB_WIHTOUT_FLOAT to mruby-sprintf 2017-10-13 16:42:35 +09:00
Tomasz Dąbrowski 17c249780f fix String#% %d test with MRB_INT16 set 2017-08-25 14:36:05 +02:00
Christopher Aue 2730231e50 added tests for #3736 2017-07-16 22:34:23 +02:00
Yukihiro "Matz" Matsumoto f0abd4241f Avoid mrb_funcall() unless absolutely necessary; ref #3722
As a result, `#chr` is not called for ch < 0x80, so we need to
update the "invalid chr" test.
2017-06-28 12:03:53 +09:00
ksss 30df7c8762 Fix segmentation fault ref: #3648 2017-05-08 23:18:14 +09:00
Nobuyoshi Nakada 7314423bd1 Fix space flag when Inf/NaN and width==3
While `"% 2f"` and `"% 4f"` result in `" Inf"` and `" Inf"`
respectively, `"% 3f"` results in `"Inf"` (no space).
2017-04-23 09:54:20 +09:00
Nobuyoshi Nakada d8c4fe7bcb Fix out-of-bound access
Get rid of out-of-bound access when single % at the end.
2017-03-13 23:56:33 +09:00
Bouke van der Bijl 9bf1c0e1dc Fix segfault when Fixnum#chr doesn't return a string 2016-11-23 15:18:52 -05:00
Tomasz Dąbrowski 4289e0cd81 Fix for sprintf test exceeding bounds with MRB_INT16 2016-11-21 00:37:03 +01:00
Yukihiro "Matz" Matsumoto 1a9b607a4d binary sprintf should not be restricted by mrb_int size; fix #3025 2015-11-19 22:49:50 +09:00
Tomoyuki Sahara fe9f3fdd20 add 'String#%'. 2014-06-03 16:31:20 +09:00
Masaki Muranaka 97136825e2 Separate Kernel#sprintf support from mruby core. It's moved to mrbgems. 2013-03-04 23:04:39 +09:00