Skip to content

Commit a3494f1

Browse files
committed
Add some memory safety
1 parent ac121f3 commit a3494f1

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

include/binsparse/hdf5_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ bsp_read_attribute_allocator(char** string, hid_t f, const char* label,
293293
*string = (char*) allocator.malloc(size + 1);
294294

295295
H5Aread(attribute, strtype, *string);
296+
(*string)[size] = '\0';
296297

297298
H5Aclose(attribute);
298299
H5Tclose(strtype);

src/write_matrix.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
2121

2222
cJSON_AddItemToObject(j, "binsparse", binsparse);
2323

24-
cJSON* item;
25-
cJSON_ArrayForEach(item, user_json) {
26-
cJSON* item_copy = cJSON_Duplicate(item, 1); // 1 = deep copy
27-
cJSON_AddItemToObject(j, item->string, item_copy);
24+
if (user_json != NULL) {
25+
cJSON* item;
26+
cJSON_ArrayForEach(item, user_json) {
27+
cJSON* item_copy = cJSON_Duplicate(item, 1); // 1 = deep copy
28+
cJSON_AddItemToObject(j, item->string, item_copy);
29+
}
2830
}
2931

3032
cJSON_AddStringToObject(binsparse, "version", BINSPARSE_VERSION);
@@ -136,7 +138,13 @@ bsp_error_t bsp_write_matrix_to_group_cjson(hid_t f, bsp_matrix_t matrix,
136138
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
137139
const char* user_json,
138140
int compression_level) {
139-
cJSON* user_json_cjson = cJSON_Parse(user_json);
141+
cJSON* user_json_cjson = NULL;
142+
if (user_json != NULL) {
143+
user_json_cjson = cJSON_Parse(user_json);
144+
}
145+
if (user_json_cjson == NULL) {
146+
user_json_cjson = cJSON_CreateObject();
147+
}
140148
bsp_error_t error = bsp_write_matrix_to_group_cjson(
141149
f, matrix, user_json_cjson, compression_level);
142150
cJSON_Delete(user_json_cjson);
@@ -181,7 +189,13 @@ bsp_error_t bsp_write_matrix_cjson(const char* fname, bsp_matrix_t matrix,
181189
bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
182190
const char* group, const char* user_json,
183191
int compression_level) {
184-
cJSON* user_json_cjson = cJSON_Parse(user_json);
192+
cJSON* user_json_cjson = NULL;
193+
if (user_json != NULL) {
194+
user_json_cjson = cJSON_Parse(user_json);
195+
}
196+
if (user_json_cjson == NULL) {
197+
user_json_cjson = cJSON_CreateObject();
198+
}
185199

186200
bsp_error_t error = bsp_write_matrix_cjson(
187201
fname, matrix, group, user_json_cjson, compression_level);

0 commit comments

Comments
 (0)