From 61ef5aab37634c83d80b6d60cd586b4079f5a405 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Sat, 19 Aug 2017 18:46:59 +0300 Subject: [PATCH] Add assertion that no hb_n_marks underflow occurs Issue #177 (bdwgc). * alloc.c (GC_clear_fl_marks): Add GC_ASSERT that hhdr->hb_n_marks is non-zero before decrementing it. * mark.c (GC_clear_mark_bit): Likewise. --- alloc.c | 7 +++++-- mark.c | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/alloc.c b/alloc.c index e69558f1..799ee294 100644 --- a/alloc.c +++ b/alloc.c @@ -388,7 +388,7 @@ STATIC void GC_maybe_gc(void) } /* We try to mark with the world stopped. */ /* If we run out of time, this turns into */ - /* incremental marking. */ + /* incremental marking. */ # ifndef NO_CLOCK if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); } # endif @@ -851,8 +851,11 @@ STATIC void GC_clear_fl_marks(ptr_t q) unsigned bit_no = MARK_BIT_NO((ptr_t)q - (ptr_t)h, sz); if (mark_bit_from_hdr(hhdr, bit_no)) { - size_t n_marks = hhdr -> hb_n_marks - 1; + size_t n_marks = hhdr -> hb_n_marks; + + GC_ASSERT(n_marks != 0); clear_mark_bit_from_hdr(hhdr, bit_no); + n_marks--; # ifdef PARALLEL_MARK /* Appr. count, don't decrement to zero! */ if (0 != n_marks || !GC_parallel) { diff --git a/mark.c b/mark.c index 0c48e0a8..60088852 100644 --- a/mark.c +++ b/mark.c @@ -203,9 +203,11 @@ GC_API void GC_CALL GC_clear_mark_bit(const void *p) word bit_no = MARK_BIT_NO((ptr_t)p - (ptr_t)h, hhdr -> hb_sz); if (mark_bit_from_hdr(hhdr, bit_no)) { - size_t n_marks; + size_t n_marks = hhdr -> hb_n_marks; + + GC_ASSERT(n_marks != 0); clear_mark_bit_from_hdr(hhdr, bit_no); - n_marks = hhdr -> hb_n_marks - 1; + n_marks--; # ifdef PARALLEL_MARK if (n_marks != 0 || !GC_parallel) hhdr -> hb_n_marks = n_marks; -- 2.40.0