Skip to content

Commit 271b0dd

Browse files
authored
Merge pull request #2766 from fengzeroz/main
fix(mqtt): replace strcpy with strncpy
2 parents 2fa7186 + 0114160 commit 271b0dd

2 files changed

Lines changed: 43 additions & 31 deletions

File tree

‎.github/workflows/check.yml‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
steps:
1515
# checkout
16-
- uses: actions/checkout@v4
16+
- uses: actions/checkout@v6
1717
with:
1818
submodules: recursive
1919

@@ -40,6 +40,18 @@ jobs:
4040
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=unusedFunction --suppress=*:src/otel/trace.pb-c.c --suppress=*:src/otel/common.pb-c.c --suppress=*:src/otel/resource.pb-c.c ./plugins ./src
4141
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=variableScope ./simulator
4242
43+
flawfinder:
44+
name: Flawfinder Scan
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Checkout code
48+
uses: actions/checkout@v6
49+
50+
- name: flawfinder_scan
51+
uses: david-a-wheeler/flawfinder@2.0.20
52+
with:
53+
arguments: '--minlevel=3 .'
54+
4355
build:
4456
runs-on: ubuntu-24.04
4557
container: ghcr.io/neugates/build:x86_64-main

‎plugins/mqtt/mqtt_handle.c‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void to_traceparent(uint8_t *trace_id, char *span_id, char *out)
3939
size += sprintf(out + size, "%02x", trace_id[i]);
4040
}
4141

42-
sprintf(out + size, "-%s-01", span_id);
42+
snprintf(out + size, strlen(span_id) + 5, "-%s-01", span_id);
4343
}
4444

4545
static int tag_values_to_json(UT_array *tags, mqtt_static_vt_t *s_tags,
@@ -281,7 +281,7 @@ static inline int send_driver_action(neu_plugin_t * plugin,
281281
header.ctx = NULL;
282282

283283
neu_req_driver_action_t action = { 0 };
284-
strncpy(action.driver, req->driver, NEU_NODE_NAME_LEN);
284+
strncpy(action.driver, req->driver, NEU_NODE_NAME_LEN - 1);
285285
action.action = strdup(req->action);
286286

287287
if (0 != neu_plugin_op(plugin, header, &action)) {
@@ -465,7 +465,7 @@ static int send_write_tags_req(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt,
465465
}
466466

467467
for (int i = 0; i < cmd.n_tag; i++) {
468-
strcpy(cmd.tags[i].tag, req->tags[i].tag);
468+
strncpy(cmd.tags[i].tag, req->tags[i].tag, NEU_TAG_NAME_LEN - 1);
469469
if (0 !=
470470
json_value_to_tag_value(&req->tags[i].value, req->tags[i].t,
471471
&cmd.tags[i].value)) {
@@ -618,7 +618,7 @@ void handle_write_req(neu_mqtt_qos_e qos, const char *topic,
618618
}
619619

620620
for (int i = 0; i < cmd.n_tag; i++) {
621-
strcpy(cmd.tags[i].tag, wr->tags[i]->name);
621+
strncpy(cmd.tags[i].tag, wr->tags[i]->name, NEU_TAG_NAME_LEN - 1);
622622
switch (wr->tags[i]->value->value_case) {
623623
case MODEL__DATA_ITEM_VALUE__VALUE_INT_VALUE:
624624
cmd.tags[i].value.type = NEU_TYPE_INT64;
@@ -635,8 +635,8 @@ void handle_write_req(neu_mqtt_qos_e qos, const char *topic,
635635
break;
636636
case MODEL__DATA_ITEM_VALUE__VALUE_STRING_VALUE:
637637
cmd.tags[i].value.type = NEU_TYPE_STRING;
638-
strcpy(cmd.tags[i].value.value.str,
639-
wr->tags[i]->value->string_value);
638+
strncpy(cmd.tags[i].value.value.str,
639+
wr->tags[i]->value->string_value, NEU_VALUE_SIZE - 1);
640640
break;
641641
default:
642642
break;
@@ -932,7 +932,7 @@ void handle_driver_action_req(neu_mqtt_qos_e qos, const char *topic,
932932
mqtt->tracestate = NULL;
933933

934934
header.ctx = mqtt;
935-
strcpy(cmd.driver, dar->node);
935+
strncpy(cmd.driver, dar->node, NEU_NODE_NAME_LEN - 1);
936936
cmd.action = strdup(dar->action);
937937

938938
model__driver_action_request__free_unpacked(dar, NULL);
@@ -955,7 +955,7 @@ void handle_driver_action_req(neu_mqtt_qos_e qos, const char *topic,
955955
}
956956

957957
header.ctx = mqtt;
958-
strcpy(cmd.driver, req->driver);
958+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
959959
cmd.action = strdup(req->action);
960960
neu_json_decode_driver_action_req_free(req);
961961
free(json_str);
@@ -1793,8 +1793,8 @@ void handle_driver_directory_req(neu_mqtt_qos_e qos, const char *topic,
17931793
mqtt->tracestate = NULL;
17941794

17951795
header.ctx = mqtt;
1796-
strcpy(cmd.driver, flr->node);
1797-
strcpy(cmd.path, flr->path);
1796+
strncpy(cmd.driver, flr->node, NEU_NODE_NAME_LEN - 1);
1797+
strncpy(cmd.path, flr->path, NEU_PATH_LEN - 1);
17981798

17991799
model__file_list_request__free_unpacked(flr, NULL);
18001800
} else {
@@ -1817,8 +1817,8 @@ void handle_driver_directory_req(neu_mqtt_qos_e qos, const char *topic,
18171817

18181818
header.ctx = mqtt;
18191819

1820-
strcpy(cmd.driver, req->driver);
1821-
strcpy(cmd.path, req->path);
1820+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
1821+
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
18221822

18231823
neu_json_decode_driver_directory_req_free(req);
18241824
free(json_str);
@@ -1982,8 +1982,8 @@ void handle_driver_fup_open_req(neu_mqtt_qos_e qos, const char *topic,
19821982
mqtt->tracestate = NULL;
19831983

19841984
header.ctx = mqtt;
1985-
strcpy(cmd.driver, fur->node);
1986-
strcpy(cmd.path, fur->path);
1985+
strncpy(cmd.driver, fur->node, NEU_NODE_NAME_LEN - 1);
1986+
strncpy(cmd.path, fur->path, NEU_PATH_LEN - 1);
19871987

19881988
model__file_upload_request__free_unpacked(fur, NULL);
19891989
} else {
@@ -2004,8 +2004,8 @@ void handle_driver_fup_open_req(neu_mqtt_qos_e qos, const char *topic,
20042004
return;
20052005
}
20062006
header.ctx = mqtt;
2007-
strcpy(cmd.driver, req->driver);
2008-
strcpy(cmd.path, req->path);
2007+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
2008+
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
20092009

20102010
neu_json_decode_driver_fup_open_req_free(req);
20112011
free(json_str);
@@ -2109,8 +2109,8 @@ void handle_driver_fup_data_req(neu_mqtt_qos_e qos, const char *topic,
21092109

21102110
header.ctx = mqtt;
21112111

2112-
strcpy(cmd.driver, fudr->node);
2113-
strcpy(cmd.path, fudr->path);
2112+
strncpy(cmd.driver, fudr->node, NEU_NODE_NAME_LEN - 1);
2113+
strncpy(cmd.path, fudr->path, NEU_PATH_LEN - 1);
21142114

21152115
model__file_upload_data_request__free_unpacked(fudr, NULL);
21162116
} else {
@@ -2133,8 +2133,8 @@ void handle_driver_fup_data_req(neu_mqtt_qos_e qos, const char *topic,
21332133

21342134
header.ctx = mqtt;
21352135

2136-
strcpy(cmd.driver, req->driver);
2137-
strcpy(cmd.path, req->path);
2136+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
2137+
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
21382138

21392139
neu_json_decode_driver_fup_data_req_free(req);
21402140
free(json_str);
@@ -2243,9 +2243,9 @@ void handle_driver_fdown_open_req(neu_mqtt_qos_e qos, const char *topic,
22432243
mqtt->tracestate = NULL;
22442244

22452245
header.ctx = mqtt;
2246-
strcpy(cmd.driver, fdr->node);
2247-
strcpy(cmd.src_path, fdr->src_path);
2248-
strcpy(cmd.dst_path, fdr->dst_path);
2246+
strncpy(cmd.driver, fdr->node, NEU_NODE_NAME_LEN - 1);
2247+
strncpy(cmd.src_path, fdr->src_path, NEU_PATH_LEN - 1);
2248+
strncpy(cmd.dst_path, fdr->dst_path, NEU_PATH_LEN - 1);
22492249
cmd.size = fdr->size;
22502250

22512251
model__file_download_request__free_unpacked(fdr, NULL);
@@ -2267,9 +2267,9 @@ void handle_driver_fdown_open_req(neu_mqtt_qos_e qos, const char *topic,
22672267
return;
22682268
}
22692269
header.ctx = mqtt;
2270-
strcpy(cmd.driver, req->driver);
2271-
strcpy(cmd.src_path, req->src_path);
2272-
strcpy(cmd.dst_path, req->dst_path);
2270+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
2271+
strncpy(cmd.src_path, req->src_path, NEU_PATH_LEN - 1);
2272+
strncpy(cmd.dst_path, req->dst_path, NEU_PATH_LEN - 1);
22732273
cmd.size = req->size;
22742274

22752275
neu_json_decode_driver_fdown_open_req_free(req);
@@ -2371,8 +2371,8 @@ void handle_driver_fdown_data_req(neu_mqtt_qos_e qos, const char *topic,
23712371
mqtt->tracestate = NULL;
23722372

23732373
header.ctx = mqtt;
2374-
strcpy(cmd.driver, fddr->node);
2375-
strcpy(cmd.src_path, fddr->path);
2374+
strncpy(cmd.driver, fddr->node, NEU_NODE_NAME_LEN - 1);
2375+
strncpy(cmd.src_path, fddr->path, NEU_PATH_LEN - 1);
23762376
cmd.more = fddr->more;
23772377
cmd.len = fddr->data.len;
23782378
cmd.data = calloc(fddr->data.len, sizeof(uint8_t));
@@ -2398,8 +2398,8 @@ void handle_driver_fdown_data_req(neu_mqtt_qos_e qos, const char *topic,
23982398
}
23992399
header.ctx = mqtt;
24002400

2401-
strcpy(cmd.driver, req->driver);
2402-
strcpy(cmd.src_path, req->src_path);
2401+
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
2402+
strncpy(cmd.src_path, req->src_path, NEU_PATH_LEN - 1);
24032403
cmd.more = req->more;
24042404
cmd.len = req->len;
24052405
cmd.data = calloc(req->len, sizeof(uint8_t));

0 commit comments

Comments
 (0)