Skip to content

Commit 85dd0d5

Browse files
Nancy Zhangclaude
andcommitted
fix: Replace strlen/strcpy/strncpy/memcpy with snprintf to resolve Codacy findings (CWE-120, CWE-126)
Use snprintf with %s and %.*s for all string copies in apphand_converter to eliminate Codacy warnings about unbounded copies, MS-banned functions, and non-null-terminated string handling. No behavioral change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Nancy Zhang <nancy.zhang@enteligent.com>
1 parent 8554dfa commit 85dd0d5

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

iso15118/shared/cbv2g_wrapper/src/apphand_converter.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,13 @@ int apphand_decode(const uint8_t* exi, size_t exi_len, char* out, size_t out_siz
146146
goto cleanup;
147147
}
148148

149-
/* Copy to output buffer */
150-
size_t json_len = strlen(json_str);
151-
if (json_len >= out_size) {
152-
set_error("Output buffer too small: need %zu, have %zu", json_len + 1, out_size);
149+
/* Copy to output buffer using snprintf for bounded write */
150+
int written = snprintf(out, out_size, "%s", json_str);
151+
if (written < 0 || (size_t)written >= out_size) {
152+
set_error("Output buffer too small: need %d, have %zu", written + 1, out_size);
153153
result = CBV2G_ERROR_BUFFER_TOO_SMALL;
154154
goto cleanup;
155155
}
156-
157-
strcpy(out, json_str);
158156
result = CBV2G_SUCCESS;
159157

160158
cleanup:
@@ -190,15 +188,16 @@ static int json_to_apphand_req(cJSON* json, struct appHand_supportedAppProtocolR
190188
struct appHand_AppProtocolType* proto = &req->AppProtocol.array[i];
191189
init_appHand_AppProtocolType(proto);
192190

193-
/* ProtocolNamespace */
191+
/* ProtocolNamespace — snprintf with %.*s is bounded and always null-terminates */
194192
const char* ns = json_get_string(item, "ProtocolNamespace");
195193
size_t ns_len = strnlen(ns, appHand_ProtocolNamespace_CHARACTER_SIZE);
196-
if (ns_len >= appHand_ProtocolNamespace_CHARACTER_SIZE) {
197-
ns_len = appHand_ProtocolNamespace_CHARACTER_SIZE - 1;
198-
}
199-
strncpy(proto->ProtocolNamespace.characters, ns, ns_len);
200-
proto->ProtocolNamespace.characters[ns_len] = '\0';
201-
proto->ProtocolNamespace.charactersLen = ns_len;
194+
int ns_written = snprintf(proto->ProtocolNamespace.characters,
195+
appHand_ProtocolNamespace_CHARACTER_SIZE,
196+
"%.*s", (int)ns_len, ns);
197+
if (ns_written < 0) ns_written = 0;
198+
if ((size_t)ns_written >= appHand_ProtocolNamespace_CHARACTER_SIZE)
199+
ns_written = appHand_ProtocolNamespace_CHARACTER_SIZE - 1;
200+
proto->ProtocolNamespace.charactersLen = (size_t)ns_written;
202201

203202
/* Version numbers */
204203
proto->VersionNumberMajor = json_get_int(item, "VersionNumberMajor");
@@ -272,8 +271,8 @@ static cJSON* apphand_req_to_json(const struct appHand_supportedAppProtocolReq*
272271
if (ns_len > appHand_ProtocolNamespace_CHARACTER_SIZE) {
273272
ns_len = appHand_ProtocolNamespace_CHARACTER_SIZE;
274273
}
275-
memcpy(ns, proto->ProtocolNamespace.characters, ns_len);
276-
ns[ns_len] = '\0';
274+
snprintf(ns, sizeof(ns), "%.*s", (int)ns_len,
275+
proto->ProtocolNamespace.characters);
277276

278277
cJSON_AddStringToObject(item, "ProtocolNamespace", ns);
279278
cJSON_AddNumberToObject(item, "VersionNumberMajor", proto->VersionNumberMajor);

0 commit comments

Comments
 (0)