mirror of
https://github.com/curl/curl.git
synced 2026-07-30 02:48:04 +03:00
localtime: detect thread-safe alternatives and use them
- add local API `toolx_localtime()` to wrap the banned function
`localtime()`. Used from libcurl, libtests and test servers.
- auto-detect and use `localtime_r()` where available (e.g. Linux).
Also to support multi-threading.
- use `localtime_s()` on Windows. It requires MSVC or mingw-w64 v4+.
Also to support multi-threading.
Use local workaround to also support mingw-w64 v3.
- add `src/toolx` to keep internal APIs used by the curl tool and tests,
but not by libcurl. `toolx_localtime()` is the first API in it.
- replace `localtime()` calls with `toolx_localtime()`.
Except in examples.
- note Windows XP's default `msvcrt.dll` doesn't offer secure CRT APIs.
XP likely needs a newer version of this DLL, or may not run.
- note that `localtime()` mirrors `gmtime()`, with the difference that
`gmtime()`'s internal wrapper lives in curlx.
Also:
- drop redundant `int` casts.
Refs:
https://learn.microsoft.com/cpp/c-runtime-library/reference/localtime-localtime32-localtime64
https://learn.microsoft.com/cpp/c-runtime-library/reference/localtime-s-localtime32-s-localtime64-s
https://pubs.opengroup.org/onlinepubs/9799919799/functions/localtime.html
https://linux.die.net/man/3/localtime_r
Ref: #19955 (for `gmtime_r()`)
Follow-up to 54d9f060b4
Closes #19957
This commit is contained in:
parent
c6988f9131
commit
32454b954a
23 changed files with 285 additions and 29 deletions
|
|
@ -64,6 +64,12 @@ CURLX_HFILES = \
|
|||
../lib/curlx/warnless.h \
|
||||
../lib/curlx/winapi.h
|
||||
|
||||
TOOLX_CFILES = \
|
||||
toolx/tool_time.c
|
||||
|
||||
TOOLX_HFILES = \
|
||||
toolx/tool_time.h
|
||||
|
||||
CURL_CFILES = \
|
||||
config2setopts.c \
|
||||
slist_wc.c \
|
||||
|
|
@ -107,7 +113,8 @@ CURL_CFILES = \
|
|||
tool_writeout.c \
|
||||
tool_writeout_json.c \
|
||||
tool_xattr.c \
|
||||
var.c
|
||||
var.c \
|
||||
$(TOOLX_CFILES)
|
||||
|
||||
CURL_HFILES = \
|
||||
config2setopts.h \
|
||||
|
|
@ -154,6 +161,7 @@ CURL_HFILES = \
|
|||
tool_writeout.h \
|
||||
tool_writeout_json.h \
|
||||
tool_xattr.h \
|
||||
var.h
|
||||
var.h \
|
||||
$(TOOLX_HFILES)
|
||||
|
||||
CURL_RCFILES = curl.rc
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@
|
|||
#include "tool_msgs.h"
|
||||
#include "tool_cb_dbg.h"
|
||||
#include "tool_util.h"
|
||||
#include "toolx/tool_time.h"
|
||||
|
||||
static void dump(const char *timebuf, const char *idsbuf, const char *text,
|
||||
FILE *stream, const unsigned char *ptr, size_t size,
|
||||
|
|
@ -34,7 +35,6 @@ static void dump(const char *timebuf, const char *idsbuf, const char *text,
|
|||
|
||||
/*
|
||||
* Return the formatted HH:MM:SS for the tv_sec given.
|
||||
* NOT thread safe.
|
||||
*/
|
||||
static const char *hms_for_sec(time_t tv_sec)
|
||||
{
|
||||
|
|
@ -42,10 +42,12 @@ static const char *hms_for_sec(time_t tv_sec)
|
|||
static char hms_buf[12];
|
||||
|
||||
if(tv_sec != cached_tv_sec) {
|
||||
/* !checksrc! disable BANNEDFUNC 1 */
|
||||
struct tm *now = localtime(&tv_sec); /* not thread safe either */
|
||||
struct tm now;
|
||||
CURLcode result = toolx_localtime(tv_sec, &now);
|
||||
if(result)
|
||||
memset(&now, 0, sizeof(now));
|
||||
curl_msnprintf(hms_buf, sizeof(hms_buf), "%02d:%02d:%02d",
|
||||
now->tm_hour, now->tm_min, now->tm_sec);
|
||||
now.tm_hour, now.tm_min, now.tm_sec);
|
||||
cached_tv_sec = tv_sec;
|
||||
}
|
||||
return hms_buf;
|
||||
|
|
|
|||
61
src/toolx/tool_time.c
Normal file
61
src/toolx/tool_time.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/***************************************************************************
|
||||
* _ _ ____ _
|
||||
* Project ___| | | | _ \| |
|
||||
* / __| | | | |_) | |
|
||||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
*
|
||||
* This software is licensed as described in the file COPYING, which
|
||||
* you should have received as part of this distribution. The terms
|
||||
* are also available at https://curl.se/docs/copyright.html.
|
||||
*
|
||||
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
||||
* copies of the Software, and permit persons to whom the Software is
|
||||
* furnished to do so, under the terms of the COPYING file.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
* SPDX-License-Identifier: curl
|
||||
*
|
||||
***************************************************************************/
|
||||
#include "tool_time.h"
|
||||
|
||||
#if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 3)
|
||||
#include <sec_api/time_s.h> /* for _localtime32_s(), _localtime64_s() */
|
||||
#ifdef _USE_32BIT_TIME_T
|
||||
#define localtime_s _localtime32_s
|
||||
#else
|
||||
#define localtime_s _localtime64_s
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* toolx_localtime() is a localtime() replacement for portability. Do not use
|
||||
* the localtime_s(), localtime_r() or localtime() functions anywhere else but
|
||||
* here.
|
||||
*/
|
||||
CURLcode toolx_localtime(time_t intime, struct tm *store)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if(localtime_s(store, &intime)) /* thread-safe */
|
||||
return CURLE_BAD_FUNCTION_ARGUMENT;
|
||||
#elif defined(HAVE_LOCALTIME_R)
|
||||
const struct tm *tm;
|
||||
tm = localtime_r(&intime, store); /* thread-safe */
|
||||
if(!tm)
|
||||
return CURLE_BAD_FUNCTION_ARGUMENT;
|
||||
#else
|
||||
const struct tm *tm;
|
||||
/* !checksrc! disable BANNEDFUNC 1 */
|
||||
tm = localtime(&intime); /* not thread-safe */
|
||||
if(tm)
|
||||
*store = *tm; /* copy the pointed struct to the local copy */
|
||||
else
|
||||
return CURLE_BAD_FUNCTION_ARGUMENT;
|
||||
#endif
|
||||
|
||||
return CURLE_OK;
|
||||
}
|
||||
30
src/toolx/tool_time.h
Normal file
30
src/toolx/tool_time.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef HEADER_TOOLX_TOOL_TIME_H
|
||||
#define HEADER_TOOLX_TOOL_TIME_H
|
||||
/***************************************************************************
|
||||
* _ _ ____ _
|
||||
* Project ___| | | | _ \| |
|
||||
* / __| | | | |_) | |
|
||||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
*
|
||||
* This software is licensed as described in the file COPYING, which
|
||||
* you should have received as part of this distribution. The terms
|
||||
* are also available at https://curl.se/docs/copyright.html.
|
||||
*
|
||||
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
||||
* copies of the Software, and permit persons to whom the Software is
|
||||
* furnished to do so, under the terms of the COPYING file.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
* SPDX-License-Identifier: curl
|
||||
*
|
||||
***************************************************************************/
|
||||
#include "curl_setup.h"
|
||||
|
||||
CURLcode toolx_localtime(time_t intime, struct tm *store);
|
||||
|
||||
#endif /* HEADER_TOOLX_TOOL_TIME_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue