Skip to content

Commit

Permalink
feat: Use _pv destroy functions to satisfy ASAN checks (#1630)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonov548 <[email protected]>
  • Loading branch information
krlmlr and Antonov548 authored Jan 6, 2025
1 parent e735811 commit adbbae0
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/rinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -11272,7 +11272,7 @@ SEXP R_igraph_eulerian_cycle(SEXP graph) {
if (0 != igraph_vector_int_init(&c_vertex_res, 0)) {
igraph_error("", __FILE__, __LINE__, IGRAPH_ENOMEM);
}
IGRAPH_FINALLY(igraph_vector_int_destroy, &c_vertex_res);
IGRAPH_FINALLY_PV(igraph_vector_int_destroy, &c_vertex_res);
/* Call igraph */
IGRAPH_R_CHECK(igraph_eulerian_cycle(&c_graph, &c_edge_res, &c_vertex_res));

Expand Down
18 changes: 18 additions & 0 deletions src/rinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ igraph_error_t R_SEXP_to_attr_comb(SEXP input, igraph_attribute_combination_t *c
else if (__c != IGRAPH_SUCCESS) { R_igraph_error(); } \
} while (0)

// This is a variant of IGRAPH_FINALLY that satisfies UBSAN checks.
#define IGRAPH_FINALLY_PV(func, ptr) \
do { \
/* the following branch makes the compiler check the compatibility of \
* func and ptr to detect cases when we are accidentally invoking an \
* incorrect destructor function with the pointer */ \
if (0) { func(ptr); } \
IGRAPH_FINALLY_REAL((func##_pv), (ptr)); \
} while (0)

// These functions are never meant to be called directly, only through IGRAPH_FINALLY_PV.
void igraph_destroy_pv(void *pv_ptr);
void igraph_matrix_destroy_pv(void *pv_ptr);
void igraph_vector_destroy_pv(void *pv_ptr);
void igraph_vector_int_destroy_pv(void *pv_ptr);
void igraph_vector_bool_destroy_pv(void *pv_ptr);
void igraph_vector_int_list_destroy_pv(void *pv_ptr);

#define IGRAPH_R_CHECK_INT(v) R_check_int_scalar(v)
#define IGRAPH_R_CHECK_REAL(v) R_check_real_scalar(v)
#define IGRAPH_R_CHECK_BOOL(v) R_check_bool_scalar(v)
Expand Down
Loading

0 comments on commit adbbae0

Please sign in to comment.