From aaa739c522fc60c511f232fc8ee60a8c95f29344 Mon Sep 17 00:00:00 2001 From: Artem Chernyshev Date: Tue, 12 Sep 2023 14:02:45 +0300 Subject: [PATCH] Use strnlen instead of strlen On older gcc we can get a stringop-overflow warning, since it trying to predict a nonstring overflow. It could be fixed with strnlen() function. --- src/output/socketoutput.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/output/socketoutput.c b/src/output/socketoutput.c index 59a24fd8..42092ed0 100644 --- a/src/output/socketoutput.c +++ b/src/output/socketoutput.c @@ -39,6 +39,7 @@ #include #include +#define PATH_SIZE 107 /* @@ -76,12 +77,11 @@ int snoopy_output_socketoutput (char const * const logMessage, char const * cons } remote.sun_family = AF_LOCAL; - strncpy(remote.sun_path, arg, 107); // Coverity suggests -1 here - if (strlen(arg) > 107) { - remote.sun_path[107] = '\0'; - } + strncpy(remote.sun_path, arg, PATH_SIZE); // Coverity suggests -1 here + if (strlen(arg) > PATH_SIZE) + remote.sun_path[PATH_SIZE] = '\0'; - remoteLength = (int) strlen(remote.sun_path) + (int) sizeof(remote.sun_family); + remoteLength = (int) strnlen(remote.sun_path, PATH_SIZE) + (int) sizeof(remote.sun_family); if (connect(s, (struct sockaddr *)&remote, remoteLength) == -1) { close(s); return SNOOPY_OUTPUT_FAILURE;