270 Commits
Author SHA1 Message Date
Nobuyoshi Nakada faefcb1105 [Bug #22461] Fix dangling pointers after associating encoding
`rb_enc_associate` may re-allocate the string buffer because of the
terminator length change.
2026-10-22 10:46:22 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 1c7e4ce28f Check for INT_MIN before negating sprintf width
Negating INT_MIN overflows, so the compiler may drop the `width < 0` check that follows it. clang-cl does so, and `sprintf("%*s", INT_MIN, "")` does not raise ArgumentError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 16:01:04 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 70e53da3e2 Reject a string too long for BSD_vfprintf in rb_sprintf
BSD_vfprintf takes the length of a PRIsVALUE field as a long, which is narrower than a String length on mswin once rb_len_t widens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Hiroshi SHIBATAandClaude Opus 5 67d69062e5 Use rb_len_t for lengths in sprintf.c
The %"PRIsVALUE" hook of BSD_vfprintf still returns the length as `long`, so it raises ArgumentError for a longer string once rb_len_t widens.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Peter Zhu edce07a8c9 Fix use-after-free in String#% when array modified
If the array object passed into String#% is modified during conversion
(such as via to_s, to_i, to_f, etc.) then it can be a use-after-free if
the buffer of the array is freed.

This commit changes it to detect if the buffer has been changed and raises
an error when that happens.

The following script reproduces the crash:

    count = 1_000
    obj = Object.new
    ary = [obj, *Array.new(count - 1) { "x" }]
    obj.define_singleton_method(:to_s) do
      ary.replace([])
      "X"
    end
    str = "%s" * count
    puts (str % ary).length
2026-09-20 21:06:47 +09:00
Jean Boussier 64450290f3 sprintf.c: don't raise "too many arguments" errors when $DEBUG is set
[Feature #22136]

This is as far as I can tell the only method that changes its behavior
in debug mode, and that's all but helpful because it might cause
the program to abort.

Usually when you run a program with $DEBUG set, it is to find some
error that may have been swallowed, so aside from the extra output
you would expect Ruby to behave the same than in normal mode.
2026-07-10 07:34:42 +02:00
Nobuyoshi Nakada df0ba42552 Keep C printf result encodings ASCII-compatible
Reject ASCII-incompatible result encodings, and avoid associating such
encodings from `PRIsVALUE` arguments; convert them to the existing
result encoding instead.
2026-06-11 16:17:18 +09:00
Nobuyoshi Nakada e39475a544 sprintf.c: Fix width overflow 2026-06-07 16:04:00 +09:00
Nobuyoshi Nakada 99d06012e4 sprintf.c: Refactor CHECK macro 2026-06-07 16:04:00 +09:00
Takashi Kokubun 1389a36b51 sprintf.c: Fix -Wmaybe-uninitialized warnings in rb_str_format
Initialize `c` and `encidx` to 0. They are assigned inside
`if (n >= 0)` and the following `if (n <= 0)` calls rb_raise, but the
compiler cannot see through the noreturn guarantee.
2026-03-31 17:24:01 -07:00
Nobuyoshi Nakada aaf47cca03 Now onigenc_single_byte_code_to_mbclen checks out-of-bound 2026-01-12 20:24:55 +09:00
John Hawthorn c8c94bfb1e Fix benign off-by-one
Previously we always reserved one more byte than necessary in the
sprintf output string.
2024-11-08 17:39:19 -08:00
1f6dd9071c Fix update_coderange for binary strings
Although a binary (aka ASCII-8BIT) string will never have a broken
coderange, it still has to differentiate between "valid" and "7bit".

On Ruby 3.4/trunk this problem is masked because we now clear the
coderange more agressively in rb_str_resize, and we happened to always
be strinking this string, but we should not assume that.

On Ruby 3.3 this created strings where `ascii_only?` was true in cases
it shouldn't be as well as other problems.

Fixes [Bug #20883]

Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
Co-authored-by: Matthew Draper <matthew@trebex.net>
2024-11-08 17:39:19 -08:00
Nobuyoshi Nakada d71be7274b [Bug #20873] Consider -FIXNUM_MIN overflow
`-FIXNUM_MIN` is usually greater than `FIXNUM_MAX` on platforms using
two's complement representation.
2024-11-06 13:31:38 +09:00
Jean Boussier 294dad22d7 Time#strftime: avoid repeated coderange scanning
Followup: a3f589640f

Restarting scanning has a fixed cost. Since we build a string
from many small parts, that causes the scan to restart many times.

It's much faster to ignore the coderange, if it's needed later it
will be faster to scan it in one go.

```
compare-ruby: ruby 3.4.0dev (2024-09-04T11:54:06Z opt-strftime-growth 149480f0ff) +YJIT [arm64-darwin23]
built-ruby: ruby 3.4.0dev (2024-09-04T11:55:26Z opt-strftime-coder.. 29a5153cba) +YJIT [arm64-darwin23]
warming up...

|                            |compare-ruby|built-ruby|
|:---------------------------|-----------:|---------:|
|time.strftime("%FT%T")      |      1.870M|    2.225M|
|                            |           -|     1.19x|
|time.strftime("%FT%T.%3N")  |      1.731M|    2.068M|
|                            |           -|     1.19x|
|time.strftime("%FT%T.%6N")  |      1.744M|    2.073M|
|                            |           -|     1.19x|
```

Cumulative with a3f589640f:

```
compare-ruby: ruby 3.4.0dev (2024-09-04T11:55:26Z opt-strftime-coder.. 29a5153cba) +YJIT [arm64-darwin23]
built-ruby: ruby 3.4.0dev (2024-09-04T12:57:30Z opt-strftime-coder.. 2b938d667a) +YJIT [arm64-darwin23]
warming up...

|                            |compare-ruby|built-ruby|
|:---------------------------|-----------:|---------:|
|time.strftime("%FT%T")      |      1.784M|    2.277M|
|                            |           -|     1.28x|
|time.strftime("%FT%T.%3N")  |      1.504M|    2.056M|
|                            |           -|     1.37x|
|time.strftime("%FT%T.%6N")  |      1.489M|    2.094M|
|                            |           -|     1.41x|
```
2024-09-04 16:12:50 +02:00
Jean Boussier 3de0e343e2 Do not warn about extra format argument when passed as keywords
[Bug #20593]

It's fairly common to use `format` to interpolate a number of values
into a user provided strings.

The arguments not matching are a problem when they are positional,
but when they are named, it's absolutely fine and we shouldn't
emit a warning.
2024-07-11 07:39:50 +02:00
Nobuyoshi Nakada 31c9a3a1d3 [Bug #20438] Disallow "%\n" and "%\0" 2024-05-29 01:02:32 +09:00
Nobuyoshi Nakada 11acb7f7bc [Bug #19167] Remove useless conversion of classes for special const 2022-12-09 22:11:44 +09:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Nobuyoshi Nakada 576bdec03f [Bug #18973] Promote US-ASCII to ASCII-8BIT when adding 8-bit char 2022-08-31 17:27:59 +09:00
Nobuyoshi Nakada 9dc60653db Extract update_coderange macro
Which restarts scanning the code range in unscanned part.
2022-08-31 17:27:59 +09:00
Nobuyoshi Nakada b043dd9c5d Check the room including the space to fill 2022-08-21 20:06:39 +09:00
Nobuyoshi Nakada 1a2f99275b [Bug #18958] format string must be ASCII compatible 2022-08-20 10:06:40 +09:00
Nobuyoshi Nakada d2483393cb [Bug #18956] Negative codepoints are invalid characters 2022-08-20 10:06:16 +09:00
Nobuyoshi Nakada 1ef49de834 [Bug #18955] format single character for %c 2022-08-20 03:57:13 +09:00
Nobuyoshi Nakada ce384ef5a9 [Bug #18955] Check length of argument for %c in proper encoding 2022-08-20 03:57:13 +09:00
Nobuyoshi Nakada 99116da7f0 Scan the code range of the last added portion 2022-08-19 03:05:53 +09:00
Nobuyoshi Nakada f34280ec6b Scan the code range of the formatted portion 2022-08-19 03:05:53 +09:00
Jean Boussier b0b9f7201a rb_str_resize: Only clear coderange on truncation
If we are expanding the string or only stripping extra capacity
then coderange won't change, so clearing it is wasteful.
2022-08-18 10:09:08 +02:00
Nobuyoshi Nakada ddb81b9307 Extract common code from rb_enc_vsprintf and rb_str_vcatf 2022-08-17 17:40:59 +09:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Nobuyoshi Nakada 04be8e84db Use C99-defined macros to classify a floating-point number 2021-08-27 12:41:30 +09:00
Nobuyoshi Nakada c38c2d8ee2 Moved exported symbols in internal/util.h to ruby/util.h
[Feature #18051]
2021-08-24 10:37:41 +09:00
Nobuyoshi Nakada eec45a93ef Escape unprintable chars only, without surrounding quotes 2021-07-24 14:31:41 +09:00
Nobuyoshi Nakada 75f60e5a46 Sort out quad_t related macros 2021-07-22 15:41:42 +09:00
Nobuyoshi Nakada 36d1bb720c sprintf.c: Removed conflicting definition 2020-09-28 12:51:53 +09:00
卜部昌平 9e41a75255 sed -i 's|ruby/impl|ruby/internal|'
To fix build failures.
2020-05-11 09:24:08 +09:00
卜部昌平 d7f4d732c1 sed -i s|ruby/3|ruby/impl|g
This shall fix compile errors.
2020-05-11 09:24:08 +09:00
卜部昌平 9e6e39c351 Merge pull request #2991 from shyouhei/ruby.h
Split ruby.h
2020-04-08 13:28:13 +09:00
Nobuyoshi Nakada bdf3032e35 Make temporary lock string encoding free
As a temporary lock string is hidden, it can not have instance
variables, including non-inlined encoding index.
2020-02-12 19:58:22 +09:00
卜部昌平 5e22f873ed decouple internal.h headers
Saves comitters' daily life by avoid #include-ing everything from
internal.h to make each file do so instead.  This would significantly
speed up incremental builds.

We take the following inclusion order in this changeset:

1.  "ruby/config.h", where _GNU_SOURCE is defined (must be the very
    first thing among everything).
2.  RUBY_EXTCONF_H if any.
3.  Standard C headers, sorted alphabetically.
4.  Other system headers, maybe guarded by #ifdef
5.  Everything else, sorted alphabetically.

Exceptions are those win32-related headers, which tend not be self-
containing (headers have inclusion order dependencies).
2019-12-26 20:45:12 +09:00
Jeremy Evans ffd0820ab3 Deprecate taint/trust and related methods, and make the methods no-ops
This removes the related tests, and puts the related specs behind
version guards.  This affects all code in lib, including some
libraries that may want to support older versions of Ruby.
2019-11-18 01:00:25 +02:00
卜部昌平 0766f67168 move docs around [ci skip]
To properly generate documents.
2019-08-29 18:34:09 +09:00
Yusuke Endoh ba94404f70 Add /* fall through */ comments
to suppress some Coverity Scan warnings
2019-07-14 14:04:19 +09:00
stomar 75e5a91e92 sprintf.c: fix typo
* sprintf.c: [DOC] fix typo.
  Patch by Lazarus Lazaridis (iridakos).  [Fix GH-1789]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63242 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-22 19:41:06 +00:00
nobu c05fa459bb quote symbols
* sprintf.c (ruby__sfvextra): quote symbols as identifiers.

* string.c (rb_id_quote_unprintable): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-14 02:35:51 +00:00
nobu d4d966f967 sprintf.c: overflow check
* sprintf.c (ruby_do_vsnprintf): pathologically, get rid of
  negative value when the result length exceeds INT_MAX.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-09 03:15:20 +00:00
nobu e9cb552ec9 internal.h: remove dependecy on ruby/encoding.h
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 06:24:11 +00:00
nobu 13f5dcb9f2 error.c: KeyError#receiver and KeyError#key
* error.c: new method KeyError#receiver and KeyError#key.
  [Feature #12063]

* hash.c: make KeyError object with receiver and key.

* sprintf.c: ditto.

Author:    ksss <co000ri@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59955 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-18 08:05:53 +00:00
nobu 067d9e9ce9 sprintf.c: NULL as str
* sprintf.c (ruby_vsnprintf, ruby_snprintf): allow NULL as str,
  just count the expected buffer size.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-09 09:16:59 +00:00