annotate tests/test-dprintf-posix2.c @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of POSIX compatible dprintf() function.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <stdio.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdlib.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <string.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <unistd.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <errno.h>
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
28 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
29 # include <sys/types.h>
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
30 # include <sys/time.h>
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
31 # include <sys/resource.h>
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 #endif
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
33
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
34 #include "resource-ext.h"
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* Test against a memory leak in the fprintf replacement. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* Number of iterations across the loop. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #define NUM_ROUNDS 1000
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 /* Number of bytes that are allowed to escape per round. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #define MAX_ALLOC_ROUND 10000
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Number of bytes that are allowed to escape in total.
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 This should be at least 10 MB, since it includes the normal memory
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 or address space of the test program. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND)
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 int
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 main (int argc, char *argv[])
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 {
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
52 uintptr_t initial_rusage_as;
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 int arg;
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
54 int result;
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
14230
a09aeba402dc Enable memory leak tests on AIX.
Bruno Haible <bruno@clisp.org>
parents: 14179
diff changeset
58 /* On AIX systems, malloc() is limited by RLIMIT_DATA. */
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
59 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
60 {
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
61 struct rlimit limit;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
62
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
63 if (getrlimit (RLIMIT_DATA, &limit) >= 0)
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
64 {
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
65 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL)
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
66 limit.rlim_max = MAX_ALLOC_TOTAL;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
67 limit.rlim_cur = limit.rlim_max;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
68 (void) setrlimit (RLIMIT_DATA, &limit);
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
69 }
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
70 }
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #endif
14230
a09aeba402dc Enable memory leak tests on AIX.
Bruno Haible <bruno@clisp.org>
parents: 14179
diff changeset
72 /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS.
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
73 On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as ()
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
74 does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
75 initial_rusage_as = get_rusage_as ();
14230
a09aeba402dc Enable memory leak tests on AIX.
Bruno Haible <bruno@clisp.org>
parents: 14179
diff changeset
76 #if !defined _AIX
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
77 if (initial_rusage_as == 0)
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 return 77;
14230
a09aeba402dc Enable memory leak tests on AIX.
Bruno Haible <bruno@clisp.org>
parents: 14179
diff changeset
79 #endif
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 arg = atoi (argv[1]);
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 if (arg == 0)
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 {
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 void *memory = malloc (MAX_ALLOC_TOTAL);
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 if (memory == NULL)
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 return 1;
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 memset (memory, 17, MAX_ALLOC_TOTAL);
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
88 result = 78;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
89 }
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
90 else
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
91 {
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
92 /* Perform the test and test whether it triggers a permanent memory
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
93 allocation of more than MAX_ALLOC_TOTAL bytes. */
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
94 int repeat;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
95
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
96 for (repeat = 0; repeat < NUM_ROUNDS; repeat++)
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
97 {
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
98 /* This may produce a temporary memory allocation of 11000 bytes.
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
99 but should not result in a permanent memory allocation. */
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
100 if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
101 && errno == ENOMEM)
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
102 return 1;
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
103 }
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
104
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
105 result = 0;
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 }
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
108 if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL)
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
109 return 1;
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
14179
c8f2d55830b2 *printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
111 return result;
12464
5bd91471b957 dprintf-posix: Check against memory leak fixed on 2009-12-15.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 }