1102 Commits
Author SHA1 Message Date
Étienne Barrié 6776cc5a38 Remove discard qualifiers warning with C23 strchr
C23 has qualifier-preserving standard library functions, so calling
strchr with a `const char *` will return a `const char *`. We can change
the type of the local variables because we don't mutate the strings.

(cherry picked from commit 42b3cdc51a)
2026-10-15 12:37:44 -07:00
Takashi Kokubun 137e40a9f7 merge revision(s) 239a472609, d29afc8ab8: [Backport #20958]
[PATCH] fix ENV.keys encoding on windows

	Fix the encoding of ENV.keys elements to UTF-8 on windows.
	They contain UTF-8 byte sequences with locale encoding.

	[PATCH] [Bug #20958] Fix test_keys_encoding on hosts without en_US.UTF-8

	`EnvUtil.invoke_ruby` forces the C locale on the child process, so the
	test set `LC_ALL=en_US.UTF-8` to get a UTF-8 locale back.  That locale
	is not generated on the debian11 chkbuild host, where `setlocale` falls
	back to C and the ENV key comes out as ASCII-8BIT instead of the
	default internal encoding.  Inherit the locale of the parent process
	instead, and omit the test when that locale is not UTF-8.

	https://rubyci.s3.amazonaws.com/debian11/ruby-master/log/20260725T093003Z.fail.html.gz
2026-09-14 12:43:45 -07:00
Benoit Daloze 37ab334cd7 Fix ar_find_entry_hint() to handle #eql? or another thread changing bound or converting to st_table
* While perusing code in hash.c I found it suspicious that ar_find_entry_hint()
  didn't reread bound in the loop and yet called arbitrary code through #eql?.
* ar_find_entry_hint() before this commit would not check if bound or the storage (AR->ST) changed
  and would return a bin index which is not correct to access.
* Check bound in the loop so it's always up-to-date.
* Return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE when converted to st_table
  and make callers retry the operation as a st_table.

[Bug #22120]

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit c88430e187)
2026-06-22 09:58:45 -07:00
John Hawthorn 642798eeca Add write barriers on Hash#rehash 2026-05-05 13:27:43 -07:00
BurdetteLamar d540903ee7 [DOC] Harmonize <= methods 2025-12-19 17:09:10 -05:00
Burdette Lamar b14f2f0116 [DOC] Harmonize lt methods 2025-12-18 19:21:00 -05:00
Jean Boussier 5770c186d1 Rename rb_obj_exivar_p -> rb_obj_gen_fields_p
The "EXIVAR" terminology has been replaced by "gen fields"
AKA "generic fields".

Exivar implies variable, but generic fields include more than
just variables, e.g. `object_id`.
2025-12-03 15:57:26 +01:00
Stan Lo 4cd6661e18 Reorganize page documentations (#15154)
Re-organize page docs
2025-11-27 20:12:24 +00:00
Peter Zhu 6014ed9968 Remove dead rb_hash_dump 2025-11-05 22:31:17 -05:00
Sam Westerman 17a7b4e031 [DOC] Fix typo in Hash#compare_by_identity docs 2025-11-01 09:18:01 +09:00
Koichi Sasada bc00c4468e use SET_SHAREABLE
to adopt strict shareable rule.

* (basically) shareable objects only refer shareable objects
* (exception) shareable objects can refere unshareable objects
  but should not leak reference to unshareable objects to Ruby world
2025-10-23 13:08:26 +09:00
MSP-Greg b92db45b98 [DOC] hash.c - fix 3 class doc typos 2025-10-03 11:08:45 +09:00
Yaroslav 8a704783f8 [DOC] Fix outdated ENV::clone method description
It does raise an exception rather than just issuing a warning:

```shell
$ docker run -e ALL_RUBY_SINCE=2.7 --rm rubylang/all-ruby ./all-ruby -We 'ENV.clone'
ruby-2.7.0
...
ruby-3.0.7
ruby-3.1.0-preview1 -e:1: warning: ENV.clone is deprecated; use ENV.to_h instead
...
ruby-3.2.0-preview1 -e:1: warning: ENV.clone is deprecated; use ENV.to_h instead
ruby-3.2.0-preview2 -e:1:in `clone': Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash (TypeError)
                    	from -e:1:in `<main>'
                exit 1
...
ruby-3.3.9          -e:1:in `clone': Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash (TypeError)
                    	from -e:1:in `<main>'
                exit 1
ruby-3.4.0-preview1 -e:1:in 'clone': Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash (TypeError)
                    	from -e:1:in '<main>'
                exit 1
...
ruby-3.5.0-preview1 -e:1:in 'clone': Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash (TypeError)
                    	from -e:1:in '<main>'
                exit 1

```
2025-08-24 06:46:32 +09:00
Burdette Lamar b3053cbb34 [DOC] Tweaks for Object#hash 2025-08-19 19:34:40 -04:00
Luke Gruber 1afc07e815 Allow encodings to be autoloaded through transcoding functions
Make sure VM lock is not held when calling `load_transcoder_entry`, as
that causes deadlock inside ractors. `String#encode` now works inside
ractors, among others.

Atomic load the rb_encoding_list

Without this, wbcheck would sometimes hit a missing write barrier.

Co-authored-by: John Hawthorn <john.hawthorn@shopify.com>

Hold VM lock when iterating over global_enc_table.names

This st_table can be inserted into at runtime when autoloading
encodings.

minor optimization when calling Encoding.list
2025-08-12 15:19:02 -07:00
Jean Boussier cbe5241c77 [DOC] Mention that Hash#replace also replaces defaults 2025-07-30 10:40:34 +02:00
Peter Zhu 23000e7123 Remove unnecessary internal/gc.h include in hash.c
hash.c compiles just fine on HASH_DEBUG without including internal/gc.h.
2025-07-28 17:18:35 -04:00
Nobuyoshi Nakada abafb662ea Adjust indents [ci skip] 2025-07-26 22:07:28 +09:00
Peter Zhu a2e165e8a0 Remove dsymbol_fstr_hash
We don't need to delay the freeing of the fstr for the symbol if we store
the hash of the fstr in the dynamic symbol and we use compare-by-identity
for removing the dynamic symbol from the sym_set.
2025-07-21 10:58:30 -04:00
Alan Wu b49ae45b88 Remove dead post-hoc rehash check
Hash#rehash checks whether the hash is iterating, and with VWA,
RHASH_ST_TABLE() always returns the same thing for the same
hash.

    RHASH_ST_TABLE(VALUE h)
    {
        return (st_table *)((uintptr_t)h + sizeof(struct RHash));
    }

So this check can never fail and raise an exception. Remove it.
2025-07-18 23:30:24 +00:00
Erik Berlin bd27460c06 Remove rehash checking TODO (GH-13919)
Hash#rehash checks for rehash during iteration, and there
seems to be no efficient way to check for it after the fact,
so remove the TODO.
2025-07-18 17:58:04 -04:00
Erik Berlin abc8745fc3 Avoid second RHASH_AR_TABLE_REF lookup 2025-07-16 16:13:21 -04:00
John HawthornandLuke Gruber fcf2c3b4d1 Fix write barriers in rb_hash_add_new_element
The write barriers must be run after the st_update callback returns,
as the objects are not on the object until then and there may be
allocation when there is a new object inserted.

This is hard to reproduce, and I haven't seen an actual crash due to it,
but it is detected by wbcheck

    RUBY_GC_LIBRARY=wbcheck WBCHECK_VERIFY_AFTER_WB=1 ./miniruby -e '("a".."zz").uniq.to_a'
    WBCHECK ERROR: Missed write barrier detected!
      Parent object: 0x720db01f99c0 (wb_protected: true)
        rb_obj_info_dump: 0x0000720db01f99c0 T_HASH/[S] 18
      Reference counts - snapshot: 32, writebarrier: 2, current: 36, missed: 2
      Missing reference to: 0x716db02e3450
        rb_obj_info_dump: 0x0000716db02e3450 T_STRING/String  len: 1, capa: 15 "q"
      Missing reference to: 0x716db02e3450
        rb_obj_info_dump: 0x0000716db02e3450 T_STRING/String  len: 1, capa: 15 "q"

A part of why this is hard to reproduce and it's unlikely to crash is
that the insertion only rarely allocates.

Co-authored-by: Luke Gruber <luke.gruber@shopify.com>
2025-06-24 13:08:35 -07:00
Nobuyoshi Nakada a60bf9e693 * adjust indent 2025-06-17 12:30:18 +09:00
Jean Boussier 6dbe24fe56 Use the shape_id rather than FL_EXIVAR
We still keep setting `FL_EXIVAR` so that `rb_shape_verify_consistency`
can detect discrepancies.
2025-06-13 23:50:30 +02:00
Nobuyoshi Nakada aad9fa2853 Use RB_VM_LOCKING 2025-05-25 15:22:43 +09:00
Daniel Colson 0564973196 [Bug #21357] Fix crash in Hash#merge with block
Prior to https://github.com/ruby/ruby/commit/49b306ecb9e2e9e06e0b1590bacc5f4b38169c3c
the `optional_arg` passed from `rb_hash_update_block_i` to `tbl_update`
was a hash value (i.e. a VALUE). After that commit it changed to an
`update_call_args`.

If the block sets or changes the value, `tbl_update_modify` will set the
`arg.value` back to an actual value and we won't crash. But in the case
where the block returns the original value we end up calling
`RB_OBJ_WRITTEN` with the `update_call_args` which is not expected and
may crash.

`arg.value` appears to only be used to pass to `RB_OBJ_WRITTEN` (others
who need the `update_call_args` get it from `arg.arg`), so I don't think
it needs to be set to anything upfront. And `tbl_update_modify` will set
the `arg.value` in the cases we need the write barrier.
2025-05-22 12:25:55 +09:00
Nobuyoshi Nakada 49b306ecb9 [Bug #21333] Prohibit hash modification inside Hash#update block 2025-05-15 15:39:15 +09:00
Nobuyoshi Nakada 7793b59c8d [Bug #21331] Prohibit hash modification during stlike loop 2025-05-14 10:37:59 +09:00
Nobuyoshi Nakada 7f5b4fb26e Remove unused retval assignments 2025-05-14 10:26:04 +09:00
Nobuyoshi Nakada b66c5c3b1b Revert "[Bug #21331] Prohibit modification during stlike loop"
This reverts commit bb180b87b4, which
caused "malloc during GC" error on wasm.
2025-05-14 10:23:16 +09:00
Nobuyoshi Nakada bb180b87b4 [Bug #21331] Prohibit modification during stlike loop 2025-05-13 23:16:58 +09:00
Jeremy EvansandNobuyoshi Nakada 926411171d Support Marshal.{dump,load} for core Set
This was missed when adding core Set, because it's handled
implicitly for T_OBJECT.

Keep marshal compatibility between core Set and stdlib Set,
so you can unmarshal core Set with stdlib Set and vice versa.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2025-04-28 08:38:35 -07:00
BurdetteLamar 3a7b03242f [DOC] Tweaks for Hash doc 2025-04-02 12:10:56 -04:00
BurdetteLamar 2d9036498e [DOC] Tweaks for Hash doc 2025-03-31 14:49:40 -04:00
Burdette Lamar cdb1bf1e53 [DOC] Tweaks for Hash#update 2025-03-31 14:47:20 -04:00
BurdetteLamar 9e87323ad0 [DOC] Tweaks for Hash#values_at 2025-03-29 17:05:21 -04:00
BurdetteLamar dbc1ceca32 [DOC] Tweaks for Hash#values 2025-03-29 17:04:56 -04:00
BurdetteLamar 4e3bc65e88 [DOC] Tweaks for Hash#has_value? 2025-03-29 17:04:33 -04:00
Burdette Lamar 0b186ed413 [DOC] Doc for Hash#transform_keys! (#12942) 2025-03-25 15:26:51 -04:00
Burdette Lamar 12b2b577b7 [DOC] Doc for Hash#transform_values! 2025-03-23 11:09:08 -04:00
BurdetteLamar 383af53a56 [DOC] Doc for Hash#transform_values 2025-03-23 11:07:31 -04:00
Peter Zhu 2dc501497e [DOC] Fix indentation for documentation of Hash#compact 2025-03-17 16:12:34 -04:00
BurdetteLamar 70a9e5f6eb [DOC] Doc for Hash#transform_keys 2025-03-16 11:35:32 -04:00
Burdette Lamar efb2d43637 [DOC] Tweaks for Hash#to_h (#12918) 2025-03-14 10:41:27 -04:00
BurdetteLamar a4328a424b [DOC] Tweaks for Hash#to_hash 2025-03-13 21:12:08 -04:00
BurdetteLamar 057ee25d1b [DOC] Tweaks for Hash#to_proc 2025-03-13 21:11:47 -04:00
Burdette Lamar 1b2cc9c2b8 [DOC] Tweaks for Hash#slice 2025-03-11 21:54:49 -04:00
BurdetteLamar 86eff8565b [DOC] Tweaks for Hash#to_a 2025-03-11 21:54:06 -04:00
BurdetteLamar 5208d2f440 [DOC] Tweaks for Hash#shift 2025-03-11 21:52:38 -04:00