[fix] calloc instead of malloc when performing initialization on structs that may fail halfway and need to clean up while partially initialized (calloc will set all the bytes to zero so the member pointers are NULL instead of garbage memory)
This commit is contained in:
@@ -284,7 +284,7 @@ phrase_t search_address_dictionaries_suffix(char *str, size_t len, char *lang) {
|
||||
bool address_dictionary_init(void) {
|
||||
if (address_dict != NULL) return false;
|
||||
|
||||
address_dict = malloc(sizeof(address_dictionary_t));
|
||||
address_dict = calloc(1, sizeof(address_dictionary_t));
|
||||
if (address_dict == NULL) return false;
|
||||
|
||||
address_dict->canonical = cstring_array_new();
|
||||
|
||||
@@ -17,7 +17,7 @@ static address_parser_t *parser = NULL;
|
||||
|
||||
|
||||
address_parser_t *address_parser_new(void) {
|
||||
address_parser_t *parser = malloc(sizeof(address_parser_t));
|
||||
address_parser_t *parser = calloc(1, sizeof(address_parser_t));
|
||||
return parser;
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ averaged_perceptron_t *averaged_perceptron_read(FILE *f) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
averaged_perceptron_t *perceptron = malloc(sizeof(averaged_perceptron_t));
|
||||
averaged_perceptron_t *perceptron = calloc(1, sizeof(averaged_perceptron_t));
|
||||
|
||||
if (!file_read_uint32(f, &perceptron->num_features) ||
|
||||
!file_read_uint32(f, &perceptron->num_classes) ||
|
||||
|
||||
@@ -389,7 +389,7 @@ bool averaged_perceptron_trainer_train_example(averaged_perceptron_trainer_t *se
|
||||
}
|
||||
|
||||
averaged_perceptron_trainer_t *averaged_perceptron_trainer_new(void) {
|
||||
averaged_perceptron_trainer_t *self = malloc(sizeof(averaged_perceptron_trainer_t));
|
||||
averaged_perceptron_trainer_t *self = calloc(1, sizeof(averaged_perceptron_trainer_t));
|
||||
|
||||
if (self == NULL) return NULL;
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ int bloom_filter_add(bloom_filter_t *self, const char *key, size_t len) {
|
||||
}
|
||||
|
||||
bloom_filter_t *bloom_filter_new(uint64_t capacity, double error) {
|
||||
bloom_filter_t *bloom = malloc(sizeof(bloom_filter_t));
|
||||
bloom_filter_t *bloom = calloc(1, sizeof(bloom_filter_t));
|
||||
|
||||
if (bloom == NULL) {
|
||||
return NULL;
|
||||
|
||||
@@ -47,7 +47,7 @@ void geodb_destroy(geodb_t *self) {
|
||||
geodb_t *geodb_init(char *dir) {
|
||||
if (dir == NULL) return NULL;
|
||||
|
||||
geodb_t *gdb = malloc(sizeof(geodb_t));
|
||||
geodb_t *gdb = calloc(1, sizeof(geodb_t));
|
||||
|
||||
if (gdb == NULL) return NULL;
|
||||
|
||||
|
||||
@@ -338,7 +338,7 @@ void geodb_builder_destroy(geodb_builder_t *self) {
|
||||
}
|
||||
|
||||
geodb_builder_t *geodb_builder_new(char *log_filename) {
|
||||
geodb_builder_t *builder = malloc(sizeof(geodb_builder_t));
|
||||
geodb_builder_t *builder = calloc(1, sizeof(geodb_builder_t));
|
||||
|
||||
if (builder == NULL) return NULL;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "graph.h"
|
||||
|
||||
graph_t *graph_new_dims(graph_type_t type, uint32_t m, uint32_t n, size_t nnz, bool fixed_rows) {
|
||||
graph_t *graph = malloc(sizeof(graph_t));
|
||||
graph_t *graph = calloc(1, sizeof(graph_t));
|
||||
graph->m = m;
|
||||
graph->fixed_rows = fixed_rows;
|
||||
graph->n = n;
|
||||
|
||||
@@ -35,7 +35,7 @@ void language_classifier_destroy(language_classifier_t *self) {
|
||||
}
|
||||
|
||||
language_classifier_t *language_classifier_new(void) {
|
||||
language_classifier_t *language_classifier = malloc(sizeof(language_classifier_t));
|
||||
language_classifier_t *language_classifier = calloc(1, sizeof(language_classifier_t));
|
||||
return language_classifier;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ numex_table_t *numex_table_init(void) {
|
||||
numex_table_t *numex_table = get_numex_table();
|
||||
|
||||
if (numex_table == NULL) {
|
||||
numex_table = malloc(sizeof(numex_table_t));
|
||||
numex_table = calloc(1, sizeof(numex_table_t));
|
||||
|
||||
if (numex_table == NULL) return NULL;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "klib/ksort.h"
|
||||
|
||||
sparse_matrix_t *sparse_matrix_new_shape(size_t m, size_t n) {
|
||||
sparse_matrix_t *matrix = malloc(sizeof(sparse_matrix_t));
|
||||
sparse_matrix_t *matrix = calloc(1, sizeof(sparse_matrix_t));
|
||||
if (matrix == NULL) return NULL;
|
||||
matrix->m = m;
|
||||
matrix->n = n;
|
||||
|
||||
@@ -1087,7 +1087,7 @@ transliteration_table_t *transliteration_table_init(void) {
|
||||
transliteration_table_t *trans_table = get_transliteration_table();
|
||||
|
||||
if (trans_table == NULL) {
|
||||
trans_table = malloc(sizeof(transliteration_table_t));
|
||||
trans_table = calloc(1, sizeof(transliteration_table_t));
|
||||
|
||||
trans_table->trie = trie_new();
|
||||
if (trans_table->trie == NULL) {
|
||||
|
||||
@@ -32,7 +32,7 @@ Constructors
|
||||
*/
|
||||
|
||||
static trie_t *trie_new_empty(uint8_t *alphabet, uint32_t alphabet_size) {
|
||||
trie_t *self = malloc(sizeof(trie_t));
|
||||
trie_t *self = calloc(1, sizeof(trie_t));
|
||||
if (!self)
|
||||
goto exit_no_malloc;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user