public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 7/7] misc: syslog: Use RFC5424
Date: Wed, 6 Oct 2021 16:36:08 -0300	[thread overview]
Message-ID: <0cbfa4bb-a5b5-9fa8-39e9-cadd3ba92330@linaro.org> (raw)
In-Reply-To: <3e3847f2-b149-8202-1e45-b1e64074b54b@cs.ucla.edu>



On 05/10/2021 16:07, Paul Eggert wrote:
> On 10/5/21 6:56 AM, Adhemerval Zanella via Libc-alpha wrote:
> 
>> +struct timebuf_t
>> +{
>> +  char b[sizeof ("YYYY-MM-DDThh:mm:ss.nnnnnnZ")];
>> +};
> 
> This won't work if people fiddle with their system clocks and set the time far in the future or past. I suggest including <intprops.h> and using the following instead:
> 
>   struct timebuf
>   {
>     /* Leave room for outlandish but possible years.
>        The "+ 1" is for strftime's adding 1900 to tm_year.  */
>     char b[INT_STRLEN_BOUND (int) + 1
>        + sizeof "-MM-DDThh:mm:ss.nnnnnnZ"];
>   };

Indeed, I did not consider it and the suggestion makes sense.

> 
> 
>> +  struct tm now_tm;
>> +  __gmtime64_r (&ts.tv_sec, &now_tm);
> 
> This messes up if __gmtime64_r fails, which is possible when time_t is very large (or very negative).
> 
>> +  __snprintf (timebuf->b, sizeof (timebuf->b),
>> +              "%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
>> +              now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
>> +              now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
>> +              (int32_t) ts.tv_nsec / 1000);
> 
> This messes up if now_tm.tm_year + 1900 overflows. (strftime has a similar bug; it should get fixed too but one thing at a time.) Also, that 'int32_t' should be plain 'int', to match the format. And it's a bit better to not use casts when it's easy, as is the case here.
> 
> I suggest something like this instead (I haven't tested it):
> 
>   struct tm *gmtm = __gmtime64_r (&ts.tv_sec, &now_tm);
>   if (gmtm == NULL)
>     strcpy (timebuf->b, "-");
>   else
>     {
>       size_t datebytes = __strftime_l (timebuf->b, sizeof timebuf->b,
>                                        "%FT%T", gmtm, _nl_C_locobj_ptr);
>       int usec = ts.tv_nsec / 1000;
>       __snprintf (timebuf->b + datebytes, sizeof timebuf->b - datebytes,
>                   ".%06dZ", usec);
>     }
> 
> The "-" is as per RFC 5434's NILVALUE.

It make sense, I changes to your suggestion thanks.

      reply	other threads:[~2021-10-06 19:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-05 13:56 [PATCH 0/7] Use RFC5424 for syslog Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 1/7] support: Add xmkfifo Adhemerval Zanella
2021-10-05 19:10   ` Florian Weimer
2021-10-06 19:25     ` Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 2/7] misc: Add syslog test Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 3/7] misc: syslog: Fix indentation and style Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 4/7] misc: syslog: Simplify implementation Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 5/7] misc: syslog: Use static buffer Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 6/7] misc: syslog: Move SYSLOG_NAME to USE_MISC (BZ #16355) Adhemerval Zanella
2021-10-05 13:56 ` [PATCH 7/7] misc: syslog: Use RFC5424 Adhemerval Zanella
2021-10-05 19:07   ` Paul Eggert
2021-10-06 19:36     ` Adhemerval Zanella [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0cbfa4bb-a5b5-9fa8-39e9-cadd3ba92330@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).