replace strcpy with snprintf

This commit is contained in:
fengzero
2026-07-01 03:41:32 +00:00
parent 5ec90849d1
commit b3e0e4d623
9 changed files with 110 additions and 58 deletions
+1
View File
@@ -41,6 +41,7 @@ if(USE_GCOV)
endif(USE_GCOV)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=format-truncation")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O0")
add_compile_options(-fstack-protector-strong)
add_link_options(-fstack-protector-strong)
+1 -1
View File
@@ -93,7 +93,7 @@
#define NEU_CID_LEN4 4
#define NEU_CID_LEN8 8
#define NEU_CID_LEN16 16
#define NEU_CID_LEN32 32
#define NEU_CID_LEN32 72
#define NEU_CID_LEN64 64
#define NEU_TRACE_ID_LEN 64
+2 -1
View File
@@ -204,7 +204,8 @@ void recv_data_callback(void *arg)
body_str = nng_msg_body(msg);
body_len = nng_msg_len(msg);
if (*(uint8_t *) body_str == 0x0A && *(uint8_t *) (body_str + 1) == 0xCE) {
if (body_len >= 26 && *(uint8_t *) body_str == 0x0A &&
*(uint8_t *) (body_str + 1) == 0xCE) {
// trace
trace_id = (uint8_t *) (body_str + 2);
span_id = (uint8_t *) (body_str + 18);
+26 -8
View File
@@ -71,8 +71,8 @@ inline static tkey_t to_key(const char *group, const char *tag)
{
tkey_t key = { 0 };
strcpy(key.group, group);
strcpy(key.tag, tag);
strncpy(key.group, group, sizeof(key.group) - 1);
strncpy(key.tag, tag, sizeof(key.tag) - 1);
return key;
}
@@ -160,8 +160,8 @@ void neu_driver_cache_add(neu_driver_cache_t *cache, const char *group,
if (elem == NULL) {
elem = calloc(1, sizeof(struct elem));
strcpy(elem->key.group, group);
strcpy(elem->key.tag, tag);
snprintf(elem->key.group, sizeof(elem->key.group), "%s", group);
snprintf(elem->key.tag, sizeof(elem->key.tag), "%s", tag);
HASH_ADD(hh, cache->table, key, sizeof(tkey_t), elem);
}
@@ -179,7 +179,7 @@ void neu_driver_cache_update_trace(neu_driver_cache_t *cache, const char *group,
group_trace_t *elem = NULL;
char key[NEU_GROUP_NAME_LEN] = { 0 };
strcpy(key, group);
snprintf(key, sizeof(key), "%s", group);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->trace_table, &key, sizeof(key), elem);
@@ -187,7 +187,7 @@ void neu_driver_cache_update_trace(neu_driver_cache_t *cache, const char *group,
if (elem == NULL) {
elem = calloc(1, sizeof(group_trace_t));
strcpy(elem->key, group);
snprintf(elem->key, sizeof(elem->key), "%s", group);
elem->trace_ctx = trace_ctx;
HASH_ADD(hh, cache->trace_table, key, sizeof(key), elem);
@@ -205,7 +205,7 @@ void *neu_driver_cache_get_trace(neu_driver_cache_t *cache, const char *group)
void *trace = NULL;
strcpy(key, group);
snprintf(key, sizeof(key), "%s", group);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->trace_table, &key, sizeof(key), elem);
@@ -275,7 +275,7 @@ bool neu_driver_cache_update_change(neu_driver_cache_t *cache,
elem->changed = true;
tag_changed = true;
} else {
if (memcpy(elem->value_old.value.bytes.bytes,
if (memcmp(elem->value_old.value.bytes.bytes,
value.value.bytes.bytes,
value.value.bytes.length) != 0) {
elem->changed = true;
@@ -1266,3 +1266,21 @@ void neu_driver_cache_del(neu_driver_cache_t *cache, const char *group,
pthread_mutex_unlock(&cache->mtx);
}
void neu_driver_cache_rename(neu_driver_cache_t *cache, const char *group,
const char *old_tag, const char *new_tag)
{
struct elem *elem = NULL;
tkey_t key = to_key(group, old_tag);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->table, &key, sizeof(tkey_t), elem);
if (elem != NULL) {
HASH_DEL(cache->table, elem);
elem->key = to_key(group, new_tag);
HASH_ADD(hh, cache->table, key, sizeof(tkey_t), elem);
}
pthread_mutex_unlock(&cache->mtx);
}
+3 -3
View File
@@ -210,7 +210,7 @@ int neu_conn_eth_check_interface(const char *interface)
return -1;
}
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (ret != 0) {
return -1;
@@ -302,7 +302,7 @@ static int get_mac(neu_conn_eth_t *conn, const char *interface)
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
int ret = -1;
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(conn->ic->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
ret = 0;
@@ -328,7 +328,7 @@ static int init_socket(const char *interface, uint16_t protocol, uint8_t mac[6])
(void) mac;
(void) pn_mcast_addr;
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
if (ioctl(fd, SIOCGIFINDEX, &ifr) != 0) {
close(fd);
return -1;
+2 -1
View File
@@ -357,7 +357,8 @@ int neu_plugin_manager_find(neu_plugin_manager_t *mgr, const char *plugin_name,
info->kind = plugin->kind;
if (plugin->single_name != NULL) {
strcpy(info->single_name, plugin->single_name);
snprintf(info->single_name, sizeof(info->single_name), "%s",
plugin->single_name);
}
strncpy(info->name, plugin->name, sizeof(info->name));
strncpy(info->library, plugin->lib_name, sizeof(info->library));
+14 -9
View File
@@ -1033,29 +1033,32 @@ void neu_otel_split_traceparent(const char *in, char *trace_id, char *span_id,
char * copy = strdup(in);
char t_flags[32] = { 0 };
const size_t trace_id_cap = 64;
const size_t span_id_cap = 32;
token = strtok_r(copy, delimiter, &saveptr);
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(trace_id, token);
snprintf(trace_id, trace_id_cap, "%s", token);
} else {
strcpy(trace_id, "");
trace_id[0] = '\0';
}
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(span_id, token);
snprintf(span_id, span_id_cap, "%s", token);
} else {
strcpy(span_id, "");
span_id[0] = '\0';
}
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(t_flags, token);
snprintf(t_flags, sizeof(t_flags), "%s", token);
sscanf(t_flags, "%x", flags);
} else {
strcpy(t_flags, "");
*flags = 0;
t_flags[0] = '\0';
*flags = 0;
}
free(copy);
@@ -1183,10 +1186,12 @@ void neu_otel_set_config(void *config)
otel_control_flag = req->control_flag;
otel_data_flag = req->data_flag;
if (req->collector_url) {
strcpy(otel_collector_url, req->collector_url);
snprintf(otel_collector_url, sizeof(otel_collector_url), "%s",
req->collector_url);
}
if (req->service_name) {
strcpy(otel_service_name, req->service_name);
snprintf(otel_service_name, sizeof(otel_service_name), "%s",
req->service_name);
}
otel_data_sample_rate = req->data_sample_rate;
+59 -34
View File
@@ -293,7 +293,8 @@ static int find_da_basic_type(cid_template_t *template, const char *datype_id,
template, template->datypes[i].bdas[j].ref_type, name,
da_types + index);
} else {
strcpy(da_types[index].all_name, name);
snprintf(da_types[index].all_name,
sizeof(da_types[index].all_name), "%s", name);
da_types[index].btype = template->datypes[i].bdas[j].btype;
index += 1;
}
@@ -457,7 +458,8 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da->btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da->name);
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s",
da->name);
}
}
@@ -475,7 +477,8 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da_types[j].btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da_types[j].all_name);
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s",
da_types[j].all_name);
}
}
@@ -488,7 +491,7 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da_types[0].btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da->name);
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s", da->name);
ctl->n_co_types = n_da_types;
for (int j = 0; j < n_da_types; j++) {
ctl->co_types[j] = da_types[j].btype;
@@ -522,7 +525,7 @@ static int parse_ied(xmlNode *xml_ied, cid_ied_t *ied)
nlog_warn("IED name is too long");
return -1;
} else {
strcpy(ied->name, ied_name);
snprintf(ied->name, sizeof(ied->name), "%s", ied_name);
xmlFree(ied_name);
}
xmlNode *access_point = xml_ied->children;
@@ -540,7 +543,7 @@ static int parse_ied(xmlNode *xml_ied, cid_ied_t *ied)
memset(ap, 0, sizeof(cid_access_point_t));
if (name != NULL) {
strcpy(ap->name, name);
snprintf(ap->name, sizeof(ap->name), "%s", name);
xmlFree(name);
}
@@ -581,7 +584,7 @@ static int parse_ldevice(xmlNode *xml_server, cid_access_point_t *ap)
memset(ldev, 0, sizeof(cid_ldevice_t));
char *inst = (char *) xmlGetProp(ldevice, (const xmlChar *) "inst");
if (inst != NULL) {
strcpy(ldev->inst, inst);
snprintf(ldev->inst, sizeof(ldev->inst), "%s", inst);
xmlFree(inst);
}
int ret = parse_lnode(ldevice, ldev);
@@ -903,8 +906,9 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->lnotypes[template->n_lnotypes - 1];
memset(tm_lno, 0, sizeof(cid_tm_lno_type_t));
strcpy(tm_lno->id, id);
strcpy(tm_lno->ln_class, ln_class);
snprintf(tm_lno->id, sizeof(tm_lno->id), "%s", id);
snprintf(tm_lno->ln_class, sizeof(tm_lno->ln_class), "%s",
ln_class);
xmlNode *xdo = child->children;
while (xdo != NULL) {
@@ -926,8 +930,9 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_lno->dos[tm_lno->n_dos - 1];
memset(tm_do, 0, sizeof(cid_tm_lno_do_t));
strcpy(tm_do->name, name);
strcpy(tm_do->ref_type, ref_type);
snprintf(tm_do->name, sizeof(tm_do->name), "%s", name);
snprintf(tm_do->ref_type, sizeof(tm_do->ref_type), "%s",
ref_type);
}
if (name != NULL) {
@@ -960,7 +965,7 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->dotypes[template->n_dotypes - 1];
memset(tm_do, 0, sizeof(cid_tm_do_type_t));
strcpy(tm_do->id, id);
snprintf(tm_do->id, sizeof(tm_do->id), "%s", id);
xmlNode *da = child->children;
while (da != NULL) {
if (da->type != XML_ELEMENT_NODE) {
@@ -982,8 +987,10 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_do->sdos[tm_do->n_sdos - 1];
memset(tm_sdo, 0, sizeof(cid_tm_sdo_t));
strcpy(tm_sdo->name, sdo_name);
strcpy(tm_sdo->ref_type, sdo_ref_type);
snprintf(tm_sdo->name, sizeof(tm_sdo->name), "%s",
sdo_name);
snprintf(tm_sdo->ref_type, sizeof(tm_sdo->ref_type),
"%s", sdo_ref_type);
}
if (sdo_name != NULL) {
@@ -1015,9 +1022,12 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_do->das[tm_do->n_das - 1];
memset(tm_da, 0, sizeof(cid_tm_do_da_t));
strcpy(tm_da->name, name);
snprintf(tm_da->name, sizeof(tm_da->name), "%s",
name);
if (ref_type != NULL) {
strcpy(tm_da->ref_type, ref_type);
snprintf(tm_da->ref_type,
sizeof(tm_da->ref_type), "%s",
ref_type);
}
tm_da->btype = T_UNKNOWN;
tm_da->btype =
@@ -1079,7 +1089,7 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->datypes[template->n_datypes - 1];
memset(tm_dat, 0, sizeof(cid_tm_da_type_t));
strcpy(tm_dat->id, id);
snprintf(tm_dat->id, sizeof(tm_dat->id), "%s", id);
xmlNode *bda = child->children;
while (bda != NULL) {
if (bda->type != XML_ELEMENT_NODE) {
@@ -1101,9 +1111,11 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
cid_tm_bda_type_t *tm_bda =
&tm_dat->bdas[tm_dat->n_bdas - 1];
memset(tm_bda, 0, sizeof(cid_tm_bda_type_t));
strcpy(tm_bda->name, name);
snprintf(tm_bda->name, sizeof(tm_bda->name), "%s",
name);
if (ref_type != NULL) {
strcpy(tm_bda->ref_type, ref_type);
snprintf(tm_bda->ref_type, sizeof(tm_bda->ref_type),
"%s", ref_type);
}
tm_bda->btype = T_UNKNOWN;
tm_bda->btype = decode_basictype((const char *) btype);
@@ -1239,12 +1251,18 @@ cid_dataset_info_t *neu_cid_info_from_string(const char *str)
cid_dataset_info_t *info = calloc(1, sizeof(cid_dataset_info_t));
info->control = elems[0].v.val_bool;
info->buffered = elems[1].v.val_bool;
strcpy(info->ied_name, elems[2].v.val_str);
strcpy(info->ldevice_inst, elems[3].v.val_str);
strcpy(info->ln_class, elems[4].v.val_str);
strcpy(info->report_name, elems[5].v.val_str);
strcpy(info->report_id, elems[6].v.val_str);
strcpy(info->dataset_name, elems[7].v.val_str);
snprintf(info->ied_name, sizeof(info->ied_name), "%s",
elems[2].v.val_str);
snprintf(info->ldevice_inst, sizeof(info->ldevice_inst), "%s",
elems[3].v.val_str);
snprintf(info->ln_class, sizeof(info->ln_class), "%s",
elems[4].v.val_str);
snprintf(info->report_name, sizeof(info->report_name), "%s",
elems[5].v.val_str);
snprintf(info->report_id, sizeof(info->report_id), "%s",
elems[6].v.val_str);
snprintf(info->dataset_name, sizeof(info->dataset_name), "%s",
elems[7].v.val_str);
free(elems[2].v.val_str);
free(elems[3].v.val_str);
@@ -1283,8 +1301,9 @@ static void ld_ctrl_do_to_group(const char *ied_name, const char *ap_name,
cid_dataset_info_t *g_info = calloc(1, sizeof(cid_dataset_info_t));
g_info->control = true;
strcpy(g_info->ied_name, ied_name);
strcpy(g_info->ldevice_inst, ld->inst);
snprintf(g_info->ied_name, sizeof(g_info->ied_name), "%s", ied_name);
snprintf(g_info->ldevice_inst, sizeof(g_info->ldevice_inst), "%s",
ld->inst);
group->context = g_info;
snprintf(group->group, NEU_GROUP_NAME_LEN, "%s/%s$Control", ap_name,
@@ -1447,13 +1466,19 @@ static void ln_dataset_to_group(const char *ied_name, const char *ap_name,
calloc(1, sizeof(cid_dataset_info_t));
g_info->control = false;
strcpy(g_info->ied_name, ied_name);
strcpy(g_info->ldevice_inst, ld_inst);
strcpy(g_info->ln_class, ln->lnclass);
snprintf(g_info->ied_name, sizeof(g_info->ied_name), "%s",
ied_name);
snprintf(g_info->ldevice_inst, sizeof(g_info->ldevice_inst),
"%s", ld_inst);
snprintf(g_info->ln_class, sizeof(g_info->ln_class), "%s",
ln->lnclass);
g_info->buffered = ln->reports[i].buffered;
strcpy(g_info->report_name, ln->reports[i].name);
strcpy(g_info->report_id, ln->reports[i].id);
strcpy(g_info->dataset_name, ln->datasets[j].name);
snprintf(g_info->report_name, sizeof(g_info->report_name), "%s",
ln->reports[i].name);
snprintf(g_info->report_id, sizeof(g_info->report_id), "%s",
ln->reports[i].id);
snprintf(g_info->dataset_name, sizeof(g_info->dataset_name),
"%s", ln->datasets[j].name);
group->context = g_info;
@@ -1606,7 +1631,7 @@ static void ln_dataset_to_group(const char *ied_name, const char *ap_name,
void neu_cid_to_msg(char *driver, cid_t *cid, neu_req_add_gtag_t *cmd)
{
strcpy(cmd->driver, driver);
snprintf(cmd->driver, sizeof(cmd->driver), "%s", driver);
cmd->n_group = 0;
cmd->groups = NULL;
+2 -1
View File
@@ -440,7 +440,8 @@ int neu_http_post_otel_trace(uint8_t *data, int len)
char url_buf[128] = { 0 };
sprintf(url_buf, "http://%s/v1/traces", neu_otel_collector_url());
snprintf(url_buf, sizeof(url_buf), "http://%s/v1/traces",
neu_otel_collector_url());
if (nng_url_parse(&url, url_buf) != 0) {
nng_aio_free(aio);