Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/xmalloc.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /* xmalloc.c -- safe versions of malloc and realloc */ | - | ||||||
2 | - | |||||||
3 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. | - | ||||||
4 | - | |||||||
5 | This file is part of GNU Bash, the GNU Bourne Again SHell. | - | ||||||
6 | - | |||||||
7 | Bash is free software: you can redistribute it and/or modify | - | ||||||
8 | it under the terms of the GNU General Public License as published by | - | ||||||
9 | the Free Software Foundation, either version 3 of the License, or | - | ||||||
10 | (at your option) any later version. | - | ||||||
11 | - | |||||||
12 | Bash is distributed in the hope that it will be useful, | - | ||||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||
15 | GNU General Public License for more details. | - | ||||||
16 | - | |||||||
17 | You should have received a copy of the GNU General Public License | - | ||||||
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | - | ||||||
19 | */ | - | ||||||
20 | - | |||||||
21 | #if defined (HAVE_CONFIG_H) | - | ||||||
22 | #include <config.h> | - | ||||||
23 | #endif | - | ||||||
24 | - | |||||||
25 | #include "bashtypes.h" | - | ||||||
26 | #include <stdio.h> | - | ||||||
27 | - | |||||||
28 | #if defined (HAVE_UNISTD_H) | - | ||||||
29 | # include <unistd.h> | - | ||||||
30 | #endif | - | ||||||
31 | - | |||||||
32 | #if defined (HAVE_STDLIB_H) | - | ||||||
33 | # include <stdlib.h> | - | ||||||
34 | #else | - | ||||||
35 | # include "ansi_stdlib.h" | - | ||||||
36 | #endif /* HAVE_STDLIB_H */ | - | ||||||
37 | - | |||||||
38 | #include "error.h" | - | ||||||
39 | - | |||||||
40 | #include "bashintl.h" | - | ||||||
41 | - | |||||||
42 | #if !defined (PTR_T) | - | ||||||
43 | # if defined (__STDC__) | - | ||||||
44 | # define PTR_T void * | - | ||||||
45 | # else | - | ||||||
46 | # define PTR_T char * | - | ||||||
47 | # endif /* !__STDC__ */ | - | ||||||
48 | #endif /* !PTR_T */ | - | ||||||
49 | - | |||||||
50 | #if defined (HAVE_SBRK) && !HAVE_DECL_SBRK | - | ||||||
51 | extern char *sbrk(); | - | ||||||
52 | #endif | - | ||||||
53 | - | |||||||
54 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) | - | ||||||
55 | static PTR_T lbreak; | - | ||||||
56 | static int brkfound; | - | ||||||
57 | static size_t allocated; | - | ||||||
58 | #endif | - | ||||||
59 | - | |||||||
60 | /* **************************************************************** */ | - | ||||||
61 | /* */ | - | ||||||
62 | /* Memory Allocation and Deallocation. */ | - | ||||||
63 | /* */ | - | ||||||
64 | /* **************************************************************** */ | - | ||||||
65 | - | |||||||
66 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) | - | ||||||
67 | #define FINDBRK() \ | - | ||||||
68 | do { \ | - | ||||||
69 | if (brkfound == 0) \ | - | ||||||
70 | { \ | - | ||||||
71 | lbreak = (PTR_T)sbrk (0); \ | - | ||||||
72 | brkfound++; \ | - | ||||||
73 | } \ | - | ||||||
74 | } while (0) | - | ||||||
75 | - | |||||||
76 | static size_t | - | ||||||
77 | findbrk () | - | ||||||
78 | { | - | ||||||
79 | FINDBRK(); never executed: end of block
| 0 | ||||||
80 | return (char *)sbrk (0) - (char *)lbreak; never executed: return (char *)sbrk (0) - (char *)lbreak; | 0 | ||||||
81 | } | - | ||||||
82 | #else | - | ||||||
83 | #define FINDBRK() | - | ||||||
84 | #endif | - | ||||||
85 | - | |||||||
86 | static void | - | ||||||
87 | allocerr (func, bytes) | - | ||||||
88 | const char *func; | - | ||||||
89 | size_t bytes; | - | ||||||
90 | { | - | ||||||
91 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) | - | ||||||
92 | allocated = findbrk (); | - | ||||||
93 | fatal_error (_("%s: cannot allocate %lu bytes (%lu bytes allocated)"), func, (unsigned long)bytes, (unsigned long)allocated); | - | ||||||
94 | #else | - | ||||||
95 | fatal_error (_("%s: cannot allocate %lu bytes"), func, (unsigned long)bytes); | - | ||||||
96 | #endif /* !HAVE_SBRK */ | - | ||||||
97 | } never executed: end of block | 0 | ||||||
98 | - | |||||||
99 | /* Return a pointer to free()able block of memory large enough | - | ||||||
100 | to hold BYTES number of bytes. If the memory cannot be allocated, | - | ||||||
101 | print an error message and abort. */ | - | ||||||
102 | PTR_T | - | ||||||
103 | xmalloc (bytes) | - | ||||||
104 | size_t bytes; | - | ||||||
105 | { | - | ||||||
106 | PTR_T temp; | - | ||||||
107 | - | |||||||
108 | #if defined (DEBUG) | - | ||||||
109 | if (bytes == 0)
| 0-2424 | ||||||
110 | internal_warning("xmalloc: size argument is 0"); never executed: internal_warning("xmalloc: size argument is 0"); | 0 | ||||||
111 | #endif | - | ||||||
112 | - | |||||||
113 | FINDBRK(); never executed: end of block
| 0-2424 | ||||||
114 | temp = malloc (bytes); | - | ||||||
115 | - | |||||||
116 | if (temp == 0)
| 0-2424 | ||||||
117 | allocerr ("xmalloc", bytes); never executed: allocerr ("xmalloc", bytes); | 0 | ||||||
118 | - | |||||||
119 | return (temp); executed 2424 times by 1 test: return (temp); Executed by:
| 2424 | ||||||
120 | } | - | ||||||
121 | - | |||||||
122 | PTR_T | - | ||||||
123 | xrealloc (pointer, bytes) | - | ||||||
124 | PTR_T pointer; | - | ||||||
125 | size_t bytes; | - | ||||||
126 | { | - | ||||||
127 | PTR_T temp; | - | ||||||
128 | - | |||||||
129 | #if defined (DEBUG) | - | ||||||
130 | if (bytes == 0)
| 0-42 | ||||||
131 | internal_warning("xrealloc: size argument is 0"); never executed: internal_warning("xrealloc: size argument is 0"); | 0 | ||||||
132 | #endif | - | ||||||
133 | - | |||||||
134 | FINDBRK(); never executed: end of block
| 0-42 | ||||||
135 | temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
| 11-31 | ||||||
136 | - | |||||||
137 | if (temp == 0)
| 0-42 | ||||||
138 | allocerr ("xrealloc", bytes); never executed: allocerr ("xrealloc", bytes); | 0 | ||||||
139 | - | |||||||
140 | return (temp); executed 42 times by 1 test: return (temp); Executed by:
| 42 | ||||||
141 | } | - | ||||||
142 | - | |||||||
143 | /* Use this as the function to call when adding unwind protects so we | - | ||||||
144 | don't need to know what free() returns. */ | - | ||||||
145 | void | - | ||||||
146 | xfree (string) | - | ||||||
147 | PTR_T string; | - | ||||||
148 | { | - | ||||||
149 | if (string)
| 6254-23833 | ||||||
150 | free (string); executed 23833 times by 1 test: free (string); Executed by:
| 23833 | ||||||
151 | } executed 30087 times by 1 test: end of block Executed by:
| 30087 | ||||||
152 | - | |||||||
153 | #ifdef USING_BASH_MALLOC | - | ||||||
154 | #include <malloc/shmalloc.h> | - | ||||||
155 | - | |||||||
156 | static void | - | ||||||
157 | sh_allocerr (func, bytes, file, line) | - | ||||||
158 | const char *func; | - | ||||||
159 | size_t bytes; | - | ||||||
160 | char *file; | - | ||||||
161 | int line; | - | ||||||
162 | { | - | ||||||
163 | #if defined (HAVE_SBRK) | - | ||||||
164 | allocated = findbrk (); | - | ||||||
165 | fatal_error (_("%s: %s:%d: cannot allocate %lu bytes (%lu bytes allocated)"), func, file, line, (unsigned long)bytes, (unsigned long)allocated); | - | ||||||
166 | #else | - | ||||||
167 | fatal_error (_("%s: %s:%d: cannot allocate %lu bytes"), func, file, line, (unsigned long)bytes); | - | ||||||
168 | #endif /* !HAVE_SBRK */ | - | ||||||
169 | } never executed: end of block | 0 | ||||||
170 | - | |||||||
171 | PTR_T | - | ||||||
172 | sh_xmalloc (bytes, file, line) | - | ||||||
173 | size_t bytes; | - | ||||||
174 | char *file; | - | ||||||
175 | int line; | - | ||||||
176 | { | - | ||||||
177 | PTR_T temp; | - | ||||||
178 | - | |||||||
179 | #if defined (DEBUG) | - | ||||||
180 | if (bytes == 0)
| 0-Inf | ||||||
181 | internal_warning("xmalloc: %s:%d: size argument is 0", file, line); never executed: internal_warning("xmalloc: %s:%d: size argument is 0", file, line); | 0 | ||||||
182 | #endif | - | ||||||
183 | - | |||||||
184 | FINDBRK(); executed 5432 times by 1 test: end of block Executed by:
| 5432-Inf | ||||||
185 | temp = sh_malloc (bytes, file, line); | - | ||||||
186 | - | |||||||
187 | if (temp == 0)
| 0-Inf | ||||||
188 | sh_allocerr ("xmalloc", bytes, file, line); never executed: sh_allocerr ("xmalloc", bytes, file, line); | 0 | ||||||
189 | - | |||||||
190 | return (temp); executed 2147483647 times by 1 test: return (temp); Executed by:
| Inf | ||||||
191 | } | - | ||||||
192 | - | |||||||
193 | PTR_T | - | ||||||
194 | sh_xrealloc (pointer, bytes, file, line) | - | ||||||
195 | PTR_T pointer; | - | ||||||
196 | size_t bytes; | - | ||||||
197 | char *file; | - | ||||||
198 | int line; | - | ||||||
199 | { | - | ||||||
200 | PTR_T temp; | - | ||||||
201 | - | |||||||
202 | #if defined (DEBUG) | - | ||||||
203 | if (bytes == 0)
| 0-4814432 | ||||||
204 | internal_warning("xrealloc: %s:%d: size argument is 0", file, line); never executed: internal_warning("xrealloc: %s:%d: size argument is 0", file, line); | 0 | ||||||
205 | #endif | - | ||||||
206 | - | |||||||
207 | FINDBRK(); never executed: end of block
| 0-4814432 | ||||||
208 | temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line);
| 92882-4721550 | ||||||
209 | - | |||||||
210 | if (temp == 0)
| 0-4814432 | ||||||
211 | sh_allocerr ("xrealloc", bytes, file, line); never executed: sh_allocerr ("xrealloc", bytes, file, line); | 0 | ||||||
212 | - | |||||||
213 | return (temp); executed 4814432 times by 1 test: return (temp); Executed by:
| 4814432 | ||||||
214 | } | - | ||||||
215 | - | |||||||
216 | void | - | ||||||
217 | sh_xfree (string, file, line) | - | ||||||
218 | PTR_T string; | - | ||||||
219 | char *file; | - | ||||||
220 | int line; | - | ||||||
221 | { | - | ||||||
222 | if (string)
| 365685933-Inf | ||||||
223 | sh_free (string, file, line); executed 2147483647 times by 1 test: sh_free (string, file, line); Executed by:
| Inf | ||||||
224 | } executed 2147483647 times by 1 test: end of block Executed by:
| Inf | ||||||
225 | #endif | - | ||||||
Source code | Switch to Preprocessed file |