[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:
Al
2017-01-13 18:06:41 -05:00
parent 1398df1260
commit df89387b5c
13 changed files with 15 additions and 15 deletions

View File

@@ -284,7 +284,7 @@ phrase_t search_address_dictionaries_suffix(char *str, size_t len, char *lang) {
bool address_dictionary_init(void) { bool address_dictionary_init(void) {
if (address_dict != NULL) return false; 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; if (address_dict == NULL) return false;
address_dict->canonical = cstring_array_new(); address_dict->canonical = cstring_array_new();

View File

@@ -17,7 +17,7 @@ static address_parser_t *parser = NULL;
address_parser_t *address_parser_new(void) { 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; return parser;
} }

View File

@@ -93,7 +93,7 @@ averaged_perceptron_t *averaged_perceptron_read(FILE *f) {
return NULL; 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) || if (!file_read_uint32(f, &perceptron->num_features) ||
!file_read_uint32(f, &perceptron->num_classes) || !file_read_uint32(f, &perceptron->num_classes) ||
@@ -216,4 +216,4 @@ void averaged_perceptron_destroy(averaged_perceptron_t *self) {
} }
free(self); free(self);
} }

View File

@@ -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 *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; if (self == NULL) return NULL;

View File

@@ -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_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) { if (bloom == NULL) {
return NULL; return NULL;
@@ -220,4 +220,4 @@ void bloom_filter_destroy(bloom_filter_t *self) {
} }
free(self); free(self);
} }

View File

@@ -47,7 +47,7 @@ void geodb_destroy(geodb_t *self) {
geodb_t *geodb_init(char *dir) { geodb_t *geodb_init(char *dir) {
if (dir == NULL) return NULL; 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; if (gdb == NULL) return NULL;

View File

@@ -338,7 +338,7 @@ void geodb_builder_destroy(geodb_builder_t *self) {
} }
geodb_builder_t *geodb_builder_new(char *log_filename) { 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; if (builder == NULL) return NULL;

View File

@@ -1,7 +1,7 @@
#include "graph.h" #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_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->m = m;
graph->fixed_rows = fixed_rows; graph->fixed_rows = fixed_rows;
graph->n = n; graph->n = n;

View File

@@ -35,7 +35,7 @@ void language_classifier_destroy(language_classifier_t *self) {
} }
language_classifier_t *language_classifier_new(void) { 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; return language_classifier;
} }

View File

@@ -51,7 +51,7 @@ numex_table_t *numex_table_init(void) {
numex_table_t *numex_table = get_numex_table(); numex_table_t *numex_table = get_numex_table();
if (numex_table == NULL) { 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; if (numex_table == NULL) return NULL;

View File

@@ -2,7 +2,7 @@
#include "klib/ksort.h" #include "klib/ksort.h"
sparse_matrix_t *sparse_matrix_new_shape(size_t m, size_t n) { 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; if (matrix == NULL) return NULL;
matrix->m = m; matrix->m = m;
matrix->n = n; matrix->n = n;

View File

@@ -1087,7 +1087,7 @@ transliteration_table_t *transliteration_table_init(void) {
transliteration_table_t *trans_table = get_transliteration_table(); transliteration_table_t *trans_table = get_transliteration_table();
if (trans_table == NULL) { if (trans_table == NULL) {
trans_table = malloc(sizeof(transliteration_table_t)); trans_table = calloc(1, sizeof(transliteration_table_t));
trans_table->trie = trie_new(); trans_table->trie = trie_new();
if (trans_table->trie == NULL) { if (trans_table->trie == NULL) {

View File

@@ -32,7 +32,7 @@ Constructors
*/ */
static trie_t *trie_new_empty(uint8_t *alphabet, uint32_t alphabet_size) { 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) if (!self)
goto exit_no_malloc; goto exit_no_malloc;