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:
parent
f8f9100a0d
commit
935d855b47
2 changed files with 22 additions and 3 deletions
|
@ -4,10 +4,11 @@
|
||||||
void test_unit(void) {
|
void test_unit(void) {
|
||||||
NTP_int64 ntp_ts, ntp_fuzz;
|
NTP_int64 ntp_ts, ntp_fuzz;
|
||||||
struct timespec ts, ts2;
|
struct timespec ts, ts2;
|
||||||
|
struct sockaddr_un sun;
|
||||||
double x, y;
|
double x, y;
|
||||||
Float f;
|
Float f;
|
||||||
int i, j, c;
|
int i, j, c;
|
||||||
char buf[16];
|
char buf[16], *s;
|
||||||
|
|
||||||
for (i = -31; i < 31; i++) {
|
for (i = -31; i < 31; i++) {
|
||||||
x = pow(2.0, i);
|
x = pow(2.0, i);
|
||||||
|
@ -145,4 +146,18 @@ void test_unit(void) {
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
TEST_CHECK(c > 46000 && c < 48000);
|
TEST_CHECK(c > 46000 && c < 48000);
|
||||||
|
|
||||||
|
for (i = 1; i < 2 * BUFFER_LENGTH; i++) {
|
||||||
|
sun.sun_family = AF_UNIX;
|
||||||
|
for (j = 0; j + 1 < i && j + 1 < sizeof (sun.sun_path); j++)
|
||||||
|
sun.sun_path[j] = 'A' + j % 26;
|
||||||
|
sun.sun_path[j] = '\0';
|
||||||
|
s = UTI_SockaddrToString((struct sockaddr *)&sun);
|
||||||
|
if (i <= BUFFER_LENGTH) {
|
||||||
|
TEST_CHECK(!strcmp(s, sun.sun_path));
|
||||||
|
} else {
|
||||||
|
TEST_CHECK(!strncmp(s, sun.sun_path, BUFFER_LENGTH - 2));
|
||||||
|
TEST_CHECK(s[BUFFER_LENGTH - 2] == '>');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
util.c
8
util.c
|
@ -558,7 +558,7 @@ char *UTI_SockaddrToString(struct sockaddr *sa)
|
||||||
{
|
{
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
IPAddr ip;
|
IPAddr ip;
|
||||||
char *result;
|
char *result, *sun_path;
|
||||||
|
|
||||||
result = NEXT_BUFFER;
|
result = NEXT_BUFFER;
|
||||||
|
|
||||||
|
@ -571,7 +571,11 @@ char *UTI_SockaddrToString(struct sockaddr *sa)
|
||||||
snprintf(result, BUFFER_LENGTH, "%s:%hu", UTI_IPToString(&ip), port);
|
snprintf(result, BUFFER_LENGTH, "%s:%hu", UTI_IPToString(&ip), port);
|
||||||
break;
|
break;
|
||||||
case AF_UNIX:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
snprintf(result, BUFFER_LENGTH, "[UNKNOWN]");
|
snprintf(result, BUFFER_LENGTH, "[UNKNOWN]");
|
||||||
|
|
Loading…
Reference in a new issue