From: naruse@... Date: 2020-03-26T14:59:02+00:00 Subject: [ruby-core:97600] [Ruby master Bug#16676] `#hash` can change Hash object from ar_table to st_table Issue #16676 has been updated by naruse (Yui NARUSE). Backport changed from 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: REQUIRED to 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: DONE ruby_2_7 ab6f78bc926f6fc12dc8d7846056fc9c04d63ead. ---------------------------------------- Bug #16676: `#hash` can change Hash object from ar_table to st_table https://bugs.ruby-lang.org/issues/16676#change-84786 * Author: ko1 (Koichi Sasada) * Status: Closed * Priority: Normal * Assignee: ko1 (Koichi Sasada) * Target version: 2.8 * Backport: 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: DONE ---------------------------------------- # Points * Hash representation `ar_table` has a problem from Ruby 2.6 (need to backport) # Problem From Ruby 2.6 `Hash` object is represented by `ar_table` (for small (<=8) pairs) and `st_table`. To lookup hash table, `Object#hash` is called to get a hash value, and this method call can modify Hash object. It means that the code assumes the Hash representation is a `ar_table`, but calling the `#hash` method, it can be a `st_table`. I believe nobody modify Hash table from `#hash` method (it should be pure, no-side effect method), but multi-threads can modify a Hash object, it can be exposed. I will commit a patch soon. # Workaround To avoid this issue, making a hash table in `st_table`, for example: ``` h = {} # ar_table 10.times{|i| h[i] = i} # st_table because the number of pair == 10 (> 8) h.clear ``` -- https://bugs.ruby-lang.org/ Unsubscribe: