From: Dave Hart Date: Sun, 19 Mar 2023 05:05:11 +0000 (-0400) Subject: Don't cast size_t to int, update contract for followlink(). X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7e82c4adda4bd800cd09ebb8709868a8dc16652;p=thirdparty%2Fntp.git Don't cast size_t to int, update contract for followlink(). Make ssize_t size match size_t, provide SSIZE_MAX on Windows. bk: 64169807wtm82iCy2AhNAbmk6IX4_Q --- diff --git a/ports/winnt/include/config.h b/ports/winnt/include/config.h index 17526c266..4439b1cdd 100644 --- a/ports/winnt/include/config.h +++ b/ports/winnt/include/config.h @@ -309,8 +309,7 @@ extern void arc4random_buf(void *buf, size_t nbytes); */ #define __func__ __FUNCTION__ -typedef int pid_t; /* PID is an int */ -typedef int ssize_t; /* ssize is an int */ +typedef int pid_t; /* PID is an int */ /* * Map the stream to the file number @@ -502,6 +501,13 @@ typedef unsigned long uintptr_t; typedef unsigned __int64 uint64_t; #endif +#ifdef _WIN64 /* mirroring SIZE_MAX from limits.h */ + typedef SSIZE_T ssize_t; +#define SSIZE_MAX _I64_MAX +#else + typedef int ssize_t; +#define SSIZE_MAX INT_MAX +#endif /* Directory separator, usually / or \ */ #define DIR_SEP '\\' diff --git a/util/ntp-keygen.c b/util/ntp-keygen.c index c40a85298..ff9d6cad8 100644 --- a/util/ntp-keygen.c +++ b/util/ntp-keygen.c @@ -277,16 +277,16 @@ followlink( ssize_t len; char * target; - REQUIRE(bufsiz > 0); + REQUIRE(bufsiz > 0 && bufsiz <= SSIZE_MAX); target = emalloc(bufsiz); len = readlink(fname, target, bufsiz); - if (len < 0 ) { + if (len < 0) { fname[0] = '\0'; return; } - if (len > (int)bufsiz - 1) - len = (int)bufsiz - 1; + if ((size_t)len > bufsiz - 1) + len = bufsiz - 1; memcpy(fname, target, len); fname[len] = '\0'; free(target);