From e1f258171fb4a6d415d40241be00d7eda43f6110 Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 13 Jan 2017 16:52:41 -0500 Subject: [PATCH 1/3] [fix] handle cstring_array_from_char_array where char_array is NULL or 0-length --- src/string_utils.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/string_utils.c b/src/string_utils.c index 7bfecb09..64984564 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -711,16 +711,17 @@ cstring_array *cstring_array_new_size(size_t size) { cstring_array *cstring_array_from_char_array(char_array *str) { cstring_array *array = malloc(sizeof(cstring_array)); - if (array == NULL) return NULL; + if (array == NULL || str == NULL) return NULL; array->str = str; array->indices = uint32_array_new_size(1); uint32_array_push(array->indices, 0); char *ptr = str->a; - uint32_t i = 0; - for (i = 0; i < str->n - 1; i++, ptr++) { - if (*ptr == '\0') { - uint32_array_push(array->indices, i + 1); + if (str->n > 0) { + for (uint32_t i = 0; i < str->n - 1; i++, ptr++) { + if (*ptr == '\0') { + uint32_array_push(array->indices, i + 1); + } } } return array; From 1398df1260ed9d6a76c2c097e8d41223e58a2f5e Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 13 Jan 2017 17:49:31 -0500 Subject: [PATCH 2/3] [fix] accept 0 for array_new_size --- src/vector.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vector.h b/src/vector.h index 25b7896d..8a626ea6 100644 --- a/src/vector.h +++ b/src/vector.h @@ -34,7 +34,7 @@ static inline void _aligned_free(void *p) name *array = malloc(sizeof(name)); \ if (array == NULL) return NULL; \ array->n = array->m = 0; \ - array->a = malloc(size * sizeof(type)); \ + array->a = malloc((size > 0 ? size : 1) * sizeof(type)); \ if (array->a == NULL) return NULL; \ array->m = size; \ return array; \ From df89387b5c6472335df9ce387de15f5f1b65af20 Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 13 Jan 2017 18:06:41 -0500 Subject: [PATCH 3/3] [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) --- src/address_dictionary.c | 2 +- src/address_parser.c | 2 +- src/averaged_perceptron.c | 4 ++-- src/averaged_perceptron_trainer.c | 2 +- src/bloom.c | 4 ++-- src/geodb.c | 2 +- src/geodb_builder.c | 2 +- src/graph.c | 2 +- src/language_classifier.c | 2 +- src/numex.c | 2 +- src/sparse_matrix.c | 2 +- src/transliterate.c | 2 +- src/trie.c | 2 +- 13 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/address_dictionary.c b/src/address_dictionary.c index b0d5e345..a41fb135 100644 --- a/src/address_dictionary.c +++ b/src/address_dictionary.c @@ -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(); diff --git a/src/address_parser.c b/src/address_parser.c index 507bee2a..187bada5 100644 --- a/src/address_parser.c +++ b/src/address_parser.c @@ -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; } diff --git a/src/averaged_perceptron.c b/src/averaged_perceptron.c index a66470e4..59a58f2e 100644 --- a/src/averaged_perceptron.c +++ b/src/averaged_perceptron.c @@ -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) || @@ -216,4 +216,4 @@ void averaged_perceptron_destroy(averaged_perceptron_t *self) { } free(self); -} \ No newline at end of file +} diff --git a/src/averaged_perceptron_trainer.c b/src/averaged_perceptron_trainer.c index 83693ef8..e10ae499 100644 --- a/src/averaged_perceptron_trainer.c +++ b/src/averaged_perceptron_trainer.c @@ -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; diff --git a/src/bloom.c b/src/bloom.c index 109f9db9..3e0671b9 100644 --- a/src/bloom.c +++ b/src/bloom.c @@ -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; @@ -220,4 +220,4 @@ void bloom_filter_destroy(bloom_filter_t *self) { } free(self); -} \ No newline at end of file +} diff --git a/src/geodb.c b/src/geodb.c index 8b0dfecb..26c91da5 100644 --- a/src/geodb.c +++ b/src/geodb.c @@ -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; diff --git a/src/geodb_builder.c b/src/geodb_builder.c index 327ce812..489a02c4 100644 --- a/src/geodb_builder.c +++ b/src/geodb_builder.c @@ -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; diff --git a/src/graph.c b/src/graph.c index 2b85dfc6..7403e17a 100644 --- a/src/graph.c +++ b/src/graph.c @@ -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; diff --git a/src/language_classifier.c b/src/language_classifier.c index bd8fb0cd..9efa4ddc 100644 --- a/src/language_classifier.c +++ b/src/language_classifier.c @@ -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; } diff --git a/src/numex.c b/src/numex.c index 7c815080..f4bdf4d7 100644 --- a/src/numex.c +++ b/src/numex.c @@ -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; diff --git a/src/sparse_matrix.c b/src/sparse_matrix.c index dc9fa7fc..6f225a94 100644 --- a/src/sparse_matrix.c +++ b/src/sparse_matrix.c @@ -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; diff --git a/src/transliterate.c b/src/transliterate.c index 7a9dd4f5..644dd110 100644 --- a/src/transliterate.c +++ b/src/transliterate.c @@ -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) { diff --git a/src/trie.c b/src/trie.c index 0c0a0bc2..35b4254c 100644 --- a/src/trie.c +++ b/src/trie.c @@ -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;