Skip to content

Commit

Permalink
Use strnlen instead of strlen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dTenebrae committed Sep 12, 2023
1 parent 2788cdc commit aaa739c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/output/socketoutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <sys/un.h>
#include <unistd.h>

#define PATH_SIZE 107


/*
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit aaa739c

Please sign in to comment.