shm: add safety assertions
authorSimon Ser <contact@emersion.fr>
Wed, 2 Jun 2021 14:35:30 +0000 (16:35 +0200)
committerSimon Ser <contact@emersion.fr>
Tue, 29 Jun 2021 09:17:44 +0000 (11:17 +0200)
Catch any API mis-use with an assert. This should abort when the
user calls unreferences the pool more times than it's referenced.

Also change the refcount check to explicitly check for positive
counts. That makes the condition more readable.

Signed-off-by: Simon Ser <contact@emersion.fr>
src/wayland-shm.c

index aa64ff3..85204e4 100644 (file)
@@ -106,13 +106,15 @@ shm_pool_unref(struct wl_shm_pool *pool, bool external)
 {
        if (external) {
                pool->external_refcount--;
+               assert(pool->external_refcount >= 0);
                if (pool->external_refcount == 0)
                        shm_pool_finish_resize(pool);
        } else {
                pool->internal_refcount--;
+               assert(pool->internal_refcount >= 0);
        }
 
-       if (pool->internal_refcount + pool->external_refcount)
+       if (pool->internal_refcount + pool->external_refcount > 0)
                return;
 
        munmap(pool->data, pool->size);