summaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorK.Takata <kentkt@csc.jp>2019-07-29 12:38:05 (GMT)
committer卜部昌平 <shyouhei@ruby-lang.org>2019-10-21 02:14:36 (GMT)
commite70e81b54e10f1882874884564454f566c41b0dd (patch)
tree107ffcd549884467472061a775d609902ab73fd1 /st.c
parent263ee6639d6f9f599613a20731c0646d125e0813 (diff)
st: Add NULL checking
These are found by Coverity.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2304
Diffstat (limited to 'st.c')
-rw-r--r--st.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/st.c b/st.c
index 924a15d..5d3588f 100644
--- a/st.c
+++ b/st.c
@@ -561,6 +561,8 @@ stat_col(void)
FILE *f;
if (!collision.total) return;
f = fopen((snprintf(fname, sizeof(fname), "/tmp/col%ld", (long)getpid()), fname), "w");
+ if (f == NULL)
+ return;
fprintf(f, "collision: %d / %d (%6.2f)\n", collision.all, collision.total,
((double)collision.all / (collision.total)) * 100);
fprintf(f, "num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
@@ -592,16 +594,27 @@ st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
n = get_power2(size);
tab = (st_table *) malloc(sizeof (st_table));
+ if (tab == NULL)
+ return NULL;
tab->type = type;
tab->entry_power = n;
tab->bin_power = features[n].bin_power;
tab->size_ind = features[n].size_ind;
if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
tab->bins = NULL;
- else
+ else {
tab->bins = (st_index_t *) malloc(bins_size(tab));
+ if (tab->bins == NULL) {
+ free(tab);
+ return NULL;
+ }
+ }
tab->entries = (st_table_entry *) malloc(get_allocated_entries(tab)
* sizeof(st_table_entry));
+ if (tab->entries == NULL) {
+ st_free_table(tab);
+ return NULL;
+ }
#ifdef ST_DEBUG
memset(tab->entries, ST_INIT_VAL_BYTE,
get_allocated_entries(tab) * sizeof(st_table_entry));
@@ -1299,13 +1312,24 @@ st_copy(st_table *old_tab)
st_table *new_tab;
new_tab = (st_table *) malloc(sizeof(st_table));
+ if (new_tab == NULL)
+ return NULL;
*new_tab = *old_tab;
if (old_tab->bins == NULL)
new_tab->bins = NULL;
- else
+ else {
new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
+ if (new_tab->bins == NULL) {
+ free(new_tab);
+ return NULL;
+ }
+ }
new_tab->entries = (st_table_entry *) malloc(get_allocated_entries(old_tab)
* sizeof(st_table_entry));
+ if (new_tab->entries == NULL) {
+ st_free_table(new_tab);
+ return NULL;
+ }
MEMCPY(new_tab->entries, old_tab->entries, st_table_entry,
get_allocated_entries(old_tab));
if (old_tab->bins != NULL)