blob: ca297dcf04c8fe724dabbaba1bb9ef6d8d342ac9 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* xmalloc.c -- safe versions of malloc and realloc */
2
Chet Rameya0c0a002016-09-15 16:59:08 -04003/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of GNU Bash, the GNU Bourne Again SHell.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00006
Jari Aalto31859422009-01-12 13:36:28 +00007 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016
17 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000018 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +000020
21#if defined (HAVE_CONFIG_H)
22#include <config.h>
23#endif
24
Jari Aaltod166f041997-06-05 14:59:13 +000025#include "bashtypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000026#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
Jari Aaltob80f6442004-07-27 13:29:18 +000040#include "bashintl.h"
41
Jari Aaltoccc6cda1996-12-23 17:02:34 +000042#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
Jari Aaltof73dda02001-11-13 17:56:06 +000050#if defined (HAVE_SBRK) && !HAVE_DECL_SBRK
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051extern char *sbrk();
52#endif
53
Chet Rameyd233b482019-01-07 09:27:52 -050054#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055static PTR_T lbreak;
56static int brkfound;
57static size_t allocated;
Chet Rameyd233b482019-01-07 09:27:52 -050058#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059
60/* **************************************************************** */
61/* */
62/* Memory Allocation and Deallocation. */
63/* */
64/* **************************************************************** */
65
Chet Rameya0c0a002016-09-15 16:59:08 -040066#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
Chet Ramey00018032011-11-21 20:51:19 -050067#define FINDBRK() \
68do { \
69 if (brkfound == 0) \
70 { \
71 lbreak = (PTR_T)sbrk (0); \
72 brkfound++; \
73 } \
74} while (0)
75
Jari Aaltob72432f1999-02-19 17:11:39 +000076static size_t
77findbrk ()
78{
Chet Ramey00018032011-11-21 20:51:19 -050079 FINDBRK();
Jari Aaltob72432f1999-02-19 17:11:39 +000080 return (char *)sbrk (0) - (char *)lbreak;
81}
Chet Ramey00018032011-11-21 20:51:19 -050082#else
83#define FINDBRK()
Jari Aaltob72432f1999-02-19 17:11:39 +000084#endif
85
Chet Ramey00018032011-11-21 20:51:19 -050086static void
87allocerr (func, bytes)
88 const char *func;
89 size_t bytes;
90{
Chet Rameya0c0a002016-09-15 16:59:08 -040091#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
Chet Ramey00018032011-11-21 20:51:19 -050092 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}
98
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099/* 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. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000102PTR_T
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000103xmalloc (bytes)
104 size_t bytes;
105{
Jari Aaltof73dda02001-11-13 17:56:06 +0000106 PTR_T temp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000107
Chet Ramey00018032011-11-21 20:51:19 -0500108#if defined (DEBUG)
109 if (bytes == 0)
110 internal_warning("xmalloc: size argument is 0");
111#endif
112
113 FINDBRK();
Jari Aaltof73dda02001-11-13 17:56:06 +0000114 temp = malloc (bytes);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000115
116 if (temp == 0)
Chet Ramey00018032011-11-21 20:51:19 -0500117 allocerr ("xmalloc", bytes);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118
119 return (temp);
120}
121
Jari Aaltof73dda02001-11-13 17:56:06 +0000122PTR_T
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000123xrealloc (pointer, bytes)
124 PTR_T pointer;
125 size_t bytes;
126{
Jari Aaltof73dda02001-11-13 17:56:06 +0000127 PTR_T temp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000128
Chet Ramey00018032011-11-21 20:51:19 -0500129#if defined (DEBUG)
130 if (bytes == 0)
131 internal_warning("xrealloc: size argument is 0");
132#endif
133
134 FINDBRK();
Jari Aaltof73dda02001-11-13 17:56:06 +0000135 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000136
137 if (temp == 0)
Chet Ramey00018032011-11-21 20:51:19 -0500138 allocerr ("xrealloc", bytes);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139
140 return (temp);
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. */
145void
146xfree (string)
Jari Aaltobb706242000-03-17 21:46:59 +0000147 PTR_T string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000148{
149 if (string)
150 free (string);
151}
Jari Aaltof73dda02001-11-13 17:56:06 +0000152
153#ifdef USING_BASH_MALLOC
154#include <malloc/shmalloc.h>
155
Chet Ramey00018032011-11-21 20:51:19 -0500156static void
157sh_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}
170
Jari Aaltof73dda02001-11-13 17:56:06 +0000171PTR_T
172sh_xmalloc (bytes, file, line)
173 size_t bytes;
174 char *file;
175 int line;
176{
177 PTR_T temp;
178
Chet Ramey00018032011-11-21 20:51:19 -0500179#if defined (DEBUG)
180 if (bytes == 0)
181 internal_warning("xmalloc: %s:%d: size argument is 0", file, line);
182#endif
183
184 FINDBRK();
Jari Aaltof73dda02001-11-13 17:56:06 +0000185 temp = sh_malloc (bytes, file, line);
186
187 if (temp == 0)
Chet Ramey00018032011-11-21 20:51:19 -0500188 sh_allocerr ("xmalloc", bytes, file, line);
Jari Aaltof73dda02001-11-13 17:56:06 +0000189
190 return (temp);
191}
192
193PTR_T
194sh_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
Chet Ramey00018032011-11-21 20:51:19 -0500202#if defined (DEBUG)
203 if (bytes == 0)
204 internal_warning("xrealloc: %s:%d: size argument is 0", file, line);
205#endif
206
207 FINDBRK();
Jari Aaltof73dda02001-11-13 17:56:06 +0000208 temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line);
209
210 if (temp == 0)
Chet Ramey00018032011-11-21 20:51:19 -0500211 sh_allocerr ("xrealloc", bytes, file, line);
Jari Aaltof73dda02001-11-13 17:56:06 +0000212
213 return (temp);
214}
215
216void
217sh_xfree (string, file, line)
218 PTR_T string;
219 char *file;
220 int line;
221{
222 if (string)
223 sh_free (string, file, line);
224}
225#endif