util: indicate truncated Unix socket path in UTI_SockaddrToString()

Specify the maximum length of the path in the snprintf() format to avoid
a new gcc warning (-Wformat-truncation). If the path doesn't fit in the
buffer, indicate with the '>' symbol that it was truncated. The function
is used only for debug messages.
This commit is contained in:
Miroslav Lichvar
2017-04-05 11:20:22 +02:00
parent f8f9100a0d
commit 935d855b47
2 changed files with 22 additions and 3 deletions

8
util.c
View File

@@ -558,7 +558,7 @@ char *UTI_SockaddrToString(struct sockaddr *sa)
{
unsigned short port;
IPAddr ip;
char *result;
char *result, *sun_path;
result = NEXT_BUFFER;
@@ -571,7 +571,11 @@ char *UTI_SockaddrToString(struct sockaddr *sa)
snprintf(result, BUFFER_LENGTH, "%s:%hu", UTI_IPToString(&ip), port);
break;
case AF_UNIX:
snprintf(result, BUFFER_LENGTH, "%s", ((struct sockaddr_un *)sa)->sun_path);
sun_path = ((struct sockaddr_un *)sa)->sun_path;
snprintf(result, BUFFER_LENGTH, "%.*s", BUFFER_LENGTH - 1, sun_path);
/* Indicate truncated path */
if (strlen(sun_path) >= BUFFER_LENGTH)
result[BUFFER_LENGTH - 2] = '>';
break;
default:
snprintf(result, BUFFER_LENGTH, "[UNKNOWN]");