From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 18739 invoked from network); 15 Mar 2021 20:06:37 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 15 Mar 2021 20:06:37 -0000 Received: (qmail 9427 invoked by uid 550); 15 Mar 2021 20:06:35 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 9409 invoked from network); 15 Mar 2021 20:06:35 -0000 Date: Mon, 15 Mar 2021 16:06:23 -0400 From: Rich Felker To: Yossi Gottlieb Cc: musl@lists.openwall.com Message-ID: <20210315200622.GE32655@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] strftime() unexpectedly modifies errno On Mon, Mar 15, 2021 at 09:43:36PM +0200, Yossi Gottlieb wrote: > It seems like strftime() unexpectedly modifies errno, which is always set > to EINVAL when it returns. Looks like it's not related to any specific > format. > > Here's an example: > > #include > #include > #include > #include > > int main(int argc, char *argv[]) > { > time_t now = time(NULL); > struct tm *tm = localtime(&now); > char buf[100]; > > errno = 0; > size_t len = strftime(buf, sizeof(buf), "%d %b %Y %H:%M:%S.", tm); > > printf("len=%zu\n", len); > printf("buf=%s\n", buf); > printf("errno=%d\n", errno); > } This isn't unexpected. Any function except for a few specific ones documented not to can modify errno as a side effect of success. Inspecting errno is only meaningful immediately after failure. The relevant text in the C language (C11) is 7.5 Errors , ΒΆ3: "The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard." There's equivalent text in POSIX as well. Rich