From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zimbra.cs.ucla.edu (zimbra.cs.ucla.edu [131.179.128.68]) by sourceware.org (Postfix) with ESMTPS id 379E1385802D for ; Tue, 5 Oct 2021 19:07:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 379E1385802D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 387A71600C2; Tue, 5 Oct 2021 12:07:33 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id SA2y5ndbMKRV; Tue, 5 Oct 2021 12:07:32 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 71C76160119; Tue, 5 Oct 2021 12:07:32 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id kOxrKZ_H5Pxm; Tue, 5 Oct 2021 12:07:32 -0700 (PDT) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 4F12B1600ED; Tue, 5 Oct 2021 12:07:32 -0700 (PDT) To: Adhemerval Zanella References: <20211005135631.3209020-1-adhemerval.zanella@linaro.org> <20211005135631.3209020-8-adhemerval.zanella@linaro.org> From: Paul Eggert Organization: UCLA Computer Science Department Cc: libc-alpha@sourceware.org Subject: Re: [PATCH 7/7] misc: syslog: Use RFC5424 Message-ID: <3e3847f2-b149-8202-1e45-b1e64074b54b@cs.ucla.edu> Date: Tue, 5 Oct 2021 12:07:32 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20211005135631.3209020-8-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_NUMSUBJECT, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Oct 2021 19:07:35 -0000 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=20 time far in the future or past. I suggest including and=20 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"]; }; > + 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=20 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=20 similar bug; it should get fixed too but one thing at a time.) Also,=20 that 'int32_t' should be plain 'int', to match the format. And it's a=20 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 =3D __gmtime64_r (&ts.tv_sec, &now_tm); if (gmtm =3D=3D NULL) strcpy (timebuf->b, "-"); else { size_t datebytes =3D __strftime_l (timebuf->b, sizeof timebuf->b, "%FT%T", gmtm, _nl_C_locobj_ptr); int usec =3D ts.tv_nsec / 1000; __snprintf (timebuf->b + datebytes, sizeof timebuf->b - datebytes, ".%06dZ", usec); } The "-" is as per RFC 5434's NILVALUE.