Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 7049f38

Browse files
authored
Merge pull request #698 from HowJMay/coverity
fix(coverity): Fix security issues coverity found
2 parents b5287d7 + 48a1957 commit 7049f38

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

accelerator/config.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "yaml.h"
1717

1818
#define CONFIG_LOGGER "config"
19-
#define CA_PEM_LEN 1208
2019
#define DEFAULT_CA_PEM \
2120
"-----BEGIN CERTIFICATE-----\r\n" \
2221
"MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\r\n" \
@@ -62,6 +61,42 @@ static int get_conf_key(char const* const key) {
6261
return 0;
6362
}
6463

64+
#define CA_BUFFER_SIZE 2048
65+
static char* read_ca_pem(char* ca_pem_path) {
66+
FILE* file = NULL;
67+
if ((file = fopen(ca_pem_path, "r")) == NULL) {
68+
ta_log_error("%s\n", ta_error_to_string(SC_CONF_FOPEN_ERROR));
69+
goto done;
70+
}
71+
72+
char* ca_pem = (char*)malloc(sizeof(char) * (CA_BUFFER_SIZE));
73+
if (!ca_pem) {
74+
ta_log_error("%s\n", ta_error_to_string(SC_OOM));
75+
goto done;
76+
}
77+
78+
char c;
79+
int i = 0, buffer_nums = 1;
80+
while ((c = fgetc(file)) != EOF) {
81+
ca_pem[i++] = c;
82+
if (!(i < CA_BUFFER_SIZE)) {
83+
ca_pem = realloc(ca_pem, sizeof(char) * buffer_nums * CA_BUFFER_SIZE);
84+
buffer_nums++;
85+
}
86+
}
87+
ca_pem[i] = '\0';
88+
89+
if (feof(file)) {
90+
ta_log_debug("Read the End Of File.\n");
91+
} else {
92+
ta_log_error("Read file error\n");
93+
}
94+
95+
done:
96+
fclose(file);
97+
return ca_pem;
98+
}
99+
65100
status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
66101
if (value == NULL && (key != CACHE && key != PROXY_API && key != QUIET && key != NO_GTTA)) {
67102
ta_log_error("%s\n", "SC_NULL");
@@ -71,7 +106,6 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
71106
iota_config_t* const iota_conf = &core->iota_conf;
72107
ta_cache_t* const cache = &core->cache;
73108
iota_client_service_t* const iota_service = &core->iota_service;
74-
FILE* file = NULL;
75109
char* conf_file = core->conf_file;
76110
#ifdef DB_ENABLE
77111
db_client_service_t* const db_service = &core->db_service;
@@ -115,7 +149,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
115149
for (char* p = strtok(value, ","); p && idx < MAX_NODE_LIST_ELEMENTS; p = strtok(NULL, ","), idx++) {
116150
ta_conf->iota_host_list[idx] = p;
117151
}
118-
strncpy(iota_service->http.host, ta_conf->iota_host_list[0], HOST_MAX_LEN);
152+
strncpy(iota_service->http.host, ta_conf->iota_host_list[0], HOST_MAX_LEN - 1);
119153
break;
120154
case NODE_PORT_CLI:
121155
idx = 0;
@@ -131,15 +165,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
131165
break;
132166

133167
case CA_PEM:
134-
if ((file = fopen(value, "r")) == NULL) {
135-
/* The specified configuration file does not exist */
136-
ta_log_error("%s\n", ta_error_to_string(SC_CONF_FOPEN_ERROR));
137-
return SC_CONF_FOPEN_ERROR;
138-
}
139-
char* temp_ca_pem = (char*)malloc(sizeof(char) * (CA_PEM_LEN + 1));
140-
fread(temp_ca_pem, CA_PEM_LEN, 1, file);
141-
iota_service->http.ca_pem = temp_ca_pem;
142-
fclose(file);
168+
iota_service->http.ca_pem = read_ca_pem(value);
143169
break;
144170

145171
case HEALTH_TRACK_PERIOD:
@@ -257,12 +283,12 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
257283
status_t ta_set_iota_client_service(iota_client_service_t* service, char const* host, uint16_t port,
258284
char const* const ca_pem) {
259285
strncpy(service->http.path, "/", CONTENT_TYPE_MAX_LEN);
260-
strncpy(service->http.content_type, "application/json", CONTENT_TYPE_MAX_LEN);
261-
strncpy(service->http.accept, "application/json", CONTENT_TYPE_MAX_LEN);
262-
strncpy(service->http.host, host, HOST_MAX_LEN);
286+
strncpy(service->http.content_type, "application/json", CONTENT_TYPE_MAX_LEN - 1);
287+
strncpy(service->http.accept, "application/json", CONTENT_TYPE_MAX_LEN - 1);
288+
strncpy(service->http.host, host, HOST_MAX_LEN - 1);
263289
service->http.port = port;
264290
service->http.api_version = 1;
265-
service->http.ca_pem = ca_pem ? ca_pem : DEFAULT_CA_PEM;
291+
service->http.ca_pem = ca_pem;
266292
service->serializer_type = SR_JSON;
267293
init_json_serializer(&service->serializer);
268294

accelerator/core/serializer/ser_mam.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
158158
if (json_value == NULL) {
159159
ret = SC_CCLIENT_JSON_KEY;
160160
ta_log_error("%s\n", ta_error_to_string(ret));
161-
return ret;
161+
goto done;
162162
}
163163
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL) &&
164164
(strlen(json_value->valuestring) == NUM_TRYTES_HASH)) {
@@ -167,7 +167,7 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
167167
} else {
168168
ret = SC_CCLIENT_JSON_PARSE;
169169
ta_log_error("%s\n", ta_error_to_string(ret));
170-
return ret;
170+
goto done;
171171
}
172172
goto set_data_id;
173173
}
@@ -177,7 +177,7 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
177177
if (json_value == NULL) {
178178
ret = SC_CCLIENT_JSON_KEY;
179179
ta_log_error("%s\n", ta_error_to_string(ret));
180-
return ret;
180+
goto done;
181181
}
182182
if (cJSON_IsString(json_value) &&
183183
(json_value->valuestring != NULL && (strlen(json_value->valuestring) == NUM_TRYTES_HASH))) {
@@ -186,7 +186,7 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
186186
} else {
187187
ret = SC_CCLIENT_JSON_PARSE;
188188
ta_log_error("%s\n", ta_error_to_string(ret));
189-
return ret;
189+
goto done;
190190
}
191191
}
192192

@@ -204,7 +204,7 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
204204
} else {
205205
ret = SC_CCLIENT_JSON_PARSE;
206206
ta_log_error("%s\n", ta_error_to_string(ret));
207-
return ret;
207+
goto done;
208208
}
209209
}
210210

common/debug.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void dump_bundle(bundle_transactions_t *bundle) {
3535
void dump_transaction_obj(iota_transaction_t *tx_obj) {
3636
field_mask_t old_mask = {};
3737
memcpy(&old_mask, &tx_obj->loaded_columns_mask, sizeof(field_mask_t));
38-
memset(&tx_obj->loaded_columns_mask, 0xFFFFF, sizeof(field_mask_t));
38+
memset(&tx_obj->loaded_columns_mask, 0xFF, sizeof(field_mask_t));
3939

4040
ta_log_debug("==========Transaction Object==========\n");
4141
// address
@@ -92,9 +92,9 @@ bool transaction_cmp(iota_transaction_t *tx1, iota_transaction_t *tx2) {
9292
bool equal = true;
9393
field_mask_t old_mask1 = {}, old_mask2 = {};
9494
memcpy(&old_mask1, &tx1->loaded_columns_mask, sizeof(field_mask_t));
95-
memset(&tx1->loaded_columns_mask, 0xFFFFF, sizeof(field_mask_t));
95+
memset(&tx1->loaded_columns_mask, 0xFF, sizeof(field_mask_t));
9696
memcpy(&old_mask2, &tx2->loaded_columns_mask, sizeof(field_mask_t));
97-
memset(&tx2->loaded_columns_mask, 0xFFFFF, sizeof(field_mask_t));
97+
memset(&tx2->loaded_columns_mask, 0xFF, sizeof(field_mask_t));
9898
uint64_t result = 0;
9999

100100
// address

utils/cache/backend_redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ bool cache_init(pthread_rwlock_t** rwlock, bool input_state, const char* host, i
268268
cache.conn = (connection_private*)malloc(sizeof(connection_private));
269269
CONN(cache)->rc = redisConnect(host, port);
270270
if (!CONN(cache)->rc || CONN(cache)->rc->err) {
271-
ta_log_error("Failed to initialize redis: %s\n", CONN(cache)->rc->err);
271+
ta_log_error("Failed to initialize redis: %d\n", CONN(cache)->rc->err);
272272
return false;
273273
}
274274

utils/cpuinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static inline int get_nthds_per_phys_proc() {
5656
if (fgets(nthd, sizeof(nthd), fd) == NULL) return -1;
5757
nthread = (int)strtol(nthd, NULL, 10);
5858
if (errno == ERANGE || nthread == 0) {
59+
pclose(fd);
5960
return -1;
6061
}
6162

0 commit comments

Comments
 (0)