]> granicus.if.org Git - python/commitdiff
delta_str(): Purged last uses of sprintf (in favor of PyOS_snprintf).
authorTim Peters <tim.peters@gmail.com>
Wed, 18 Dec 2002 20:19:21 +0000 (20:19 +0000)
committerTim Peters <tim.peters@gmail.com>
Wed, 18 Dec 2002 20:19:21 +0000 (20:19 +0000)
Modules/datetimemodule.c

index 5652cf43a58e55b7c76036c6b9cb3ebdc6b14815..8d245707138ce43222b564722c6b4dad95b837f2 100644 (file)
@@ -1805,27 +1805,42 @@ delta_str(PyDateTime_Delta *self)
        int us = GET_TD_MICROSECONDS(self);
        int hours;
        int minutes;
-       char buf[500];
-       int i = 0;
+       char buf[100];
+       char *pbuf = buf;
+       size_t buflen = sizeof(buf);
+       int n;
 
        minutes = divmod(seconds, 60, &seconds);
        hours = divmod(minutes, 60, &minutes);
 
        if (days) {
-               i += sprintf(buf + i, "%d day%s, ", days,
-                            (days == 1 || days == -1) ? "" : "s");
-               assert(i < sizeof(buf));
+               n = PyOS_snprintf(pbuf, buflen, "%d day%s, ", days,
+                                 (days == 1 || days == -1) ? "" : "s");
+               if (n < 0 || (size_t)n >= buflen)
+                       goto Fail;
+               pbuf += n;
+               buflen -= (size_t)n;
        }
 
-       i += sprintf(buf + i, "%d:%02d:%02d", hours, minutes, seconds);
-       assert(i < sizeof(buf));
+       n = PyOS_snprintf(pbuf, buflen, "%d:%02d:%02d",
+                         hours, minutes, seconds);
+       if (n < 0 || (size_t)n >= buflen)
+               goto Fail;
+       pbuf += n;
+       buflen -= (size_t)n;
 
        if (us) {
-               i += sprintf(buf + i, ".%06d", us);
-               assert(i < sizeof(buf));
+               n = PyOS_snprintf(pbuf, buflen, ".%06d", us);
+               if (n < 0 || (size_t)n >= buflen)
+                       goto Fail;
+               pbuf += n;
        }
 
-       return PyString_FromStringAndSize(buf, i);
+       return PyString_FromStringAndSize(buf, pbuf - buf);
+
+ Fail:
+       PyErr_SetString(PyExc_SystemError, "goofy result from PyOS_snprintf");
+       return NULL;
 }
 
 /* Pickle support.  Quite a maze!  While __getstate__/__setstate__ sufficed