From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 152EC1F8C8 for ; Thu, 9 Sep 2021 02:26:53 +0000 (UTC) Received: from localhost ([::1]:55214 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mO9m7-0008Ar-N7 for normalperson@yhbt.net; Wed, 08 Sep 2021 22:26:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33202) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mO9m4-0008Ai-JY for bug-gnulib@gnu.org; Wed, 08 Sep 2021 22:26:48 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:59646) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mO9m2-0000li-2k for bug-gnulib@gnu.org; Wed, 08 Sep 2021 22:26:48 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 00AC516014B; Wed, 8 Sep 2021 19:26:43 -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 T6Fgrkr3zkhT; Wed, 8 Sep 2021 19:26:41 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id C28FB160149; Wed, 8 Sep 2021 19:26:41 -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 X9Znklo7Fejy; Wed, 8 Sep 2021 19:26:41 -0700 (PDT) Received: from day.example.com (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 96D1C1600E5; Wed, 8 Sep 2021 19:26:41 -0700 (PDT) From: Paul Eggert To: bug-gnulib@gnu.org, landfillbaby69@gmail.com Subject: [PATCH] strerror_r-posix: port even better to Android Date: Wed, 8 Sep 2021 19:26:34 -0700 Message-Id: <20210909022634.387656-1-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=131.179.128.68; envelope-from=eggert@cs.ucla.edu; helo=zimbra.cs.ucla.edu X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paul Eggert Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" * lib/strerror_r.c: Use STRERROR_R_CHAR_P to decide whether the system strerror_r returns char *, and HAVE_DECL_STRERROR_R to decide whether it either does that or returns an integer. In the former case, use the system strerror_r even on platforms like Android API level 23 that don=E2=80=99t have __xpg_strerror_r; also check for strerror_r failure just in case. * m4/error.m4 (gl_PREREQ_ERROR): * m4/strerror_r.m4 (gl_PREREQ_STRERROR_R): Use system extensions on Android, to avoid mishandling strerror_r on API level 23 and later. * modules/error, modules/strerror_r-posix (configure.ac): Surround gl_PREREQ_ERROR with AS_IF instead of plain if, so that AC_REQUIREs are propagated out. --- ChangeLog | 15 +++++++++++++++ lib/strerror_r.c | 35 +++++++++++++++++------------------ m4/error.m4 | 6 +++++- m4/strerror_r.m4 | 3 ++- modules/error | 4 ++-- modules/strerror_r-posix | 4 ++-- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1db7e333b..7c56ed2a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2021-09-08 Paul Eggert =20 + strerror_r-posix: port even better to Android + * lib/strerror_r.c: Use STRERROR_R_CHAR_P to decide whether the + system strerror_r returns char *, and HAVE_DECL_STRERROR_R to + decide whether it either does that or returns an integer. In the + former case, use the system strerror_r even on platforms like + Android API level 23 that don=E2=80=99t have __xpg_strerror_r; also + check for strerror_r failure just in case. + * m4/error.m4 (gl_PREREQ_ERROR): + * m4/strerror_r.m4 (gl_PREREQ_STRERROR_R): + Use system extensions on Android, to avoid mishandling strerror_r + on API level 23 and later. + * modules/error, modules/strerror_r-posix (configure.ac): + Surround gl_PREREQ_ERROR with AS_IF instead of plain if, so that + AC_REQUIREs are propagated out. + strerror_r-posix: port better to Android * m4/strerror_r.m4 (gl_FUNC_STRERROR_R): Move requirement of gl_USE_SYSTEM_EXTENSIONS from here ... diff --git a/lib/strerror_r.c b/lib/strerror_r.c index f0244520b..90043c223 100644 --- a/lib/strerror_r.c +++ b/lib/strerror_r.c @@ -34,33 +34,26 @@ =20 #include "strerror-override.h" =20 -#if (__GLIBC__ >=3D 2 || defined __UCLIBC__ || defined __CYGWIN__) && HA= VE___XPG_STRERROR_R /* glibc >=3D 2.3.4, cygwin >=3D 1.7.9 */ +#if STRERROR_R_CHAR_P =20 -# define USE_XPG_STRERROR_R 1 -extern -#ifdef __cplusplus -"C" -#endif -int __xpg_strerror_r (int errnum, char *buf, size_t buflen); +# if HAVE___XPG_STRERROR_R +_GL_EXTERN_C int __xpg_strerror_r (int errnum, char *buf, size_t buflen)= ; +# endif =20 -#elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >=3D 2 || defined __UCLIBC__ |= | defined __CYGWIN__) +#elif HAVE_DECL_STRERROR_R =20 -/* The system's strerror_r function is OK, except that its third argumen= t +/* The system's strerror_r function's API is OK, except that its third a= rgument is 'int', not 'size_t', or its return type is wrong. */ =20 # include =20 -# define USE_SYSTEM_STRERROR_R 1 - -#else /* (__GLIBC__ >=3D 2 || defined __UCLIBC__ || defined __CYGWIN__ ?= !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */ +#else =20 /* Use the system's strerror(). Exclude glibc and cygwin because the system strerror_r has the wrong return type, and cygwin 1.7.9 strerror_r clobbers strerror. */ # undef strerror =20 -# define USE_SYSTEM_STRERROR 1 - # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined= __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || def= ined __CYGWIN__ =20 /* No locking needed. */ @@ -166,22 +159,28 @@ strerror_r (int errnum, char *buf, size_t buflen) int ret; int saved_errno =3D errno; =20 -#if USE_XPG_STRERROR_R +#if STRERROR_R_CHAR_P =20 { + ret =3D 0; + +# if HAVE___XPG_STRERROR_R ret =3D __xpg_strerror_r (errnum, buf, buflen); if (ret < 0) ret =3D errno; +# endif + if (!*buf) { /* glibc 2.13 would not touch buf on err, so we have to fall back to GNU strerror_r which always returns a thread-safe untruncated string to (partially) copy into our buf. */ - safe_copy (buf, buflen, strerror_r (errnum, buf, buflen)); + char *errstring =3D strerror_r (errnum, buf, buflen); + ret =3D errstring ? safe_copy (buf, buflen, errstring) : errno= ; } } =20 -#elif USE_SYSTEM_STRERROR_R +#elif HAVE_DECL_STRERROR_R =20 if (buflen > INT_MAX) buflen =3D INT_MAX; @@ -245,7 +244,7 @@ strerror_r (int errnum, char *buf, size_t buflen) } # endif =20 -#else /* USE_SYSTEM_STRERROR */ +#else /* strerror_r is not declared. */ =20 /* Try to do what strerror (errnum) does, but without clobbering the buffer used by strerror(). */ diff --git a/m4/error.m4 b/m4/error.m4 index 77f67f78b..8e3cf95d9 100644 --- a/m4/error.m4 +++ b/m4/error.m4 @@ -1,4 +1,4 @@ -#serial 14 +#serial 15 =20 # Copyright (C) 1996-1998, 2001-2004, 2009-2021 Free Software Foundation= , Inc. # @@ -22,6 +22,10 @@ AC_DEFUN([gl_ERROR], # Prerequisites of lib/error.c. AC_DEFUN([gl_PREREQ_ERROR], [ + dnl Use system extensions on Android, so that AC_FUNC_STRERROR_R + dnl discovers the GNU API for strerror_r on Android API level 23 and l= ater. + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + AC_REQUIRE([AC_FUNC_STRERROR_R]) : ]) diff --git a/m4/strerror_r.m4 b/m4/strerror_r.m4 index 29b309221..bc40ec294 100644 --- a/m4/strerror_r.m4 +++ b/m4/strerror_r.m4 @@ -1,4 +1,4 @@ -# strerror_r.m4 serial 22 +# strerror_r.m4 serial 23 dnl Copyright (C) 2002, 2007-2021 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -37,6 +37,7 @@ AC_DEFUN([gl_FUNC_STRERROR_R], =20 # Prerequisites of lib/strerror_r.c. AC_DEFUN([gl_PREREQ_STRERROR_R], [ + AC_REQUIRE([AC_FUNC_STRERROR_R]) dnl glibc >=3D 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r= . AC_CHECK_FUNCS_ONCE([__xpg_strerror_r]) AC_CHECK_FUNCS_ONCE([catgets]) diff --git a/modules/error b/modules/error index 2945c4845..08370da77 100644 --- a/modules/error +++ b/modules/error @@ -15,10 +15,10 @@ msvc-nothrow [test $ac_cv_lib_error_at_line =3D no= ] =20 configure.ac: gl_ERROR -if test $ac_cv_lib_error_at_line =3D no; then +AS_IF([test "$ac_cv_lib_error_at_line" =3D no], [ AC_LIBOBJ([error]) gl_PREREQ_ERROR -fi +]) m4_ifdef([AM_XGETTEXT_OPTION], [AM_][XGETTEXT_OPTION([--flag=3Derror:3:c-format]) AM_][XGETTEXT_OPTION([--flag=3Derror_at_line:5:c-format])]) diff --git a/modules/strerror_r-posix b/modules/strerror_r-posix index f91bc0e7c..25f049365 100644 --- a/modules/strerror_r-posix +++ b/modules/strerror_r-posix @@ -14,10 +14,10 @@ strerror-override [test $HAVE_DECL_STRERROR_R =3D 0 |= | test $REPLACE_STRERROR_R =3D =20 configure.ac: gl_FUNC_STRERROR_R -if test $HAVE_DECL_STRERROR_R =3D 0 || test $REPLACE_STRERROR_R =3D 1; t= hen +AS_IF([test $HAVE_DECL_STRERROR_R =3D 0 || test $REPLACE_STRERROR_R =3D = 1], [ AC_LIBOBJ([strerror_r]) gl_PREREQ_STRERROR_R -fi +]) gl_STRING_MODULE_INDICATOR([strerror_r]) dnl For the modules argp, error. gl_MODULE_INDICATOR([strerror_r-posix]) --=20 2.30.2