From: Mathieu Desnoyers Date: Thu, 22 Jun 2023 13:59:53 +0000 (-0400) Subject: Avoid calling caa_container_of on NULL pointer in cds_lfht macros X-Git-Tag: v0.14.1~23 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=5b373e008426545e2dbe7c9b427281aa144b55ef;p=urcu.git Avoid calling caa_container_of on NULL pointer in cds_lfht macros The cds_lfht_for_each_entry and cds_lfht_for_each_entry_duplicate macros would call caa_container_of() macro on NULL pointer. This is not a problem under normal circumstances as the check in the for loop fails and the loop-statement is not called with invalid (pos) value. However AddressSanitizer doesn't like that and complains about this: runtime error: applying non-zero offset 18446744073709551056 to null pointer Move the cds_lfht_iter_get_node(iter) != NULL from the cond-expression of the for loop into both init-clause and iteration-expression as conditional operator and check for (pos) value in the cond-expression instead. Introduce the cds_lfht_entry() macro to eliminate code duplication. Reported-by: Ondřej Surý Signed-off-by: Mathieu Desnoyers Change-Id: I9969c1e0bc0eefc8c90c0d8f17b2927f6a4feb2a --- diff --git a/include/urcu/rculfhash.h b/include/urcu/rculfhash.h index fbd33cc..16f7d1c 100644 --- a/include/urcu/rculfhash.h +++ b/include/urcu/rculfhash.h @@ -544,24 +544,30 @@ void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); cds_lfht_next_duplicate(ht, match, key, iter), \ node = cds_lfht_iter_get_node(iter)) +#define cds_lfht_entry(ptr, type, member) \ + ({ \ + caa_unqual_scalar_typeof(ptr) ___ptr = (ptr); \ + ___ptr ? caa_container_of(___ptr, type, member) : NULL; \ + }) + #define cds_lfht_for_each_entry(ht, iter, pos, member) \ for (cds_lfht_first(ht, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member); \ - cds_lfht_iter_get_node(iter) != NULL; \ + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member); \ + pos != NULL; \ cds_lfht_next(ht, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member)) + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member)) #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ iter, pos, member) \ for (cds_lfht_lookup(ht, hash, match, key, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member); \ - cds_lfht_iter_get_node(iter) != NULL; \ + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member); \ + pos != NULL; \ cds_lfht_next_duplicate(ht, match, key, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member)) + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member)) #ifdef __cplusplus }