Use a wrapper function to call strptime()

Encapsulate the feature requirements for strptime() in a
portability wrapper.

Use _GNU_SOURCE to expose strptime. It should be enough on glibc
without the side-effect of selecting a particular SUS version,
which we don't need and might hide other definitions.
This commit is contained in:
João Valverde 2021-12-27 02:03:26 +00:00
parent b83cefd2fe
commit 0d5bfd44a8
10 changed files with 43 additions and 90 deletions

View File

@ -87,7 +87,6 @@ check_function_exists("getifaddrs" HAVE_GETIFADDRS)
check_function_exists("issetugid" HAVE_ISSETUGID)
check_function_exists("setresgid" HAVE_SETRESGID)
check_function_exists("setresuid" HAVE_SETRESUID)
check_function_exists("strptime" HAVE_STRPTIME)
if (APPLE)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES ${APPLE_CORE_FOUNDATION_LIBRARY})
@ -99,6 +98,7 @@ if(UNIX)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists("memmem" "string.h" HAVE_MEMMEM)
check_symbol_exists("strcasestr" "string.h" HAVE_STRCASESTR)
check_symbol_exists("strptime" "time.h" HAVE_STRPTIME)
check_symbol_exists("vasprintf" "stdio.h" HAVE_VASPRINTF)
cmake_pop_check_state()
endif()

View File

@ -428,6 +428,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_regex_pattern@Base 3.7.0
ws_socket_ptoa@Base 3.1.1
ws_strcasestr@Base 3.7.0
ws_strptime@Base 3.7.0
ws_strtoi16@Base 2.3.0
ws_strtoi32@Base 2.3.0
ws_strtoi64@Base 2.3.0

View File

@ -23,16 +23,6 @@
#include <stdarg.h>
#include <math.h>
/*
* Just make sure we include the prototype for strptime as well
* (needed for glibc 2.2) but make sure we do this only if not
* yet defined.
*/
#ifndef __USE_XOPEN
# define __USE_XOPEN
#endif
#include <time.h>
#include <glib.h>
@ -53,10 +43,6 @@
#include <winsock2.h>
#endif
#ifndef HAVE_STRPTIME
# include "wsutil/strptime.h"
#endif
#include <ui/clopts_common.h>
#include <ui/cmdarg_err.h>
#include <ui/exit_codes.h>

View File

@ -7,28 +7,14 @@
*/
#include "config.h"
#include "ftypes-int.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Just make sure we include the prototype for strptime as well
* (needed for glibc 2.2) but make sure we do this only if not
* yet defined.
*/
#ifndef __USE_XOPEN
# define __USE_XOPEN
#endif
#include <time.h>
#include <ftypes-int.h>
#include <epan/to_str.h>
#ifndef HAVE_STRPTIME
#include "wsutil/strptime.h"
#endif
#include <wsutil/time_util.h>
static int
@ -204,20 +190,20 @@ absolute_val_from_string(fvalue_t *fv, const char *s, gchar **err_msg)
/* Do not use '%b' to parse the month name, it is locale-specific. */
if (s[3] == ' ' && parse_month_name(s, &tm.tm_mon))
curptr = strptime(s + 4, "%d, %Y %H:%M:%S", &tm);
curptr = ws_strptime(s + 4, "%d, %Y %H:%M:%S", &tm);
if (curptr == NULL)
curptr = strptime(s,"%Y-%m-%dT%H:%M:%S", &tm);
curptr = ws_strptime(s,"%Y-%m-%dT%H:%M:%S", &tm);
if (curptr == NULL)
curptr = strptime(s,"%Y-%m-%d %H:%M:%S", &tm);
curptr = ws_strptime(s,"%Y-%m-%d %H:%M:%S", &tm);
if (curptr == NULL) {
has_seconds = FALSE;
curptr = strptime(s,"%Y-%m-%d %H:%M", &tm);
curptr = ws_strptime(s,"%Y-%m-%d %H:%M", &tm);
}
if (curptr == NULL)
curptr = strptime(s,"%Y-%m-%d %H", &tm);
curptr = ws_strptime(s,"%Y-%m-%d %H", &tm);
if (curptr == NULL)
curptr = strptime(s,"%Y-%m-%d", &tm);
curptr = ws_strptime(s,"%Y-%m-%d", &tm);
if (curptr == NULL)
goto fail;
tm.tm_isdst = -1; /* let the computer figure out if it's DST */

View File

@ -75,38 +75,8 @@
*/
#include "config.h"
/*
* Just make sure we include the prototype for strptime as well
* (needed for glibc 2.2) but make sure we do this only if not
* yet defined.
*/
#ifndef __USE_XOPEN
# define __USE_XOPEN
#endif
#ifndef _XOPEN_SOURCE
# ifndef __sun
# define _XOPEN_SOURCE 600
# endif
#endif
#include "text_import.h"
/*
* Defining _XOPEN_SOURCE is needed on some platforms, e.g. platforms
* using glibc, to expand the set of things system header files define.
*
* Unfortunately, on other platforms, such as some versions of Solaris
* (including Solaris 10), it *reduces* that set as well, causing
* strptime() not to be declared, presumably because the version of the
* X/Open spec that _XOPEN_SOURCE implies doesn't include strptime() and
* blah blah blah namespace pollution blah blah blah.
*
* So we define __EXTENSIONS__ so that "strptime()" is declared.
*/
#ifndef __EXTENSIONS__
# define __EXTENSIONS__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -127,9 +97,7 @@
#include <wsutil/exported_pdu_tlvs.h>
#include <wsutil/nstime.h>
#ifndef HAVE_STRPTIME
# include "wsutil/strptime.h"
#endif
#include <wsutil/time_util.h>
#include "text_import_scanner.h"
#include "text_import_scanner_lex.h"
@ -1082,7 +1050,7 @@ _parse_time(const guchar* start_field, const guchar* end_field, const gchar* _fo
*subsecs_fmt = 0;
}
cursor = strptime(cursor, format, &timecode);
cursor = ws_strptime(cursor, format, &timecode);
if (cursor == NULL) {
return FALSE;
@ -1099,7 +1067,7 @@ _parse_time(const guchar* start_field, const guchar* end_field, const gchar* _fo
subseclen = (int) (p - cursor);
cursor = p;
cursor = strptime(cursor, subsecs_fmt + 2, &timecode);
cursor = ws_strptime(cursor, subsecs_fmt + 2, &timecode);
if (cursor == NULL) {
return FALSE;
}

View File

@ -17,10 +17,6 @@
#include "epochs.h"
#include "time_util.h"
#ifndef HAVE_STRPTIME
# include "wsutil/strptime.h"
#endif
/* this is #defined so that we can clearly see that we have the right number of
zeros, rather than as a guard against the number of nanoseconds in a second
changing ;) */
@ -539,7 +535,7 @@ unix_epoch_to_nstime(nstime_t *nstime, const char *ptr)
tm.tm_isdst = -1;
nstime_set_unset(nstime);
if (!(ptr_new=strptime(ptr, "%s", &tm))) {
if (!(ptr_new = ws_strptime(ptr, "%s", &tm))) {
return 0;
}

View File

@ -12,6 +12,7 @@
this is enough information for determining the date. */
#include "config.h"
#include "strptime.h"
#include <ctype.h>
#include <string.h>
@ -21,8 +22,6 @@
# include "../locale/localeinfo.h"
#endif
#include "strptime.h"
#ifndef __P
# if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
# define __P(args) args
@ -974,7 +973,7 @@ strptime_internal (rp, fmt, tm, decided, era_cnt)
char *
strptime (buf, format, tm)
strptime_gnulib (buf, format, tm)
const char *buf;
const char *format;
struct tm *tm;

View File

@ -10,11 +10,13 @@
#ifndef __STRPTIME_H__
#define __STRPTIME_H__
#include "ws_symbol_export.h"
#include <ws_symbol_export.h>
#include <time.h>
/*
* Version of "strptime()", for the benefit of OSes that don't have it.
*/
WS_DLL_PUBLIC char *strptime(const char *, const char *, struct tm *);
WS_DLL_LOCAL
char *strptime_gnulib(const char *s, const char *format, struct tm *tm);
#endif

View File

@ -7,16 +7,12 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#define _GNU_SOURCE /* For strptime(). */
#include "config.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
#include <glib.h>
#include "time_util.h"
#include <wsutil/epochs.h>
#include <wsutil/wslog.h>
#include "time_util.h"
#ifndef _WIN32
#include <sys/time.h>
@ -25,6 +21,10 @@
#include <windows.h>
#endif
#ifndef HAVE_STRPTIME
#include "strptime.h"
#endif
/* Test if the given year is a leap year */
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
@ -240,6 +240,16 @@ ws_clock_get_realtime(struct timespec *ts)
#endif
}
char *ws_strptime(const char *restrict s, const char *restrict format,
struct tm *restrict tm)
{
#ifdef HAVE_STRPTIME
return strptime(s, format, tm);
#else
return strptime_gnulib(s, format, tm);
#endif
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*

View File

@ -10,8 +10,7 @@
#ifndef __TIME_UTIL_H__
#define __TIME_UTIL_H__
#include "ws_symbol_export.h"
#include <wireshark.h>
#include <time.h>
#ifdef __cplusplus
@ -66,6 +65,12 @@ guint64 create_timestamp(void);
WS_DLL_PUBLIC
struct timespec *ws_clock_get_realtime(struct timespec *ts);
/*
* Portability wrapper around strptime().
*/
WS_DLL_PUBLIC
char *ws_strptime(const char *s, const char *format, struct tm *tm);
#ifdef __cplusplus
}
#endif /* __cplusplus */