diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 9107ce550..44db05176 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -1464,7 +1464,9 @@ scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure, handle = SCM_CAR (ls); if (!scm_is_pair (handle)) SCM_WRONG_TYPE_ARG (SCM_ARG3, buckets); - fn (closure, handle); + if (!SCM_HASHTABLE_WEAK_P (table) + || !SCM_WEAK_PAIR_DELETED_P (handle)) + fn (closure, handle); ls = SCM_CDR (ls); } } diff --git a/test-suite/tests/hash.test b/test-suite/tests/hash.test index 64d10bb38..4c21d7129 100644 --- a/test-suite/tests/hash.test +++ b/test-suite/tests/hash.test @@ -347,3 +347,15 @@ (pass-if (equal? 2 (hash-count (lambda (k v) (string? v)) table))))) + +;;; +;;; weak key hash table +;;; + +(with-test-prefix "weak key hash table" + (pass-if "hash-for-each after gc" + (let ((table (make-weak-key-hash-table))) + (hashq-set! table (list 'foo) 'bar) + (gc) + ;; Iterate over deleted weak ref without crashing. + (unspecified? (hash-for-each (lambda (key value) key) table)))))