1 /* Test of POSIX compatible fprintf() function.
2 Copyright (C) 2007, 2009-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
23 /* This test assumes getrlimit() and setrlimit().
24 With "gcc -fcheck-pointer-bounds -mmpx -static", it produces an
25 endless loop of "Saw a #BR!" messages. */
26 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && !defined __CHKP__
29 #include <sys/types.h>
31 #include <sys/resource.h>
36 main (int argc
, char *argv
[])
42 /* Some printf implementations allocate temporary space with malloc. */
43 /* On BSD systems, malloc() is limited by RLIMIT_DATA. */
45 if (getrlimit (RLIMIT_DATA
, &limit
) < 0)
47 if (limit
.rlim_max
== RLIM_INFINITY
|| limit
.rlim_max
> 10000000)
48 limit
.rlim_max
= 10000000;
49 limit
.rlim_cur
= limit
.rlim_max
;
50 if (setrlimit (RLIMIT_DATA
, &limit
) < 0)
53 /* On Linux systems, malloc() is limited by RLIMIT_AS. */
55 if (getrlimit (RLIMIT_AS
, &limit
) < 0)
57 if (limit
.rlim_max
== RLIM_INFINITY
|| limit
.rlim_max
> 10000000)
58 limit
.rlim_max
= 10000000;
59 limit
.rlim_cur
= limit
.rlim_max
;
60 if (setrlimit (RLIMIT_AS
, &limit
) < 0)
63 /* Some printf implementations allocate temporary space on the stack. */
65 if (getrlimit (RLIMIT_STACK
, &limit
) < 0)
67 if (limit
.rlim_max
== RLIM_INFINITY
|| limit
.rlim_max
> 10000000)
68 limit
.rlim_max
= 10000000;
69 limit
.rlim_cur
= limit
.rlim_max
;
70 if (setrlimit (RLIMIT_STACK
, &limit
) < 0)
79 void *memory
= malloc (10000000);
82 memset (memory
, 17, 10000000);
86 ret
= fprintf (stdout
, "%.10000000f", 1.0);
87 return !(ret
== 10000002 || (ret
< 0 && errno
== ENOMEM
));
89 ret
= fprintf (stdout
, "%.10000000f", -1.0);
90 return !(ret
== 10000003 || (ret
< 0 && errno
== ENOMEM
));
92 ret
= fprintf (stdout
, "%.10000000e", 1.0);
93 return !(ret
>= 10000006 || (ret
< 0 && errno
== ENOMEM
));
95 ret
= fprintf (stdout
, "%.10000000d", 1);
96 return !(ret
== 10000000 || (ret
< 0 && errno
== ENOMEM
));
98 ret
= fprintf (stdout
, "%.10000000d", -1);
99 return !(ret
== 10000001 || (ret
< 0 && errno
== ENOMEM
));
101 ret
= fprintf (stdout
, "%.10000000u", 1);
102 return !(ret
== 10000000 || (ret
< 0 && errno
== ENOMEM
));
110 main (int argc
, char *argv
[])