]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tst-vfprintf-width-prec.c
nptl: Add tst-robust-fork
[thirdparty/glibc.git] / stdio-common / tst-vfprintf-width-prec.c
CommitLineData
fdcf1c94 1/* Test for memory leak with large width and precision.
bfff8b1b 2 Copyright (C) 1991-2017 Free Software Foundation, Inc.
fdcf1c94
FW
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 <http://www.gnu.org/licenses/>. */
18
19#include <mcheck.h>
20#include <stdio.h>
21#include <sys/resource.h>
22#include <wchar.h>
23
24static int
25do_test (void)
26{
27 mtrace ();
28
29 int ret;
30 {
31 char *result;
32 ret = asprintf (&result, "%133000.133001x", 17);
33 if (ret < 0)
34 {
35 printf ("error: asprintf: %m\n");
36 return 1;
37 }
38 free (result);
39 }
40 {
41 wchar_t *result = calloc (ret + 1, sizeof (wchar_t));
42 if (result == NULL)
43 {
44 printf ("error: calloc (%d, %zu): %m", ret + 1, sizeof (wchar_t));
45 return 1;
46 }
47
48 ret = swprintf (result, ret + 1, L"%133000.133001x", 17);
49 if (ret < 0)
50 {
51 printf ("error: swprintf: %d (%m)\n", ret);
52 return 1;
53 }
54 free (result);
55 }
56
57 /* Limit the size of the process, so that the second allocation will
58 fail. */
59 {
60 struct rlimit limit;
61 if (getrlimit (RLIMIT_AS, &limit) != 0)
62 {
63 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
64 return 1;
65 }
66 long target = 200 * 1024 * 1024;
67 if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
68 {
69 limit.rlim_cur = target;
70 if (setrlimit (RLIMIT_AS, &limit) != 0)
71 {
72 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
73 return 1;
74 }
75 }
76 }
77
78 {
79 char *result;
80 ret = asprintf (&result, "%133000.999999999x", 17);
81 if (ret >= 0)
82 {
83 printf ("error: asprintf: incorrect result %d\n", ret);
84 return 1;
85 }
86 }
87 {
88 wchar_t result[100];
89 if (result == NULL)
90 {
91 printf ("error: calloc (%d, %zu): %m", ret + 1, sizeof (wchar_t));
92 return 1;
93 }
94
95 ret = swprintf (result, 100, L"%133000.999999999x", 17);
96 if (ret >= 0)
97 {
98 printf ("error: swprintf: incorrect result %d\n", ret);
99 return 1;
100 }
101 }
102
103 return 0;
104}
105
106#define TEST_FUNCTION do_test ()
107#include "../test-skeleton.c"