Skip to content

Commit 355caa3

Browse files
committed
Add sanitizers build option, fix a few memory leaks.
1 parent b37c626 commit 355caa3

File tree

7 files changed

+35
-1
lines changed

7 files changed

+35
-1
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(CMAKE_C_STANDARD 11)
1111
set(CMAKE_CXX_STANDARD 20)
1212
set(CMAKE_C_FLAGS "-O3 -march=native")
1313

14+
option(ENABLE_SANITIZERS "Enable Clang sanitizers" OFF)
15+
1416
include(GNUInstallDirs)
1517

1618
add_library(binsparse STATIC)
@@ -84,6 +86,12 @@ install(FILES
8486
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
8587

8688
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
89+
if (ENABLE_SANITIZERS)
90+
set(SANITIZER_FLAGS "-fsanitize=address,undefined")
91+
target_compile_options(binsparse INTERFACE ${SANITIZER_FLAGS} -g -O1 -fno-omit-frame-pointer)
92+
target_link_options(binsparse INTERFACE ${SANITIZER_FLAGS})
93+
endif()
94+
8795
add_subdirectory(examples)
8896
add_subdirectory(test)
8997
endif()

examples/check_equivalence.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ int main(int argc, char** argv) {
203203
return 9;
204204
}
205205

206+
bsp_destroy_matrix_t(&matrix1);
207+
bsp_destroy_matrix_t(&matrix2);
208+
bsp_destroy_fdataset_info_t(&info1);
209+
bsp_destroy_fdataset_info_t(&info2);
210+
206211
printf("The files are equivalent.\n");
207212
printf("OK!\n");
208213

examples/mtx2bsp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ int main(int argc, char** argv) {
171171
printf(" === Done writing. ===\n");
172172

173173
bsp_destroy_matrix_t(&matrix);
174+
bsp_destroy_mm_metadata(&m);
175+
bsp_destroy_fdataset_info_t(&info2);
176+
cJSON_Delete(user_json);
174177

175178
return 0;
176179
}

include/binsparse/detail/parse_dataset.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ typedef struct {
1313
char* dataset;
1414
} bsp_fdataset_info_t;
1515

16+
static inline void bsp_destroy_fdataset_info_t(bsp_fdataset_info_t* info) {
17+
free(info->fname);
18+
free(info->dataset);
19+
}
20+
1621
static inline bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
1722
size_t len = strlen(str);
1823

include/binsparse/matrix_market/matrix_market_inspector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ typedef struct bsp_mm_metadata {
3232
char* comments;
3333
} bsp_mm_metadata;
3434

35+
static inline void bsp_destroy_mm_metadata(bsp_mm_metadata* metadata) {
36+
free(metadata->comments);
37+
}
38+
3539
static inline bsp_mm_metadata bsp_mmread_metadata(const char* file_path) {
3640
FILE* f = fopen(file_path, "r");
3741

include/binsparse/matrix_market/matrix_market_read.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static inline bsp_matrix_t bsp_mmread_explicit_array(const char* file_path,
109109

110110
fclose(f);
111111

112+
bsp_destroy_mm_metadata(&metadata);
113+
112114
return matrix;
113115
}
114116

@@ -306,6 +308,8 @@ bsp_mmread_explicit_coordinate(const char* file_path, bsp_type_t value_type,
306308
free(indices);
307309
fclose(f);
308310

311+
bsp_destroy_mm_metadata(&metadata);
312+
309313
return matrix;
310314
}
311315

@@ -315,8 +319,10 @@ static inline bsp_matrix_t bsp_mmread_explicit(const char* file_path,
315319
bsp_mm_metadata metadata = bsp_mmread_metadata(file_path);
316320

317321
if (strcmp(metadata.format, "array") == 0) {
322+
bsp_destroy_mm_metadata(&metadata);
318323
return bsp_mmread_explicit_array(file_path, value_type, index_type);
319324
} else if (strcmp(metadata.format, "coordinate") == 0) {
325+
bsp_destroy_mm_metadata(&metadata);
320326
return bsp_mmread_explicit_coordinate(file_path, value_type, index_type);
321327
} else {
322328
assert(false);
@@ -345,5 +351,7 @@ static inline bsp_matrix_t bsp_mmread(const char* file_path) {
345351

346352
bsp_type_t index_type = bsp_pick_integer_type(max_dim);
347353

354+
bsp_destroy_mm_metadata(&metadata);
355+
348356
return bsp_mmread_explicit(file_path, value_type, index_type);
349357
}

src/write_matrix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
2222

2323
cJSON* item;
2424
cJSON_ArrayForEach(item, user_json) {
25-
cJSON_AddItemToObject(j, item->string, item);
25+
cJSON* item_copy = cJSON_Duplicate(item, 1); // 1 = deep copy
26+
cJSON_AddItemToObject(j, item->string, item_copy);
2627
}
2728

2829
cJSON_AddStringToObject(binsparse, "version", BINSPARSE_VERSION);

0 commit comments

Comments
 (0)