(root)/
glibc-2.38/
stdio-common/
tst-vfprintf-width-prec.c
       1  /* Test for memory leak with large width and precision.
       2     Copyright (C) 1991-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <mcheck.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <sys/resource.h>
      23  #include <wchar.h>
      24  
      25  static int
      26  do_test (void)
      27  {
      28    mtrace ();
      29  
      30    int ret;
      31    {
      32      char *result;
      33      ret = asprintf (&result, "%133000.133001x", 17);
      34      if (ret < 0)
      35        {
      36          printf ("error: asprintf: %m\n");
      37          return 1;
      38        }
      39      free (result);
      40    }
      41    {
      42      wchar_t *result = calloc (ret + 1, sizeof (wchar_t));
      43      if (result == NULL)
      44        {
      45          printf ("error: calloc (%d, %zu): %m", ret + 1, sizeof (wchar_t));
      46          return 1;
      47        }
      48  
      49      ret = swprintf (result, ret + 1, L"%133000.133001x", 17);
      50      if (ret < 0)
      51        {
      52          printf ("error: swprintf: %d (%m)\n", ret);
      53          return 1;
      54        }
      55      free (result);
      56    }
      57  
      58    /* Limit the size of the process, so that the second allocation will
      59       fail.  */
      60    {
      61      struct rlimit limit;
      62      if (getrlimit (RLIMIT_AS, &limit) != 0)
      63        {
      64          printf ("getrlimit (RLIMIT_AS) failed: %m\n");
      65          return 1;
      66        }
      67      long target = 200 * 1024 * 1024;
      68      if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
      69        {
      70          limit.rlim_cur = target;
      71          if (setrlimit (RLIMIT_AS, &limit) != 0)
      72            {
      73              printf ("setrlimit (RLIMIT_AS) failed: %m\n");
      74              return 1;
      75            }
      76        }
      77    }
      78  
      79    {
      80      char *result;
      81      ret = asprintf (&result, "%133000.999999999x", 17);
      82      if (ret >= 0)
      83        {
      84          printf ("error: asprintf: incorrect result %d\n", ret);
      85          return 1;
      86        }
      87    }
      88    {
      89      wchar_t result[100];
      90      ret = swprintf (result, 100, L"%133000.999999999x", 17);
      91      if (ret >= 0)
      92        {
      93          printf ("error: swprintf: incorrect result %d\n", ret);
      94          return 1;
      95        }
      96    }
      97  
      98    return 0;
      99  }
     100  
     101  #define TEST_FUNCTION do_test ()
     102  #include "../test-skeleton.c"