Skip to content

Commit 893cc75

Browse files
committed
Added the kh_is_end() macro function
The primary reason is to fix an issue that occurs when an element is removed from the khash data during the `KHASH_FOREACH()` loop. If the value of `kh_end()` becomes smaller than `k` during the loop, it will repeat a meaningless internal loop until an integer overflow occurs.
1 parent b18a99c commit 893cc75

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

include/mruby/khash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0};
407407
#define kh_value(name, h, x) (kh_vals_##name(h)[x])
408408
#define kh_begin(h) (khint_t)(0)
409409
#define kh_end(h) ((h)->n_buckets == 0 ? (h)->size : (h)->n_buckets)
410+
#define kh_is_end(h, i) ((i) >= kh_end(h))
410411
#define kh_size(h) ((h)->size)
411412
#define kh_n_buckets(h) ((h)->n_buckets)
412413

@@ -448,7 +449,7 @@ MRB_END_DECL
448449
*/
449450
#define KHASH_FOREACH(name, kh, k) \
450451
if (kh) \
451-
for (khiter_t k = kh_begin(kh); k != kh_end(kh); k++) \
452+
for (khiter_t k = kh_begin(kh); !kh_is_end(kh, k); k++) \
452453
if (kh_exist(name, kh, k))
453454

454455
#endif /* MRUBY_KHASH_H */

0 commit comments

Comments
 (0)