Re: [review v2] Use ctime_r and localtime_r if available

Christian Biesinger via gdb-patches
2019-11-03T20:09:36Z

On Sun, Nov 3, 2019 at 2:20 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Nov 02 2019, Christian Biesinger (Code Review) wrote:
>
> > diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
> > index 67f9f3a..e0bad81 100644
> > --- a/gdb/nat/linux-osdata.c
> > +++ b/gdb/nat/linux-osdata.c
> > @@ -912,7 +912,13 @@
> > {
> > time_t t = (time_t) seconds;
> >
> > - strncpy (time, ctime (&t), maxlen);
> > + char buf[30];
> > +#ifdef HAVE_CTIME_R
> > + const char *time_str = ctime_r (&t, buf);
> > +#else
> > + const char *time_str = ctime (&t);
> > +#endif
>
> buf is unused if !HAVE_CTIME_R.

Thanks, fixed.

> Note that both ctime and ctime_r are obsolescent and should be replaced
> by strftime. gdb currently doesn't setlocale LC_TIME, but if it does it
> would make the use of these functions undefined.

I read through the manpage for ctime/ctime_r and I don't see any note
that the behavior is undefined when setlocale is called, or that it is
obsolete. For completeness, I also looked at the Solaris manpage and
it does not say that either. Instead, it will use a locale-independent
format, which seems desirable here since it is serialized into XML.

Christian