On Thu, Jun 23, 2022 at 12:53:37PM +0200, Claudio Jeker wrote:
> Newer gcc likes to bitch about snprintf buffers being to small for
> insane numbers. This made me look at fmt_timeframe() and I decided to
> clean it up a bit.

It also likes to throw colorful fits about uninitialized variables...

> 
> First of all the ring buffer is not needed. fmt_timeframe() is never used
> multiple time in a single printf() call. So drop that.
> Then make sure that the time_t is positive.
> Last print just the number of weeks when a session is up for more than
> 19 years.
> 
> Now gcc is still not happy but that is a false claim.

Agreed.

ok

> -- 
> :wq Claudio
> 
> Index: bgpctl.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/bgpctl/bgpctl.c,v
> retrieving revision 1.277
> diff -u -p -r1.277 bgpctl.c
> --- bgpctl.c  15 Jun 2022 10:10:50 -0000      1.277
> +++ bgpctl.c  23 Jun 2022 10:50:15 -0000
> @@ -575,22 +575,17 @@ fmt_auth_method(enum auth_method method)
>       }
>  }
>  
> -#define TF_BUFS      8
> -#define TF_LEN       9
> +#define TF_LEN       16
>  
>  const char *
>  fmt_timeframe(time_t t)
>  {
> -     char            *buf;
> -     static char      tfbuf[TF_BUFS][TF_LEN];        /* ring buffer */
> -     static int       idx = 0;
> +     static char      buf[TF_LEN];
>       unsigned int     sec, min, hrs, day;
> -     unsigned long long      week;
> -
> -     buf = tfbuf[idx++];
> -     if (idx == TF_BUFS)
> -             idx = 0;
> +     unsigned long long       week;
>  
> +     if (t < 0)
> +             t = 0;
>       week = t;
>  
>       sec = week % 60;
> @@ -602,7 +597,9 @@ fmt_timeframe(time_t t)
>       day = week % 7;
>       week /= 7;
>  
> -     if (week > 0)
> +     if (week >= 1000)
> +             snprintf(buf, TF_LEN, "%02lluw", week);
> +     else if (week > 0)
>               snprintf(buf, TF_LEN, "%02lluw%01ud%02uh", week, day, hrs);
>       else if (day > 0)
>               snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
> 

Reply via email to