| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* xmalloc.c -- safe versions of malloc and realloc */ |
| 2 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | This file is part of GNU Bash, the GNU Bourne Again SHell. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 6 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | |
| 21 | #if defined (HAVE_CONFIG_H) |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 25 | #include "bashtypes.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 40 | #include "bashintl.h" |
| 41 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 50 | #if defined (HAVE_SBRK) && !HAVE_DECL_SBRK |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | extern char *sbrk(); |
| 52 | #endif |
| 53 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 54 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | static PTR_T lbreak; |
| 56 | static int brkfound; |
| 57 | static size_t allocated; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 58 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | |
| 60 | /* **************************************************************** */ |
| 61 | /* */ |
| 62 | /* Memory Allocation and Deallocation. */ |
| 63 | /* */ |
| 64 | /* **************************************************************** */ |
| 65 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 66 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 67 | #define FINDBRK() \ |
| 68 | do { \ |
| 69 | if (brkfound == 0) \ |
| 70 | { \ |
| 71 | lbreak = (PTR_T)sbrk (0); \ |
| 72 | brkfound++; \ |
| 73 | } \ |
| 74 | } while (0) |
| 75 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 76 | static size_t |
| 77 | findbrk () |
| 78 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 79 | FINDBRK(); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 80 | return (char *)sbrk (0) - (char *)lbreak; |
| 81 | } |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 82 | #else |
| 83 | #define FINDBRK() |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 86 | static void |
| 87 | allocerr (func, bytes) |
| 88 | const char *func; |
| 89 | size_t bytes; |
| 90 | { |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 91 | #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 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 | } |
| 98 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 102 | PTR_T |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 103 | xmalloc (bytes) |
| 104 | size_t bytes; |
| 105 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 106 | PTR_T temp; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 107 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 108 | #if defined (DEBUG) |
| 109 | if (bytes == 0) |
| 110 | internal_warning("xmalloc: size argument is 0"); |
| 111 | #endif |
| 112 | |
| 113 | FINDBRK(); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 114 | temp = malloc (bytes); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | |
| 116 | if (temp == 0) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 117 | allocerr ("xmalloc", bytes); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 118 | |
| 119 | return (temp); |
| 120 | } |
| 121 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 122 | PTR_T |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 123 | xrealloc (pointer, bytes) |
| 124 | PTR_T pointer; |
| 125 | size_t bytes; |
| 126 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 127 | PTR_T temp; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 128 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 129 | #if defined (DEBUG) |
| 130 | if (bytes == 0) |
| 131 | internal_warning("xrealloc: size argument is 0"); |
| 132 | #endif |
| 133 | |
| 134 | FINDBRK(); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 135 | temp = pointer ? realloc (pointer, bytes) : malloc (bytes); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | |
| 137 | if (temp == 0) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 138 | allocerr ("xrealloc", bytes); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | |
| 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. */ |
| 145 | void |
| 146 | xfree (string) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 147 | PTR_T string; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 148 | { |
| 149 | if (string) |
| 150 | free (string); |
| 151 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 152 | |
| 153 | #ifdef USING_BASH_MALLOC |
| 154 | #include <malloc/shmalloc.h> |
| 155 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 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 | } |
| 170 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 179 | #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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 185 | temp = sh_malloc (bytes, file, line); |
| 186 | |
| 187 | if (temp == 0) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 188 | sh_allocerr ("xmalloc", bytes, file, line); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 189 | |
| 190 | return (temp); |
| 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 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 202 | #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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 208 | temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line); |
| 209 | |
| 210 | if (temp == 0) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 211 | sh_allocerr ("xrealloc", bytes, file, line); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 212 | |
| 213 | return (temp); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | sh_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 |