manual: document snprintf truncation better
[glibc.git] / stdio-common / tst-vfprintf-width-prec.c
blob5dcdc561ca6d2c37359bc7245f6a41599e765a19
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.
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.
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.
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/>. */
19 #include <mcheck.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/resource.h>
23 #include <wchar.h>
25 static int
26 do_test (void)
28 mtrace ();
30 int ret;
32 char *result;
33 ret = asprintf (&result, "%133000.133001x", 17);
34 if (ret < 0)
36 printf ("error: asprintf: %m\n");
37 return 1;
39 free (result);
42 wchar_t *result = calloc (ret + 1, sizeof (wchar_t));
43 if (result == NULL)
45 printf ("error: calloc (%d, %zu): %m", ret + 1, sizeof (wchar_t));
46 return 1;
49 ret = swprintf (result, ret + 1, L"%133000.133001x", 17);
50 if (ret < 0)
52 printf ("error: swprintf: %d (%m)\n", ret);
53 return 1;
55 free (result);
58 /* Limit the size of the process, so that the second allocation will
59 fail. */
61 struct rlimit limit;
62 if (getrlimit (RLIMIT_AS, &limit) != 0)
64 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
65 return 1;
67 long target = 200 * 1024 * 1024;
68 if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
70 limit.rlim_cur = target;
71 if (setrlimit (RLIMIT_AS, &limit) != 0)
73 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
74 return 1;
80 char *result;
81 ret = asprintf (&result, "%133000.999999999x", 17);
82 if (ret >= 0)
84 printf ("error: asprintf: incorrect result %d\n", ret);
85 return 1;
89 wchar_t result[100];
90 ret = swprintf (result, 100, L"%133000.999999999x", 17);
91 if (ret >= 0)
93 printf ("error: swprintf: incorrect result %d\n", ret);
94 return 1;
98 return 0;
101 #define TEST_FUNCTION do_test ()
102 #include "../test-skeleton.c"