]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Don't cast size_t to int, update contract for followlink().
authorDave Hart <hart@ntp.org>
Sun, 19 Mar 2023 05:05:11 +0000 (01:05 -0400)
committerDave Hart <hart@ntp.org>
Sun, 19 Mar 2023 05:05:11 +0000 (01:05 -0400)
Make ssize_t size match size_t, provide SSIZE_MAX on Windows.

bk: 64169807wtm82iCy2AhNAbmk6IX4_Q

ports/winnt/include/config.h
util/ntp-keygen.c

index 17526c266434dce5866be26f78c6e6618df4a01c..4439b1cdd4f623b5ec481e495f7c36bb1c16771e 100644 (file)
@@ -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 '\\'
index c40a85298b1165ced1db044cae409cf3a713c0a8..ff9d6cad89809444f11bdaa291b983f892a14b39 100644 (file)
@@ -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);