Commit graph

967 commits

Author SHA1 Message Date
Nobuyoshi Nakada
f5c3cda7d6 Do not change hash type in Hash#assoc 2023-11-21 15:15:23 +09:00
Yusuke Endoh
1cf2fa3af5 Raise an exception when Hash#compare_by_identity during its iteration 2023-11-21 15:15:23 +09:00
Yusuke Endoh
a787e0d649 Raise an exception if ar_table is converted to st_table during iteration
ar_table may be converted to st_table by `ar_force_convert_table`.
If the conversion occurs during the iteration of ar_table, the iteration
may lead to memory corruption.

This change prevents the catastrophy by throwing an exception when the
conversion is detected.

This issue is reported by [SuperS](https://hackerone.com/superss)
2023-11-21 15:15:23 +09:00
Nobuyoshi Nakada
60e19a0b5f
Just check if iteration level is non-zero
The level in ivar is no longer needed to check if iterating, only used
for increment/decrement.
2023-11-12 13:51:16 +09:00
Nobuyoshi Nakada
9ab64b1d70
Refactor hash iteration level
- Make it unsigned like as in-flags bits
- Make it long since it should be fixable
- Reduce it to in-flags bits after decrement
2023-11-12 13:51:15 +09:00
Nobuyoshi Nakada
9eac9d7178
[Bug #19969] Compact st_table after deleted if possible 2023-11-11 18:49:19 +09:00
Yuki Tsujimoto
e721a7aec7
[DOC] Update documentation for typical implementation of hash 2023-10-22 09:47:22 +00:00
Nobuyoshi Nakada
262a0cc868
[DOC] Missing comment marker 2023-09-27 15:50:37 +09:00
Peter Zhu
f43dac0df2 Add rb_hash_free for the GC to use 2023-09-24 09:07:52 -04:00
Peter Zhu
36cdf163df Add hash_st_free 2023-09-24 09:07:34 -04:00
Peter Zhu
61a2e9450c Fix memory leak in Hash#rehash for ST hashes
We need to free the old ST table in Hash#rehash.

Co-authored-by: Adam Hess <adamhess1991@gmail.com>
2023-09-23 11:24:41 -04:00
Nobuyoshi Nakada
779cab6655
RHASH_AR_TABLE never returns NULL now [ci skip] 2023-09-06 17:46:36 +09:00
Dorian Marié (perso)
8cd62fbc17
[DOC] Typo in Hash#key description "so/no such value" 2023-09-03 01:47:50 +09:00
Dorian Marié (perso)
3887b45f47
[DOC] FIx typo in description of Hash#hash (regardless or/of order) 2023-09-03 01:46:33 +09:00
BurdetteLamar
b18462c14b [DOC] Don't suppress autolinks 2023-08-12 10:04:39 -04:00
Nobuyoshi Nakada
8f2bbbbcaf
Add assertion in RHASH_AR_TABLE_BOUND 2023-08-03 13:17:41 +09:00
Peter Zhu
de327ccb63 Remove RARRAY_PTR_USE_TRANSIENT
RARRAY_PTR_USE now does the same things as RARRAY_PTR_USE_TRANSIENT.
2023-07-13 14:48:14 -04:00
Peter Zhu
58386814a7 Don't check for null pointer in calls to free
According to the C99 specification section 7.20.3.2 paragraph 2:

> If ptr is a null pointer, no action occurs.

So we do not need to check that the pointer is a null pointer.
2023-06-30 09:13:31 -04:00
Peter Zhu
d3ff43852b Fix memory leak in Hash#replace
Hash#replace can leak memory if the receiver has an ST table.
2023-06-29 19:07:26 -04:00
Peter Zhu
f0d08d11dc Fix memory leak when copying ST tables
st_copy allocates a st_table, which is not needed for hashes since it is
allocated by VWA and embedded, so this causes a memory leak.

The following script demonstrates the issue:

```ruby
20.times do
  100_000.times do
    {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9}
  end

  puts `ps -o rss= -p #{$$}`
end
```
2023-06-29 11:16:50 -04:00
Peter Zhu
df2b3a2998 Refactor rb_hash_replace to use hash_copy 2023-06-29 11:16:50 -04:00
Nobuyoshi Nakada
73529a12bb
Declare RHASH_AR_TABLE and RHASH_ST_TABLE return non-null 2023-06-23 14:54:50 +09:00
Peter Zhu
52f8de4f21 Remove dead code in hash.c
RHASH_TABLE_NULL_P and ar_alloc_table are no longer needed since
all Hash will have AR tables.
2023-06-22 14:08:25 -04:00
Peter Zhu
3168f618cb hash.c no longer needs the transient heap 2023-06-12 14:38:56 -04:00
git
4149630566 * remove trailing spaces. [ci skip] 2023-06-08 14:28:48 +00:00
Peter Zhu
d9ef326d29 [DOC] Add comment about flags for Hash 2023-06-08 10:28:16 -04:00
Nobuyoshi Nakada
a5e1d549b5 [DOC] Mention the edge case of any?/all? 2023-06-01 21:44:53 +09:00
Peter Zhu
9592bc7039 Remove dead code in rb_hash_replace
We now always copy the ST table, so we don't need to initialize the ST
table of hash when hash2 is empty.
2023-05-23 15:27:56 -04:00
Peter Zhu
a86f798fc2 Fix crash when replacing ST hash with AR hash
With VWA, AR hashes are much larger than ST hashes. Hash#replace
attempts to directly copy the contents of AR hashes into ST hashes so
there will be memory corruption caused by writing past the end of memory.

This commit changes it so that if a ST hash is being replaced with an AR
hash it will insert each element into the ST hash.
2023-05-23 15:27:56 -04:00
Peter Zhu
24a6609ccb Remove dead code in ar_copy
new_tab can no longer ever be NULL so this is dead code.
2023-05-23 11:11:23 -04:00
Jean byroot Boussier
31ac8efca8
Hash.new: print a deprecation warning when receiving keyword arguments (#7828)
[Feature #19236]

In Ruby 3.3, `Hash.new` shall print a deprecation warning if keyword arguments
are passed instead of treating them as an implicit positional Hash.

This will allow to safely introduce a `capacity` keyword argument in 3.4

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2023-05-23 15:51:28 +02:00
Peter Zhu
cea9c30fa5 Move ar_hint to ar_table_struct
This allows Hashes with ST tables to fit int he 80 byte size pool.
2023-05-17 09:19:40 -04:00
Peter Zhu
0938964ba1 Implement Hash ST tables on VWA 2023-05-17 09:19:40 -04:00
Peter Zhu
5199f2aaf9 Implement Hash AR tables on VWA 2023-05-17 09:19:40 -04:00
Nobuyoshi Nakada
30d7205671
Add rb_sys_fail_sprintf macro 2023-05-12 18:36:02 +09:00
Piotr Szotkowski
33833aaf03
[DOC] hash.c: fix typo in #<=> docs 2023-05-01 08:09:14 +09:00
Nobuyoshi Nakada
607cd24128
Adjust function style [ci skip] 2023-04-15 11:50:54 +09:00
Jean Boussier
8c360ce713 hash.c: Fix hash_iter_lev_dec corrupting shape
[Bug #19589]

When decrementing `iter_lev` from `65` to `64` the flags would be
corrupted, causing the shape_id to be invalid.
2023-04-11 16:56:14 +02:00
Matt Valentine-House
026321c5b9 [Feature #19474] Refactor NEWOBJ macros
NEWOBJ_OF is now our canonical newobj macro. It takes an optional ec
2023-04-06 11:07:16 +01:00
Jeremy Evans
5d6579bd91 Change Hash#compact to keep default values and compare_by_identity flag
The documentation states it returns a copy of self with nil value
entries removed.  However, the previous behavior was creating a
plain new hash with non-nil values copied into it.  This change
aligns the behavior with the documentation.

Fixes [Bug #19113]
2023-03-24 10:55:13 -07:00
Jeremy Evans
1b13db25d8 Copy compare_by_identity flag for empty hashes in Hash.ruby2_keywords_hash
This was already copied for non-empty hashes.  As Hash.ruby2_keywords_hash
copies default values, it should also copy the compare_by_identity flag.

Partially Fixes [Bug #19113]
2023-03-24 10:55:13 -07:00
Jeremy Evans
d3197def88 Do not copy compare_by_identity flag for non-empty hashes in Hash.[]
It wasn't copied for empty hashes, and Hash.[] doesn't copy the
default value, so copying the compare_by_identity flag does not
make sense.

Partially Fixes [Bug #19113]
2023-03-24 10:55:13 -07:00
Takashi Kokubun
50a709fb9e Resurrect symbols used by ObjectSpace 2023-03-06 21:59:23 -08:00
Takashi Kokubun
233ddfac54 Stop exporting symbols for MJIT 2023-03-06 21:59:23 -08:00
Jean Boussier
2a5354e593 Implement ObjectSpace::WeakKeyMap basic allocator
[Feature #18498]
2023-02-23 16:01:57 +01:00
BurdetteLamar
3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00
Matt Valentine-House
72aba64fff Merge gc.h and internal/gc.h
[Feature #19425]
2023-02-09 10:32:29 -05:00
Peter Zhu
e30cf60a19 Remove dead code in ar_copy
All of the callers of ar_copy guarantee that hash2 has an ar table,
so we don't need the logic to handle the case when hash2 doesn't have
an ar table.
2023-02-03 14:59:00 -05:00
Peter Zhu
5b34839b0f Remove redundant else if statement in hash.c
Hashes can only be ar or st, so the else if is redundant.
2023-02-02 14:43:50 -05:00
Peter Zhu
9f9dca0c52 Remove rb_hash_ar_table_p
It's dead code and duplicate of RHASH_AR_TABLE_P.
2023-01-31 15:48:09 -05:00