From: Mika Laitio Date: Mon, 7 Sep 2015 07:22:26 +0000 (+0300) Subject: fix more errors from uninitalized variables X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;ds=sidebyside;p=libplp.git fix more errors from uninitalized variables Signed-off-by: Mika Laitio --- diff --git a/src/StoreCache.cc b/src/StoreCache.cc index fe5c583..a289f54 100644 --- a/src/StoreCache.cc +++ b/src/StoreCache.cc @@ -156,8 +156,9 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) { res_data = NULL; cnt = 0; val_cnt = 0; - dr = NULL; while(cur_date->before(max_date)) { + store = NULL; + dr = NULL; if (period_type_param == PERIOD_YEARLY) { store = new StoreCache(device_id, cur_date); dr = store->get_mean(PERIOD_MONTHLY); @@ -192,7 +193,8 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) { } delete(dr); } - delete(store); + if (store != NULL) + delete(store); if (period_type_param == PERIOD_YEARLY) { cur_date->next_month(); } @@ -218,7 +220,7 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) { case PERIOD_MINUTELY: case PERIOD_SECONDLY: if (StoreDay::exist(device_id, date, false)) { - StoreDay *store; + StoreDay *store; store = new StoreDay(device_id, date); ret_val = store->get_mean(period_type_param); @@ -541,6 +543,8 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) { res_data = NULL; cnt = 0; while(cur_date->before(max_date)) { + store = NULL; + dr = NULL; if (period_type_param == PERIOD_YEARLY) { store = new StoreCache(device_id, cur_date); dr = store->get_max(PERIOD_MONTHLY); @@ -584,7 +588,8 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) { } delete(dr); } - delete(store); + if (store != NULL) + delete(store); if (period_type_param == PERIOD_YEARLY) { cur_date->next_month(); } @@ -667,6 +672,8 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) { res_data = NULL; cnt = 0; while(cur_date->before(max_date)) { + store = NULL; + dr = NULL; if (period_type_param == PERIOD_YEARLY) { store = new StoreCache(device_id, cur_date); dr = store->get_min(PERIOD_MONTHLY); @@ -710,7 +717,8 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) { } delete(dr); } - delete(store); + if (store != NULL) + delete(store); if (period_type_param == PERIOD_YEARLY) { cur_date->next_month(); } diff --git a/src_test/test_config1.c b/src_test/test_config1.c index 8bddb50..e520dc3 100644 --- a/src_test/test_config1.c +++ b/src_test/test_config1.c @@ -29,60 +29,63 @@ int test_config() { char work_dir[FILENAME_MAX]; char *val; + char *ret; - getcwd(work_dir, sizeof(work_dir)); - printf("working directory: %s\n", work_dir); + ret = getcwd(work_dir, sizeof(work_dir)); + if (ret != NULL) { + printf("working directory: %s\n", work_dir); - set_config_value_and_save(work_dir, - CONF_FILENAME, - SECTION_TYPE, - SECTION_NAME, - KEY_NAME, - VALUE1); - val = get_config_value_and_close(work_dir, - CONF_FILENAME, - SECTION_NAME, - KEY_NAME); - if ((val != NULL) && - (strcmp(val, VALUE1) == 0)) { - printf("value 1 read ok: %s\n", val); - free(val); - } - else { - printf("failed to read value\n"); - return 1; - } - - val = get_config_value_and_close(work_dir, + set_config_value_and_save(work_dir, + CONF_FILENAME, + SECTION_TYPE, + SECTION_NAME, + KEY_NAME, + VALUE1); + val = get_config_value_and_close(work_dir, CONF_FILENAME, SECTION_NAME, - KEY_NAME_INVALID_READ); - if ((val != NULL) && - (strcmp(val, VALUE1) == 0)) { - printf("pl, should not be possible to read invalid key value.\n"); - return 0; - } - else { - printf("error, value was supposed to be NULL, but is: %s\n", val); + KEY_NAME); + if ((val != NULL) && + (strcmp(val, VALUE1) == 0)) { + printf("value 1 read ok: %s\n", val); + free(val); + } + else { + printf("failed to read value\n"); + return 1; + } + + val = get_config_value_and_close(work_dir, + CONF_FILENAME, + SECTION_NAME, + KEY_NAME_INVALID_READ); + if ((val != NULL) && + (strcmp(val, VALUE1) == 0)) { + printf("pl, should not be possible to read invalid key value.\n"); + return 0; + } + else { + printf("error, value was supposed to be NULL, but is: %s\n", val); + } + set_config_value_and_save(work_dir, + CONF_FILENAME, + SECTION_TYPE, + SECTION_NAME, + KEY_NAME, + VALUE2); + set_config_value_and_save(work_dir, + CONF_FILENAME, + SECTION_TYPE, + SECTION_NAME, + KEY_NAME2, + VALUE3); + set_config_value_and_save(work_dir, + CONF_FILENAME, + SECTION_TYPE2, + SECTION_NAME2, + KEY_NAME3, + VALUE4); } - set_config_value_and_save(work_dir, - CONF_FILENAME, - SECTION_TYPE, - SECTION_NAME, - KEY_NAME, - VALUE2); - set_config_value_and_save(work_dir, - CONF_FILENAME, - SECTION_TYPE, - SECTION_NAME, - KEY_NAME2, - VALUE3); - set_config_value_and_save(work_dir, - CONF_FILENAME, - SECTION_TYPE2, - SECTION_NAME2, - KEY_NAME3, - VALUE4); return 0; }