From 5d4cf4d445b2829deac9c755f65c98151eabc157 Mon Sep 17 00:00:00 2001 From: Denis 'GNUtoo' Carikli Date: Wed, 1 Jun 2022 14:11:27 +0200 Subject: gprs: ipc_gprs_define_pdp_context_setup: fix truncated string Without that fix, when compiling libsamsung-ipc with --enable-strict-cflags, we have the following error: gprs.c: In function 'ipc_gprs_define_pdp_context_setup': gprs.c:38:9: error: 'strncpy' specified bound 124 equals destination size [-Werror=stringop-truncation] 38 | strncpy((char *) data->apn, apn, sizeof(data->apn)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It comes from the fact that the size of data->apn is fixed (to 124) in include/gprs.h: struct ipc_gprs_define_pdp_context_data { unsigned char enable; unsigned char cid; unsigned char magic; unsigned char apn[124]; } __attribute__((__packed__)); The issue is that in ipc_gprs_define_pdp_context_setup we had the following code: int ipc_gprs_define_pdp_context_setup( struct ipc_gprs_define_pdp_context_data *data, unsigned char enable, unsigned char cid, const char *apn) { [...] strncpy((char *) data->apn, apn, sizeof(data->apn)); [...] return 0; } And in the strcpy(3) manual we have: The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. So if we have an apn of 124 or more characters, the copied data will not be null terminated hence the warning. Since apn is a character array and not a string, and that the field is padded with zeros anyway we can use memcpy instead. Signed-off-by: Denis 'GNUtoo' Carikli --- samsung-ipc/gprs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samsung-ipc/gprs.c b/samsung-ipc/gprs.c index 89f06e7..64d94f9 100644 --- a/samsung-ipc/gprs.c +++ b/samsung-ipc/gprs.c @@ -35,7 +35,7 @@ int ipc_gprs_define_pdp_context_setup( data->cid = cid; data->magic = 0x02; - strncpy((char *) data->apn, apn, sizeof(data->apn)); + memcpy(data->apn, apn, sizeof(data->apn)); return 0; } -- cgit v1.2.3