[PATCH] [FIXED] Compiler warning for JSON get time
authorIvan Kozlovic <ivan@synadia.com>
Tue, 12 Jul 2022 16:22:53 +0000 (10:22 -0600)
committerPeter Michael Green <plugwash@raspbian.org>
Thu, 28 Jul 2022 07:54:37 +0000 (08:54 +0100)
When formatting a date/time, the max lenght is 35 characters, so
the backing array should be 36 at least (for the terminal '\0').

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
Gbp-Pq: Name 208f27a8ccd80bbadba4409b68fb5fe308b97c56.patch

src/util.c

index bd770ebbed927a72f0f3e13fee28c5b254642f4b..05018609770fea5ebcb2773197e6e07eeb1ef6f0 100644 (file)
@@ -1292,8 +1292,8 @@ nats_JSONGetTime(nats_JSON *json, const char *fieldName, int64_t *timeUTC)
     char        utcOff[7]   = {'\0'};
     int64_t     nanosecs    = 0;
     char        *p          = NULL;
-    char        orgStr[35]  = {'\0'};
-    char        timeStr[35] = {'\0'};
+    char        orgStr[36]  = {'\0'};
+    char        timeStr[36] = {'\0'};
     char        offSign     = '+';
     int         offHours    = 0;
     int         offMin      = 0;
@@ -1319,7 +1319,7 @@ nats_JSONGetTime(nats_JSON *json, const char *fieldName, int64_t *timeUTC)
     l = (int) strlen(str);
     // The smallest date/time should be: "YYYY:MM:DDTHH:MM:SSZ", which is 20
     // while the longest should be: "YYYY:MM:DDTHH:MM:SS.123456789-12:34" which is 35
-    if ((l < 20) || (l > (int) sizeof(timeStr)))
+    if ((l < 20) || (l > (int) (sizeof(timeStr) - 1)))
     {
         if (l < 20)
             s = nats_setError(NATS_INVALID_ARG, "time '%s' too small", str);