QProcess/Unix: fix unsafe strncpy() use

GCC 11 complains:

    qprocess_unix.cpp:672:12: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 508 equals destination size [-Werror=stringop-truncation]
      672 |     strncpy(error.function, description, sizeof(error.function));
          |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And it's correct: if description is longer than
sizeof(error.function), then error.function will not be
NUL-terminated.

While a quick check suggests that the user of the field performs a
qstrnlen(), thus avoiding falling off the unterminated end of
error.function, it's safer to always NUL-terminate. A single added
qDebug() << error.function would already be UB.

Fix by using _q_strncpy(), which is also more efficient, as it doesn't
write 0.5KiB of NULs in the likely case that description is short.

Amends 90bc0ad41f.

Change-Id: If5c2cb80fc4a3c92b8e78b680a635045bb14a30d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2023-06-12 12:01:15 +02:00
parent 103ffe1b58
commit 30f87c86b4

View File

@ -669,7 +669,7 @@ failChildProcess(const QProcessPrivate *d, const char *description, int code) no
{
ChildError error = {};
error.code = code;
strncpy(error.function, description, sizeof(error.function));
qstrncpy(error.function, description, sizeof(error.function));
qt_safe_write(d->childStartedPipe[1], &error, sizeof(error));
_exit(-1);
}