If strftime(3) returns 0, the contents of the string array is
authornbk <nbk>
Fri, 23 Sep 2005 14:35:00 +0000 (14:35 +0000)
committernbk <nbk>
Fri, 23 Sep 2005 14:35:00 +0000 (14:35 +0000)
undefined, therefore it should not be copied.

Thanks to Primoz Bratanic for spotting this.

src/lib/print.c
src/modules/rlm_counter/rlm_counter.c
src/modules/rlm_sqlcounter/rlm_sqlcounter.c

index 3cedc00..873bb0b 100644 (file)
@@ -105,11 +105,12 @@ int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
 {
        DICT_VALUE  *v;
        char        buf[1024];
-       const char  *a;
+       const char  *a = NULL;
+       size_t      len;
        time_t      t;
        struct tm   s_tm;
 
-       out[0] = 0;
+       out[0] = '\0';
        if (!vp) return 0;
 
        switch (vp->type) {
@@ -162,13 +163,13 @@ int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
                case PW_TYPE_DATE:
                        t = vp->lvalue;
                        if (delimitst) {
-                         strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
-                                  localtime_r(&t, &s_tm));
+                         len = strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
+                                        localtime_r(&t, &s_tm));
                        } else {
-                         strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
-                                  localtime_r(&t, &s_tm));
+                         len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
+                                        localtime_r(&t, &s_tm));
                        }
-                       a = buf;
+                       if (len > 0) a = buf;
                        break;
                case PW_TYPE_IPADDR:
                        a = inet_ntop(AF_INET, &(vp->lvalue),
@@ -213,17 +214,17 @@ int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
                        a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
                        if (a) {
                                char *p = buf + strlen(buf);
-                               
                                sprintf(p, "/%u", (unsigned int) vp->vp_strvalue[1]);
                        }
                }
                        break;
 
                default:
-                       a = 0;
+                       a = "UNKNOWN-TYPE";
                        break;
        }
-       strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
+
+       if (a != NULL) strlcpy(out, a, outlen);
 
        return strlen(out);
 }
index e7b1dd9..8afac56 100644 (file)
@@ -243,21 +243,21 @@ static int reset_db(rlm_counter_t *data)
 
 static int find_next_reset(rlm_counter_t *data, time_t timeval)
 {
-       int ret=0;
-       unsigned int num=1;
-       char last = 0;
+       int ret = 0;
+       size_t len;
+       unsigned int num = 1;
+       char last = '\0';
        struct tm *tm, s_tm;
        char sCurrentTime[40], sNextTime[40];
 
        tm = localtime_r(&timeval, &s_tm);
-       strftime(sCurrentTime, sizeof(sCurrentTime),"%Y-%m-%d %H:%M:%S",tm);
+       len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
+       if (len == 0) *sCurrentTime = '\0';
        tm->tm_sec = tm->tm_min = 0;
 
        if (data->reset == NULL)
                return -1;
        if (isdigit((int) data->reset[0])){
-               unsigned int len=0;
-
                len = strlen(data->reset);
                if (len == 0)
                        return -1;
@@ -299,9 +299,11 @@ static int find_next_reset(rlm_counter_t *data, time_t timeval)
                        data->reset);
                return -1;
        }
-       strftime(sNextTime, sizeof(sNextTime),"%Y-%m-%d %H:%M:%S",tm);
-       DEBUG2("rlm_counter: Current Time: %d [%s], Next reset %d [%s]",
-               (int)timeval,sCurrentTime,(int)data->reset_time,sNextTime);
+
+       len = strftime(sNextTime, sizeof(sNextTime), "%Y-%m-%d %H:%M:%S", tm);
+       if (len == 0) *sNextTime = '\0';
+       DEBUG2("rlm_counter: Current Time: %li [%s], Next reset %li [%s]",
+               timeval, sCurrentTime, data->reset_time, sNextTime);
 
        return ret;
 }
index a618ee9..01efb36 100644 (file)
@@ -152,21 +152,21 @@ static int sql_escape_func(char *out, int outlen, const char *in)
 
 static int find_next_reset(rlm_sqlcounter_t *data, time_t timeval)
 {
-       int ret=0;
-       unsigned int num=1;
-       char last = 0;
+       int ret = 0;
+       size_t len;
+       unsigned int num = 1;
+       char last = '\0';
        struct tm *tm, s_tm;
        char sCurrentTime[40], sNextTime[40];
 
        tm = localtime_r(&timeval, &s_tm);
-       strftime(sCurrentTime, sizeof(sCurrentTime),"%Y-%m-%d %H:%M:%S",tm);
+       len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
+       if (len == 0) *sCurrentTime = '\0';
        tm->tm_sec = tm->tm_min = 0;
 
        if (data->reset == NULL)
                return -1;
        if (isdigit((int) data->reset[0])){
-               unsigned int len=0;
-
                len = strlen(data->reset);
                if (len == 0)
                        return -1;
@@ -208,9 +208,11 @@ static int find_next_reset(rlm_sqlcounter_t *data, time_t timeval)
                        data->reset);
                return -1;
        }
-       strftime(sNextTime, sizeof(sNextTime),"%Y-%m-%d %H:%M:%S",tm);
-       DEBUG2("rlm_sqlcounter: Current Time: %d [%s], Next reset %d [%s]",
-               (int)timeval,sCurrentTime,(int)data->reset_time, sNextTime);
+
+       len = strftime(sNextTime, sizeof(sNextTime),"%Y-%m-%d %H:%M:%S",tm);
+       if (len == 0) *sNextTime = '\0';
+       DEBUG2("rlm_sqlcounter: Current Time: %li [%s], Next reset %li [%s]",
+               timeval, sCurrentTime, data->reset_time, sNextTime);
 
        return ret;
 }
@@ -222,21 +224,21 @@ static int find_next_reset(rlm_sqlcounter_t *data, time_t timeval)
 
 static int find_prev_reset(rlm_sqlcounter_t *data, time_t timeval)
 {
-       int ret=0;
-       unsigned int num=1;
-       char last = 0;
+       int ret = 0;
+       size_t len;
+       unsigned int num = 1;
+       char last = '\0';
        struct tm *tm, s_tm;
        char sCurrentTime[40], sPrevTime[40];
 
        tm = localtime_r(&timeval, &s_tm);
-       strftime(sCurrentTime, sizeof(sCurrentTime),"%Y-%m-%d %H:%M:%S",tm);
+       len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
+       if (len == 0) *sCurrentTime = '\0';
        tm->tm_sec = tm->tm_min = 0;
 
        if (data->reset == NULL)
                return -1;
        if (isdigit((int) data->reset[0])){
-               unsigned int len=0;
-
                len = strlen(data->reset);
                if (len == 0)
                        return -1;
@@ -278,9 +280,10 @@ static int find_prev_reset(rlm_sqlcounter_t *data, time_t timeval)
                        data->reset);
                return -1;
        }
-       strftime(sPrevTime, sizeof(sPrevTime),"%Y-%m-%d %H:%M:%S",tm);
-       DEBUG2("rlm_sqlcounter: Current Time: %d [%s], Prev reset %d [%s]",
-               (int)timeval,sCurrentTime,(int)data->last_reset, sPrevTime);
+       len = strftime(sPrevTime, sizeof(sPrevTime), "%Y-%m-%d %H:%M:%S", tm);
+       if (len == 0) *sPrevTime = '\0';
+       DEBUG2("rlm_sqlcounter: Current Time: %li [%s], Prev reset %li [%s]",
+              timeval, sCurrentTime, data->last_reset, sPrevTime);
 
        return ret;
 }