Skip to content

Commit b7bd627

Browse files
committed
autotools, CMake: fix issues with snprintf test and sanitizers.
Avoid trying to cast negative values to unsigned types, or doing shifts of signed types, in order not to have the test program fail if we're building with undefined-behavior sanitizers enabled. See the-tcpdump-group/libpcap#1396 for the equivalent libpcap issue.
1 parent 7fe2a0e commit b7bd627

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

CMakeLists.txt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,32 +542,50 @@ if (NOT CMAKE_CROSSCOMPILING)
542542
#include <inttypes.h>
543543
#include <sys/types.h>
544544
545+
#if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
546+
/*
547+
* On UN*Xes, this is a signed integer type of the same size as size_t.
548+
*
549+
* It's not defined by Visual Studio; we assume that ptrdiff_t will
550+
* be a type that is a signed integer type of the same size as size_t.
551+
*/
552+
typedef ptrdiff_t ssize_t;
553+
#endif
554+
555+
/*
556+
* Avoid trying to cast negative values to unsigned types, or doing
557+
* shifts of signed types, in order not to have the test program fail
558+
* if we're building with undefined-behavior sanitizers enabled.
559+
*/
545560
int main()
546561
{
547562
char buf[100];
548-
uint64_t t = (uint64_t)1 << 32;
563+
unsigned int ui = sizeof(buf);
564+
int i = sizeof(buf);
565+
int64_t i64 = INT64_C(0x100000000);
566+
uint64_t ui64 = UINT64_C(0x100000000);
549567
550-
snprintf(buf, sizeof(buf), \"%zu\", sizeof(buf));
568+
snprintf(buf, sizeof(buf), \"%zu\", (size_t)ui);
551569
if (strncmp(buf, \"100\", sizeof(buf)))
552570
return 1;
553571
554-
snprintf(buf, sizeof(buf), \"%zd\", -sizeof(buf));
572+
snprintf(buf, sizeof(buf), \"%zd\", (ssize_t)(-i));
555573
if (strncmp(buf, \"-100\", sizeof(buf)))
556574
return 2;
557575
558-
snprintf(buf, sizeof(buf), \"%\" PRId64, -t);
576+
snprintf(buf, sizeof(buf), \"%\" PRId64, -i64);
559577
if (strncmp(buf, \"-4294967296\", sizeof(buf)))
560578
return 3;
561579
562-
snprintf(buf, sizeof(buf), \"0o%\" PRIo64, t);
580+
snprintf(buf, sizeof(buf), \"0o%\" PRIo64, ui64);
563581
if (strncmp(buf, \"0o40000000000\", sizeof(buf)))
564582
return 4;
565583
566-
snprintf(buf, sizeof(buf), \"0x%\" PRIx64, t);
584+
snprintf(buf, sizeof(buf), \"0x%\" PRIx64, ui64);
567585
if (strncmp(buf, \"0x100000000\", sizeof(buf)))
568586
return 5;
569587
570-
snprintf(buf, sizeof(buf), \"%\" PRIu64, t);
588+
snprintf(buf, sizeof(buf), \"%\" PRIu64, ui64);
571589
if (strncmp(buf, \"4294967296\", sizeof(buf)))
572590
return 6;
573591

configure.ac

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,32 +327,50 @@ AC_RUN_IFELSE(
327327
#include <inttypes.h>
328328
#include <sys/types.h>
329329
330+
#if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
331+
/*
332+
* On UN*Xes, this is a signed integer type of the same size as size_t.
333+
*
334+
* It's not defined by Visual Studio; we assume that ptrdiff_t will
335+
* be a type that is a signed integer type of the same size as size_t.
336+
*/
337+
typedef ptrdiff_t ssize_t;
338+
#endif
339+
340+
/*
341+
* Avoid trying to cast negative values to unsigned types, or doing
342+
* shifts of signed types, in order not to have the test program fail
343+
* if we're building with undefined-behavior sanitizers enabled.
344+
*/
330345
int main()
331346
{
332347
char buf[100];
333-
uint64_t t = (uint64_t)1 << 32;
348+
unsigned int ui = sizeof(buf);
349+
int i = sizeof(buf);
350+
int64_t i64 = INT64_C(0x100000000);
351+
uint64_t ui64 = UINT64_C(0x100000000);
334352
335-
snprintf(buf, sizeof(buf), "%zu", sizeof(buf));
353+
snprintf(buf, sizeof(buf), "%zu", (size_t)ui);
336354
if (strncmp(buf, "100", sizeof(buf)))
337355
return 1;
338356
339-
snprintf(buf, sizeof(buf), "%zd", -sizeof(buf));
357+
snprintf(buf, sizeof(buf), "%zd", (ssize_t)(-i));
340358
if (strncmp(buf, "-100", sizeof(buf)))
341359
return 2;
342360
343-
snprintf(buf, sizeof(buf), "%" PRId64, -t);
361+
snprintf(buf, sizeof(buf), "%" PRId64, -i64);
344362
if (strncmp(buf, "-4294967296", sizeof(buf)))
345363
return 3;
346364
347-
snprintf(buf, sizeof(buf), "0o%" PRIo64, t);
365+
snprintf(buf, sizeof(buf), "0o%" PRIo64, ui64);
348366
if (strncmp(buf, "0o40000000000", sizeof(buf)))
349367
return 4;
350368
351-
snprintf(buf, sizeof(buf), "0x%" PRIx64, t);
369+
snprintf(buf, sizeof(buf), "0x%" PRIx64, ui64);
352370
if (strncmp(buf, "0x100000000", sizeof(buf)))
353371
return 5;
354372
355-
snprintf(buf, sizeof(buf), "%" PRIu64, t);
373+
snprintf(buf, sizeof(buf), "%" PRIu64, ui64);
356374
if (strncmp(buf, "4294967296", sizeof(buf)))
357375
return 6;
358376

0 commit comments

Comments
 (0)