From 1a12551012430c38186da14611dd4921e7319d20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Mon, 16 Jan 2023 14:19:20 -0500 Subject: [PATCH] Fix: ini parser: truncation of value name MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit clang 14 reports the following: ini-config/ini.cpp:88:16: warning: 'char* strncpy(char*, const char*, size_t)' output may be truncated copying 49 bytes from a string of length 199 [-Wstringop-truncation] 88 | strncpy(dest, src, size - 1); | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~ Indeed, a silent truncation of `name` occurs whenever it is longer than prev_name (49 characters, excluding the terminator). Report an error when this condition occurs. Signed-off-by: Jérémie Galarneau Change-Id: I973bd27185e0130d8e4a452525d9277de45ba200 --- src/common/ini-config/ini.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common/ini-config/ini.cpp b/src/common/ini-config/ini.cpp index cca38f8ca..c17a8c0e8 100644 --- a/src/common/ini-config/ini.cpp +++ b/src/common/ini-config/ini.cpp @@ -178,6 +178,10 @@ int ini_parse_file(FILE *file, ini_entry_handler handler, void *user) * Valid name[=:]value pair found, call * handler */ + if (strlen(name) >= sizeof(prev_name)) { + /* Truncation occurs, report an error. */ + error = lineno; + } strncpy0(prev_name, name, sizeof(prev_name)); if (handler(user, section, name, value) < 0 && !error) { error = lineno; -- 2.34.1