Skip to content

Commit

Permalink
Improve portability (#237)
Browse files Browse the repository at this point in the history
* Fix C89 compliance (mixed declarations and code)

* Fix C89 compliance (C++ style comments)

* Add strdup() in case it isn't available

* Add missing time.h to tools/psl.c

* Add localtime_r() declaration in case it isn't available

* meson: Check for strdup
  • Loading branch information
rockdaboot authored Feb 11, 2024
1 parent e8ef87a commit fe13dad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ AC_ARG_WITH(psl-testfile,
PSL_TESTFILE="\$(top_srcdir)/list/tests/tests.txt")
AC_SUBST(PSL_TESTFILE)

AC_CHECK_FUNCS([strndup clock_gettime fmemopen nl_langinfo])
AC_CHECK_FUNCS([strdup strndup clock_gettime fmemopen nl_langinfo])
AC_CHECK_DECLS([strdup])
AC_CHECK_DECLS([localtime_r])

# check for dirent.h
AC_HEADER_DIRENT
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ config.set('ENABLE_BUILTIN', enable_builtin)
config.set('HAVE_UNISTD_H', cc.check_header('unistd.h'))
config.set('HAVE_STDINT_H', cc.check_header('stdint.h'))
config.set('HAVE_DIRENT_H', cc.check_header('dirent.h'))
config.set('HAVE_STRNDUP', cc.has_function('strndup'))
config.set('HAVE_STRDUP', cc.has_function('strdup'))
config.set('HAVE_CLOCK_GETTIME', cc.has_function('clock_gettime'))
config.set('HAVE_FMEMOPEN', cc.has_function('fmemopen'))
config.set('HAVE_NL_LANGINFO', cc.has_function('nl_langinfo'))
Expand Down
27 changes: 25 additions & 2 deletions src/psl.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,23 @@ static int suffix_init(psl_entry_t *suffix, const char *rule, size_t length)
return 0;
}

#ifndef HAVE_STRDUP
static char *strdup(const char *s)
{
char *p = malloc(strlen(s) + 1);
if (!p)
return NULL;
return strcpy(p, s);
}
#elif !HAVE_DECL_STRDUP
/*
* On Linux with
* CC=gcc CFLAGS="-Wall -Wextra -Wpedantic -std=c89" ./configure
* strdup isn't declared (warning: implicit declaration of function 'strdup').
*/
char *strdup(const char *);
#endif

#if !defined(WITH_LIBIDN) && !defined(WITH_LIBIDN2) && !defined(WITH_LIBICU)
/*
* When configured without runtime IDNA support (./configure --disable-runtime), we need a pure ASCII
Expand Down Expand Up @@ -675,9 +692,9 @@ static int psl_idna_toASCII(psl_idna_t *idna, const char *utf8, char **ascii)
{
int ret = -1;

#if defined(WITH_LIBICU)
(void) idna;

#if defined(WITH_LIBICU)
/* IDNA2008 UTS#46 punycode conversion */
if (idna) {
char lookupname_buf[128] = "", *lookupname = lookupname_buf;
Expand Down Expand Up @@ -737,6 +754,8 @@ static int psl_idna_toASCII(psl_idna_t *idna, const char *utf8, char **ascii)
#if IDN2_VERSION_NUMBER >= 0x00140000
int rc;

(void) idna;

/* IDN2_TRANSITIONAL automatically converts to lowercase
* IDN2_NFC_INPUT converts to NFC before toASCII conversion
* Since IDN2_TRANSITIONAL implicitly does NFC conversion, we don't need
Expand Down Expand Up @@ -769,6 +788,8 @@ static int psl_idna_toASCII(psl_idna_t *idna, const char *utf8, char **ascii)
#elif defined(WITH_LIBIDN)
int rc;

(void) idna;

if (!utf8_is_valid(utf8)) {
/* fprintf(stderr, "Invalid UTF-8 sequence not converted: '%s'\n", utf8); */
return -1;
Expand All @@ -783,6 +804,8 @@ static int psl_idna_toASCII(psl_idna_t *idna, const char *utf8, char **ascii)
#else
char lookupname[128];

(void) idna;

if (domain_to_punycode(utf8, lookupname, sizeof(lookupname)) == 0) {
if (ascii)
if ((*ascii = strdup(lookupname)))
Expand Down Expand Up @@ -835,7 +858,7 @@ static int is_public_suffix(const psl_ctx_t *psl, const char *domain, int type)

for (p = domain; *p; p++) {
if (*p == '.') {
if (suffix.nlabels == 255) // weird input, avoid 8bit overflow
if (suffix.nlabels == 255) /* weird input, avoid 8bit overflow */
return 0;
suffix.nlabels++;
}
Expand Down
9 changes: 7 additions & 2 deletions tools/psl.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@

// Windows does not have localtime_r but has localtime_s, which is more or less
// the same except that the arguments are reversed
# define LOCALTIME_R_SUCCESSFUL(t_sec,t_now) \
# define LOCALTIME_R_SUCCESSFUL(t_sec, t_now) \
(localtime_s(t_now, t_sec) == 0)
#else
# define LOCALTIME_R_SUCCESSFUL(t_sec,t_now) \
# include <time.h>
# if ! HAVE_DECL_LOCALTIME_R
struct tm *localtime_r(const time_t *, struct tm *);
#endif

# define LOCALTIME_R_SUCCESSFUL(t_sec, t_now) \
(localtime_r(t_sec, t_now) != NULL)
#endif

Expand Down

0 comments on commit fe13dad

Please sign in to comment.