1 /* $NetBSD: memalloc.c,v 1.42 2026/04/18 14:35:16 kre Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)memalloc.c 8.3 (Berkeley) 5/4/95"; 39 #else 40 __RCSID("$NetBSD: memalloc.c,v 1.42 2026/04/18 14:35:16 kre Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <limits.h> 45 #include <stdarg.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 49 #include "shell.h" 50 #include "output.h" 51 #include "memalloc.h" 52 #include "error.h" 53 #include "machdep.h" 54 #include "mystring.h" 55 56 /* 57 * Like malloc, but returns an error when out of space. 58 */ 59 60 pointer 61 ckmalloc(size_t nbytes) 62 { 63 pointer p; 64 65 p = malloc(nbytes); 66 if (p == NULL) 67 error("Out of space"); 68 return p; 69 } 70 71 72 /* 73 * Same for realloc. 74 */ 75 76 pointer 77 ckrealloc(pointer p, int nbytes) 78 { 79 p = realloc(p, nbytes); 80 if (p == NULL) 81 error("Out of space"); 82 return p; 83 } 84 85 86 /* 87 * Make a copy of a string in safe storage. 88 */ 89 90 char * 91 savestr(const char *s) 92 { 93 char *p; 94 95 p = ckmalloc(strlen(s) + 1); 96 scopy(s, p); 97 return p; 98 } 99 100 101 /* 102 * Parse trees for commands are allocated in lifo order, so we use a stack 103 * to make this more efficient, and also to avoid all sorts of exception 104 * handling code to handle interrupts in the middle of a parse. 105 * 106 * The size 504 was chosen because the Ultrix malloc handles that size 107 * well. 108 */ 109 110 #define MINSIZE 504 /* minimum size of a block */ 111 112 struct stack_block { 113 struct stack_block *prev; 114 char space[MINSIZE]; 115 }; 116 117 struct stack_block stackbase; 118 struct stack_block *stackp = &stackbase; 119 struct stackmark *markp; 120 char *stacknxt = stackbase.space; 121 int stacknleft = MINSIZE; 122 int sstrnleft; 123 124 pointer 125 stalloc(int nbytes) 126 { 127 char *p; 128 129 nbytes = SHELL_ALIGN(nbytes); 130 if (nbytes > stacknleft) { 131 int blocksize; 132 struct stack_block *sp; 133 134 blocksize = nbytes; 135 if (blocksize < MINSIZE) 136 blocksize = MINSIZE; 137 INTOFF; 138 sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize); 139 sp->prev = stackp; 140 stacknxt = sp->space; 141 stacknleft = blocksize; 142 stackp = sp; 143 INTON; 144 } 145 INTOFF; 146 p = stacknxt; 147 stacknxt += nbytes; 148 stacknleft -= nbytes; 149 INTON; 150 return p; 151 } 152 153 154 void 155 stunalloc(pointer p) 156 { 157 if (p == NULL) { /*DEBUG */ 158 write(2, "stunalloc\n", 10); 159 abort(); 160 } 161 stacknleft += stacknxt - (char *)p; 162 stacknxt = p; 163 } 164 165 166 /* save the current status of the sh stack */ 167 void 168 setstackmark(struct stackmark *mark) 169 { 170 mark->stackp = stackp; 171 mark->stacknxt = stacknxt; 172 mark->stacknleft = stacknleft; 173 mark->sstrnleft = sstrnleft; 174 mark->marknext = markp; 175 markp = mark; 176 } 177 178 /* reset the stack mark, and remove it from the list of marks */ 179 void 180 popstackmark(struct stackmark *mark) 181 { 182 INTOFF; 183 markp = mark->marknext; /* delete mark from the list */ 184 rststackmark(mark); /* and reset stack */ 185 INTON; 186 } 187 188 /* reset the shell stack to its state recorded in the stack mark */ 189 void 190 rststackmark(struct stackmark *mark) 191 { 192 struct stack_block *sp; 193 194 INTOFF; 195 while (stackp != mark->stackp) { 196 /* delete any recently allocated mem blocks */ 197 sp = stackp; 198 stackp = sp->prev; 199 ckfree(sp); 200 } 201 stacknxt = mark->stacknxt; 202 stacknleft = mark->stacknleft; 203 sstrnleft = mark->sstrnleft; 204 INTON; 205 } 206 207 208 /* 209 * When the parser reads in a string, it wants to stick the string on the 210 * stack and only adjust the stack pointer when it knows how big the 211 * string is. Stackblock (defined in stack.h) returns a pointer to a block 212 * of space on top of the stack and stackblocklen returns the length of 213 * this block. Growstackblock will grow this space by at least one byte, 214 * possibly moving it (like realloc). Grabstackblock actually allocates the 215 * part of the block that has been used. 216 */ 217 218 void 219 growstackblock(void) 220 { 221 int newlen = SHELL_ALIGN(stacknleft * 2 + 100); 222 223 INTOFF; 224 if (stacknxt == stackp->space && stackp != &stackbase) { 225 struct stack_block *oldstackp; 226 struct stackmark *xmark; 227 struct stack_block *sp; 228 229 oldstackp = stackp; 230 sp = stackp; 231 stackp = sp->prev; 232 sp = ckrealloc((pointer)sp, 233 sizeof(struct stack_block) - MINSIZE + newlen); 234 sp->prev = stackp; 235 stackp = sp; 236 stacknxt = sp->space; 237 sstrnleft += newlen - stacknleft; 238 stacknleft = newlen; 239 240 /* 241 * Stack marks pointing to the start of the old block 242 * must be relocated to point to the new block 243 */ 244 xmark = markp; 245 while (xmark != NULL && xmark->stackp == oldstackp) { 246 xmark->stackp = stackp; 247 xmark->stacknxt = stacknxt; 248 xmark->sstrnleft += stacknleft - xmark->stacknleft; 249 xmark->stacknleft = stacknleft; 250 xmark = xmark->marknext; 251 } 252 } else { 253 char *oldspace = stacknxt; 254 int oldlen = stacknleft; 255 char *p = stalloc(newlen); 256 257 (void)memcpy(p, oldspace, oldlen); 258 stacknxt = p; /* free the space */ 259 stacknleft += newlen; /* we just allocated */ 260 } 261 INTON; 262 } 263 264 void 265 grabstackblock(int len) 266 { 267 len = SHELL_ALIGN(len); 268 INTOFF; 269 stacknxt += len; 270 stacknleft -= len; 271 INTON; 272 } 273 274 /* 275 * The following routines are somewhat easier to use than the above. 276 * The macro STARTSTACKSTR(ptr) initializes things. "ptr" must be the 277 * name of a "char *" variable. Then the user uses the macro STPUTC to 278 * add characters to the string. In effect, STPUTC(c, ptr) is the same 279 * as *ptr++ = c except that the stack is grown as necessary. Note that 280 * the value of ptr is not exoected to increase linearly, its value 281 * after a call to STPUTC() is not comparable to any value it had before. 282 * 283 * When the user is done, she can just leave the string there and refer to 284 * it using stackblock(). In that case, the string will be overwritten by 285 * the next call to stalloc() or STARTSTACKSTR(). Or she can allocate 286 * the space for it using grabstackstr(). If it is necessary to allow 287 * someone else to use the stack temporarily and then continue to grow 288 * the string, the user should use grabstackstr() to allocate the space, 289 * and then call ungrabstackstr(p) to return to the previous mode of operation. 290 * 291 * USTPUTC is like STPUTC except that it doesn't check for overflow. 292 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there 293 * is space for (at least) some known number of calls to USTPUTC() 294 * CHECKSTACKSPACE may make arbitrary changes to the ptr, USTPUTC does not. 295 * 296 * STUNPUTC() is the inverse of STPUTC() (and USTPUTC()) and undoes 297 * the most recent (not previously undone) call of either of those. 298 * The user must ensure that no more than what has been added to the 299 * current string is removed. 300 * 301 * Aside from when adjusted by the macros/functions here, the "ptr" 302 * variable MUST not be altered (it can be used to examine data). 303 * The ptr var always refers to one past the most recently added byte, 304 * *ptr is is always undefined as is ptr[1] (etc), but ptr[-1] can 305 * be used after data has been added (STTOPC() is a cleaner way). 306 */ 307 308 char * 309 growstackstr(void) 310 { 311 int len = stackblocksize(); 312 313 growstackblock(); 314 sstrnleft = stackblocksize() - len - 1; 315 return stackblock() + len; 316 } 317 318 /* 319 * Called from CHECKSTRSPACE(). (nowhere else) 320 * Make sure there is space for "required" bytes 321 */ 322 char * 323 makestrspace(int required) 324 { 325 int len = stackblocksize() - sstrnleft; 326 do { 327 growstackblock(); 328 sstrnleft = stackblocksize() - len; 329 } while (sstrnleft < required); 330 return stackblock() + len; 331 } 332 333 /* 334 * Note that this only works to release stack space for reuse 335 * if nothing else has allocated space on the stack since the grabstackstr() 336 * 337 * "s" is the start of the area to be released, and "p" represents the end 338 * of the string we have stored beyond there and are now releasing. 339 * (ie: "p" should be the same as in the call to grabstackstr()). 340 * 341 * stunalloc(s) and ungrabstackstr(s, p) are almost interchangeable after 342 * a grabstackstr(), however the latter also returns string space so we 343 * can just continue with STPUTC() etc without needing a new STARTSTACKSTR(s) 344 */ 345 void 346 ungrabstackstr(char *s, char *p) 347 { 348 #ifdef DEBUG 349 if (s < stacknxt || stacknxt + stacknleft < s) 350 abort(); 351 #endif 352 stacknleft += stacknxt - s; 353 stacknxt = s; 354 sstrnleft = stacknleft - (p - s); 355 } 356 357 /* 358 * Save the concat of a sequence of strings in stack space 359 * 360 * The first arg (if not NULL) is a pointer to where the final string 361 * length will be returned. 362 * 363 * Remaining args are pointers to strings - sufficient space to hold 364 * the concat of the strings is allocated on the stack, the strings 365 * are copied into that space, and a pointer to its start is returned. 366 * The arg list is terminated with STSTRC_END. 367 * 368 * Use stunalloc(string) (in proper sequence) to release the string 369 */ 370 char * 371 ststrcat(size_t *lp, ...) 372 { 373 va_list ap; 374 const char *arg; 375 size_t len, tlen = 0, alen[8]; 376 char *str, *nxt; 377 unsigned int n; 378 379 n = 0; 380 va_start(ap, lp); 381 arg = va_arg(ap, const char *); 382 while (arg != STSTRC_END) { 383 len = strlen(arg); 384 if (n < sizeof(alen)/sizeof(alen[0])) 385 alen[n++] = len; 386 tlen += len; 387 arg = va_arg(ap, const char *); 388 } 389 va_end(ap); 390 391 if (lp != NULL) 392 *lp = tlen; 393 394 if (tlen >= INT_MAX) 395 error("ststrcat() over length botch"); 396 str = (char *)stalloc((int)tlen + 1); /* 1 for \0 */ 397 str[tlen] = '\0'; /* in case of no args */ 398 399 n = 0; 400 nxt = str; 401 va_start(ap, lp); 402 arg = va_arg(ap, const char *); 403 while (arg != STSTRC_END) { 404 if (n < sizeof(alen)/sizeof(alen[0])) 405 len = alen[n++]; 406 else 407 len = strlen(arg); 408 409 scopy(arg, nxt); 410 nxt += len; 411 412 arg = va_arg(ap, const char *); 413 } 414 va_end(ap); 415 416 return str; 417 } 418