[PATCH 05/10] Return EXIT_UNSUPPORTED if __builtin_add_overflow unavailable

H.J. Lu hjl.tools@gmail.com
Sat Dec 14 12:14:32 GMT 2024


On Sat, Dec 14, 2024 at 5:38 PM Sam James <sam@gentoo.org> wrote:
>
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
> > Since GCC 4.9 doesn't have __builtin_add_overflow:
> >
> > In file included from tst-stringtable.c:180:0:
> > stringtable.c: In function ‘stringtable_finalize’:
> > stringtable.c:185:7: error: implicit declaration of function ‘__builtin_add_overflow’ [-Werror=implicit-function-declaration]
> >        else if (__builtin_add_overflow (previous->offset,
> >        ^
> >
> > return EXIT_UNSUPPORTED for GCC 4.9 or older.
> >
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > ---
> >  elf/tst-stringtable.c                    | 10 ++++++++++
> >  io/tst-utimensat-skeleton.c              |  5 +++++
> >  sysdeps/unix/sysv/linux/tst-getdents64.c |  7 +++++++
> >  time/tst-ctime.c                         |  5 +++++
> >  time/tst-difftime.c                      |  7 ++++++-
> >  time/tst-mktime4.c                       |  8 +++++++-
> >  6 files changed, 40 insertions(+), 2 deletions(-)
> >
> > diff --git a/elf/tst-stringtable.c b/elf/tst-stringtable.c
> > index e18496f3f1..a0e358f02a 100644
> > --- a/elf/tst-stringtable.c
> > +++ b/elf/tst-stringtable.c
> > @@ -17,6 +17,7 @@
> >
> >  #include <array_length.h>
> >  #include <stdlib.h>
> > +#if __GNUC_PREREQ (5, 0)
> >  #include <string.h>
> >  #include <stringtable.h>
> >  #include <support/check.h>
> > @@ -179,3 +180,12 @@ do_test (void)
> >  #define _(arg) arg
> >  #include "stringtable.c"
> >  #include "stringtable_free.c"
> > +#else
> > +#include <support/test-driver.h>
> > +
> > +int
> > +main (void)
> > +{
> > +  return EXIT_UNSUPPORTED;
> > +}
> > +#endif
> > diff --git a/io/tst-utimensat-skeleton.c b/io/tst-utimensat-skeleton.c
> > index 290ed81c9b..1386fa3c8f 100644
> > --- a/io/tst-utimensat-skeleton.c
> > +++ b/io/tst-utimensat-skeleton.c
> > @@ -20,6 +20,7 @@
> >  #include <inttypes.h>
> >  #include <support/support.h>
> >  #include <support/temp_file.h>
> > +#include <support/test-driver.h>
> >  #include <stdio.h>
> >
> >  static int temp_fd = -1;
> > @@ -72,6 +73,7 @@ do_test (void)
> >    for (int i = 0; i < array_length (tests); i++)
> >      {
> >        /* Check if we run on port with 32 bit time_t size.  */
> > +#if __GNUC_PREREQ (5, 0)
> >        time_t t;
> >        if (__builtin_add_overflow (tests[i].v1, 0, &t)
> >         || __builtin_add_overflow (tests[i].v2, 0, &t))
> > @@ -80,6 +82,9 @@ do_test (void)
> >                 "time_t overflows\n", i, tests[i].v1, tests[i].v2);
> >         continue;
> >          }
> > +#else
> > +      return EXIT_UNSUPPORTED;
> > +#endif
> >
> >        if (tests[i].v1 >= 0x100000000LL && !y2106)
> >       {
> > diff --git a/sysdeps/unix/sysv/linux/tst-getdents64.c b/sysdeps/unix/sysv/linux/tst-getdents64.c
> > index 3dd22a4e03..ba66d49a2e 100644
> > --- a/sysdeps/unix/sysv/linux/tst-getdents64.c
> > +++ b/sysdeps/unix/sysv/linux/tst-getdents64.c
> > @@ -30,6 +30,7 @@
> >  #include <sys/mman.h>
> >  #include <unistd.h>
> >
> > +#if __GNUC_PREREQ (5, 0)
> >  /* Called by large_buffer_checks below.  */
> >  static void
> >  large_buffer_check (int fd, char *large_buffer, size_t large_buffer_size)
> > @@ -85,6 +86,12 @@ do_test_large_size (void)
> >
> >    xclose (fd);
> >  }
> > +#else
> > +static void
> > +do_test_large_size (void)
> > +{
> > +}
> > +#endif
> >
> >  static void
> >  do_test_by_size (size_t buffer_size)
> > diff --git a/time/tst-ctime.c b/time/tst-ctime.c
> > index 8debf15004..f997a93801 100644
> > --- a/time/tst-ctime.c
> > +++ b/time/tst-ctime.c
> > @@ -19,6 +19,7 @@
> >  #include <time.h>
> >  #include <stdlib.h>
> >  #include <support/check.h>
> > +#include <support/test-driver.h>
> >
> >  static int
> >  do_test (void)
> > @@ -54,8 +55,12 @@ do_test (void)
> >
> >    /* Check if we run on port with 32 bit time_t size */
> >    time_t tov;
> > +#if __GNUC_PREREQ (5, 0)
> >    if (__builtin_add_overflow (t, 1, &tov))
> >      return 0;
> > +#else
> > +  return EXIT_UNSUPPORTED;
> > +#endif
> >
> >    /* Check if the time is converted after 32 bit time_t overflow.  */
> >    str = ctime (&tov);
> > diff --git a/time/tst-difftime.c b/time/tst-difftime.c
> > index 5043d55087..94723aa906 100644
> > --- a/time/tst-difftime.c
> > +++ b/time/tst-difftime.c
> > @@ -18,6 +18,7 @@
> >
> >  #include <time.h>
> >  #include <support/check.h>
> > +#include <support/test-driver.h>
> >
> >  static void
> >  test_difftime_helper (time_t t1, time_t t0, double exp_val)
> > @@ -37,10 +38,14 @@ do_test (void)
> >    test_difftime_helper (t - 1800, t + 1800, -3600.0);
> >
> >    t = 0x7FFFFFFF;
> > -  /* Check if we run on port with 32 bit time_t size */
> >    time_t tov;
> > +#if __GNUC_PREREQ (5, 0)
> > +  /* Check if we run on port with 32 bit time_t size */
> >    if (__builtin_add_overflow (t, 1, &tov))
> >      return 0;
> > +#else
> > +  return EXIT_UNSUPPORTED;
> > +#endif
> >
> >    /* Check if the time is converted after 32 bit time_t overflow.  */
> >    test_difftime_helper (t + 1800, t - 1800, 3600.0);
> > diff --git a/time/tst-mktime4.c b/time/tst-mktime4.c
> > index 505d9fcc2e..50d3acd375 100644
> > --- a/time/tst-mktime4.c
> > +++ b/time/tst-mktime4.c
> > @@ -19,6 +19,7 @@
> >  #include <time.h>
> >  #include <stdlib.h>
> >  #include <support/check.h>
> > +#include <support/test-driver.h>
> >
> >  const struct tm tm0 =
> >    {
> > @@ -55,11 +56,16 @@ const struct tm tm32bitmax =
> >  static
> >  int test_mktime_helper (struct tm *tm, long long int exp_val, int line)
> >  {
> > -  time_t result, t;
> > +  time_t result;
> >
> > +#if __GNUC_PREREQ (5, 0)
> >    /* Check if we run on port with 32 bit time_t size.  */
> > +  time_t t;
> >    if (__builtin_add_overflow (exp_val, 0, &t))
> >      return 0;
> > +#else
> > +  return EXIT_UNSUPPORTED;
> > +#endif
> >
> >    result = mktime (tm);
> >    if (result == (time_t) -1)
>
> Can you do early exit instead please?
>
> #if ! ...
>    return ...
> #endif

Changed in v2.   __builtin_add_overflow must be inside of

#if __GNUC_PREREQ (5, 0)
#endif

since it is unavailable before GCC 5.

Thanks.

-- 
H.J.


More information about the Libc-alpha mailing list