glibc/stdio-common/tst-vfprintf-width-prec.c
Joseph Myers a312e8fe6d Fix stdio-common tests for GCC 12 -Waddress
My glibc bot shows failures building the testsuite with GCC mainline
across all architectures:

tst-vfprintf-width-prec.c: In function 'do_test':
tst-vfprintf-width-prec.c:90:16: error: the comparison will always evaluate as 'false' for the address of 'result' will never be NULL [-Werror=address]
   90 |     if (result == NULL)
      |                ^~
tst-vfprintf-width-prec.c:89:13: note: 'result' declared here
   89 |     wchar_t result[100];
      |             ^~~~~~

This is clearly a correct warning; the comparison against NULL is
clearly a cut-and-paste mistake from an earlier case in the test that
does use calloc.  Thus, remove the unnecessary check for NULL shown up
by the warning.

Similarly, two other tests have bogus comparisons against NULL; remove
those as well:

scanf14a.c:95:13: error: the comparison will always evaluate as 'false' for the address of 'fname' will never be NULL [-Werror=address]
   95 |   if (fname == NULL)
      |             ^~
scanf14a.c:93:8: note: 'fname' declared here
   93 |   char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"];
      |        ^~~~~

scanf16a.c:125:13: error: the comparison will always evaluate as 'false' for the address of 'fname' will never be NULL [-Werror=address]
  125 |   if (fname == NULL)
      |             ^~
scanf16a.c:123:8: note: 'fname' declared here
  123 |   char fname[strlen (tmpdir) + sizeof "/tst-scanf16.XXXXXX"];
      |        ^~~~~

Tested with build-many-glibcs.py (GCC mainline) for aarch64-linux-gnu.
2021-10-04 19:10:43 +00:00

103 lines
2.6 KiB
C

/* Test for memory leak with large width and precision.
Copyright (C) 1991-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <wchar.h>
static int
do_test (void)
{
mtrace ();
int ret;
{
char *result;
ret = asprintf (&result, "%133000.133001x", 17);
if (ret < 0)
{
printf ("error: asprintf: %m\n");
return 1;
}
free (result);
}
{
wchar_t *result = calloc (ret + 1, sizeof (wchar_t));
if (result == NULL)
{
printf ("error: calloc (%d, %zu): %m", ret + 1, sizeof (wchar_t));
return 1;
}
ret = swprintf (result, ret + 1, L"%133000.133001x", 17);
if (ret < 0)
{
printf ("error: swprintf: %d (%m)\n", ret);
return 1;
}
free (result);
}
/* Limit the size of the process, so that the second allocation will
fail. */
{
struct rlimit limit;
if (getrlimit (RLIMIT_AS, &limit) != 0)
{
printf ("getrlimit (RLIMIT_AS) failed: %m\n");
return 1;
}
long target = 200 * 1024 * 1024;
if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
{
limit.rlim_cur = target;
if (setrlimit (RLIMIT_AS, &limit) != 0)
{
printf ("setrlimit (RLIMIT_AS) failed: %m\n");
return 1;
}
}
}
{
char *result;
ret = asprintf (&result, "%133000.999999999x", 17);
if (ret >= 0)
{
printf ("error: asprintf: incorrect result %d\n", ret);
return 1;
}
}
{
wchar_t result[100];
ret = swprintf (result, 100, L"%133000.999999999x", 17);
if (ret >= 0)
{
printf ("error: swprintf: incorrect result %d\n", ret);
return 1;
}
}
return 0;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"