AmendHub

Download:

jcs

/

wikipedia

/

amendments

/

17

util: Bring in C extensions, put malloc_map on the heap

This lets it compile with ANSI-small

jcs made amendment 17 about 1 year ago
--- util.c Wed Aug 24 13:18:35 2022 +++ util.c Wed Aug 31 22:54:33 2022 @@ -1,6 +1,7 @@ /* * Copyright (c) 2020-2022 joshua stein <jcs@jcs.org> - * + * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org> + * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. @@ -14,6 +15,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +/* THINK C Project must have a header of "#include <MacHeaders>" */ + #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -73,21 +76,26 @@ enum { NOTE_ALERT }; -#ifdef MALLOC_DEBUG +/* + * Define to audit each malloc and free and verify that a pointer isn't + * double-freed. The list of outstanding allocations can be checked by + * looking through malloc_map. + */ +//#define MALLOC_DEBUG +#ifdef MALLOC_DEBUG /* * List of allocations, updated at xmalloc() and xfree(). If an address * passed to xfree() isn't in the list, it indicates a double-free. */ -#define MALLOC_MAP_SIZE 512 +#define MALLOC_MAP_CHUNK_SIZE 1024 struct malloc_map_e { unsigned long addr; unsigned long size; char note[MALLOC_NOTE_SIZE]; -} malloc_map[MALLOC_MAP_SIZE]; - +} *malloc_map = NULL; +unsigned long malloc_map_size = 0; static bool malloc_map_compact = false; - #endif void vwarn(short alert_func, const char *format, va_list ap); @@ -106,7 +114,12 @@ util_init(void) HUnlock(alert_ditl_h); #ifdef MALLOC_DEBUG - memset(&malloc_map, 0, sizeof(malloc_map)); + malloc_map_size = MALLOC_MAP_CHUNK_SIZE; + malloc_map = (struct malloc_map_e *)NewPtr(malloc_map_size * + sizeof(struct malloc_map_e)); + if (malloc_map == NULL) + panic("NewPtr(%lu) failed", MALLOC_MAP_CHUNK_SIZE); + memset(malloc_map, 0, malloc_map_size); #endif } @@ -121,6 +134,7 @@ xmalloc(size_t size, char *note) { void *ptr; #ifdef MALLOC_DEBUG + struct malloc_map_e *new_malloc_map; unsigned short n, j; #endif @@ -133,16 +147,16 @@ xmalloc(size_t size, char *note) #ifdef MALLOC_DEBUG if (malloc_map_compact) { - for (n = 0; n < MALLOC_MAP_SIZE; n++) { + for (n = 0; n < malloc_map_size; n++) { if (malloc_map[n].addr != 0) continue; - for (j = n + 1; j < MALLOC_MAP_SIZE; j++) { + for (j = n + 1; j < malloc_map_size; j++) { if (malloc_map[j].addr == 0) continue; malloc_map[n] = malloc_map[j]; - memset(&malloc_map[j], 0, sizeof(malloc_map[j])); + memset(&malloc_map[j], 0, sizeof(struct malloc_map_e)); break; } } @@ -150,10 +164,21 @@ xmalloc(size_t size, char *note) malloc_map_compact = false; } - for (n = 0; n <= MALLOC_MAP_SIZE; n++) { - if (n == MALLOC_MAP_SIZE) - panic("xmalloc(%lu): out of malloc map entries, likely a " - "memory leak", size); + for (n = 0; n <= malloc_map_size; n++) { + if (n == malloc_map_size) { + malloc_map_size += MALLOC_MAP_CHUNK_SIZE; + warn("xmalloc(%lu): out of malloc map entries, maybe a " + "memory leak, resizing to %ld", size, malloc_map_size); + new_malloc_map = (struct malloc_map_e *)NewPtr( + malloc_map_size * sizeof(struct malloc_map_e)); + if (new_malloc_map == NULL) + panic("out of memory resizing malloc map"); + memcpy(new_malloc_map, malloc_map, + (malloc_map_size - MALLOC_MAP_CHUNK_SIZE) * + sizeof(struct malloc_map_e)); + DisposePtr(malloc_map); + malloc_map = new_malloc_map; + } if (malloc_map[n].addr == 0) { malloc_map[n].addr = (unsigned long)ptr; malloc_map[n].size = size; @@ -175,8 +200,8 @@ xfree(void *ptrptr) #ifdef MALLOC_DEBUG unsigned long n; - for (n = 0; n <= MALLOC_MAP_SIZE; n++) { - if (n == MALLOC_MAP_SIZE) + for (n = 0; n <= malloc_map_size; n++) { + if (n == malloc_map_size) panic("xfree(0x%lx): can't find in alloc map, likely " "double free()", *addr); if (malloc_map[n].addr == *addr) { @@ -228,8 +253,8 @@ xrealloc(void *src, size_t size) #ifdef MALLOC_DEBUG if (src != NULL) { - for (n = 0; n <= MALLOC_MAP_SIZE; n++) { - if (n == MALLOC_MAP_SIZE) { + for (n = 0; n <= malloc_map_size; n++) { + if (n == malloc_map_size) { panic("xrealloc(%lu): can't find in alloc map, likely " "double free()", (unsigned long)src); return NULL; @@ -1492,4 +1517,424 @@ RestoreHiddenMenuBar(void) HiliteMenu(0); DrawMenuBar(); +} + +/* C Extensions */ + +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} + +char * +strndup(const char *str, size_t maxlen) +{ + char *copy; + const char *cp; + size_t len; + + /* strnlen */ + for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) + ; + + len = (size_t)(cp - str); + copy = malloc(len + 1); + if (copy != NULL) { + (void)memcpy(copy, str, len); + copy[len] = '\0'; + } + + return copy; +} + +static struct format { + unsigned leftJustify : 1; + unsigned forceSign : 1; + unsigned altForm : 1; + unsigned zeroPad : 1; + unsigned havePrecision : 1; + unsigned hSize : 1; + unsigned lSize : 1; + unsigned LSize : 1; + char sign; + char exponent; + int fieldWidth; + int precision; +} default_format; + +int bounded_vfprintf(FILE *fp, const char *fmt, va_list arg); +static int nullio(FILE *fp, int i); + +int +bounded_vfprintf(FILE *fp, const char *fmt, va_list arg) +{ + register int c, i, j, nwritten = 0; + register unsigned long n; + long double x; + register char *s; +#define VFPRINTF_BUFLEN 512 + char buf[VFPRINTF_BUFLEN], *digits, *t; + struct format F; + + for (c = *fmt; c; c = *++fmt) { + if (c != '%') + goto copy1; + F = default_format; + + /* decode flags */ + + for (;;) { + c = *++fmt; + if (c == '-') + F.leftJustify = TRUE; + else if (c == '+') + F.forceSign = TRUE; + else if (c == ' ') + F.sign = ' '; + else if (c == '#') + F.altForm = TRUE; + else if (c == '0') + F.zeroPad = TRUE; + else + break; + } + + /* decode field width */ + + if (c == '*') { + if ((F.fieldWidth = va_arg(arg, int)) < 0) { + F.leftJustify = TRUE; + F.fieldWidth = -F.fieldWidth; + } + c = *++fmt; + } + else { + for (; c >= '0' && c <= '9'; c = *++fmt) + F.fieldWidth = (10 * F.fieldWidth) + (c - '0'); + } + + /* decode precision */ + + if (c == '.') { + if ((c = *++fmt) == '*') { + F.precision = va_arg(arg, int); + c = *++fmt; + } + else { + for (; c >= '0' && c <= '9'; c = *++fmt) + F.precision = (10 * F.precision) + (c - '0'); + } + if (F.precision >= 0) + F.havePrecision = TRUE; + } + + /* perform appropriate conversion */ + + s = &buf[VFPRINTF_BUFLEN]; + if (F.leftJustify) + F.zeroPad = FALSE; +conv: switch (c) { + + /* 'h' size modifier */ + + case 'h': + F.hSize = TRUE; + c = *++fmt; + goto conv; + + /* 'l' size modifier */ + + case 'l': + F.lSize = TRUE; + c = *++fmt; + goto conv; + + /* 'L' size modifier */ + + case 'L': + F.LSize = TRUE; + c = *++fmt; + goto conv; + + /* decimal (signed) */ + + case 'd': + case 'i': + if (F.lSize) + n = va_arg(arg, long); + else + n = va_arg(arg, int); + if (F.hSize) + n = (short) n; + if ((long) n < 0) { + n = -n; + F.sign = '-'; + } + else if (F.forceSign) + F.sign = '+'; + goto decimal; + + /* decimal (unsigned) */ + + case 'u': + if (F.lSize) + n = va_arg(arg, unsigned long); + else + n = va_arg(arg, unsigned int); + if (F.hSize) + n = (unsigned short) n; + F.sign = 0; + goto decimal; + + /* decimal (common code) */ + + decimal: + if (!F.havePrecision) { + if (F.zeroPad) { + F.precision = F.fieldWidth; + if (F.sign) + --F.precision; + } + if (F.precision < 1) + F.precision = 1; + } + for (i = 0; n; n /= 10, i++) + *--s = n % 10 + '0'; + for (; i < F.precision; i++) + *--s = '0'; + if (F.sign) { + *--s = F.sign; + i++; + } + break; + + /* octal (unsigned) */ + + case 'o': + if (F.lSize) + n = va_arg(arg, unsigned long); + else + n = va_arg(arg, unsigned int); + if (F.hSize) + n = (unsigned short) n; + if (!F.havePrecision) { + if (F.zeroPad) + F.precision = F.fieldWidth; + if (F.precision < 1) + F.precision = 1; + } + for (i = 0; n; n /= 8, i++) + *--s = n % 8 + '0'; + if (F.altForm && i && *s != '0') { + *--s = '0'; + i++; + } + for (; i < F.precision; i++) + *--s = '0'; + break; + + /* hexadecimal (unsigned) */ + + case 'p': + F.havePrecision = F.lSize = TRUE; + F.precision = 8; + /* ... */ + case 'X': + digits = "0123456789ABCDEF"; + goto hexadecimal; + case 'x': + digits = "0123456789abcdef"; + /* ... */ + hexadecimal: + if (F.lSize) + n = va_arg(arg, unsigned long); + else + n = va_arg(arg, unsigned int); + if (F.hSize) + n = (unsigned short) n; + if (!F.havePrecision) { + if (F.zeroPad) { + F.precision = F.fieldWidth; + if (F.altForm) + F.precision -= 2; + } + if (F.precision < 1) + F.precision = 1; + } + for (i = 0; n; n /= 16, i++) + *--s = digits[n % 16]; + for (; i < F.precision; i++) + *--s = '0'; + if (F.altForm) { + *--s = c; + *--s = '0'; + i += 2; + } + break; + + /* character */ + + case 'c': + *--s = va_arg(arg, int); + i = 1; + break; + + /* string */ + + case 's': + s = va_arg(arg, char *); + if (F.altForm) { + i = (unsigned char) *s++; + if (F.havePrecision && i > F.precision) + i = F.precision; + } + else { + if (!F.havePrecision) + i = strlen(s); + else if (t = memchr(s, '\0', F.precision)) + i = t - s; + else + i = F.precision; + } + break; + + /* store # bytes written so far */ + + case 'n': + s = va_arg(arg, void *); + if (F.hSize) + * (short *) s = nwritten; + else if (F.lSize) + * (long *) s = nwritten; + else + * (int *) s = nwritten; + continue; + + /* oops - unknown conversion, abort */ + + default: + if (c != '%') + goto done; + copy1: + putc(c, fp); /* disregard EOF */ + ++nwritten; + continue; + } + + /* pad on the left */ + + if (i < F.fieldWidth && !F.leftJustify) { + do { + putc(' ', fp); /* disregard EOF */ + ++nwritten; + } while (i < --F.fieldWidth); + } + + /* write the converted result */ + + fwrite(s, 1, i, fp); /* disregard EOF */ + nwritten += i; + + /* pad on the right */ + + for (; i < F.fieldWidth; i++) { + putc(' ', fp); /* disregard EOF */ + ++nwritten; + } + } + + /* all done! */ + +done: + return(nwritten); +} + +int +snprintf(char *s, size_t size, const char *fmt, ...) +{ + return(vsnprintf(s, size, fmt, __va(fmt))); +} + +static int +nullio(FILE *fp, int i) +{ + return(EOF); +} + +int +vsnprintf(char *s, size_t size, const char *fmt, void *p) +{ + FILE f; + int n; + + memset(&f, 0, sizeof(f)); + f.refnum = -1; + f.ptr = (unsigned char *) s; + f.cnt = size; + f.proc = nullio; + f.dirty = 1; + + if ((n = bounded_vfprintf(&f, fmt, p)) >= 0) + s[n] = 0; + return(n); } --- util.h Wed Aug 24 13:18:06 2022 +++ util.h Wed Aug 31 23:13:02 2022 @@ -21,9 +21,6 @@ #include <limits.h> #include <time.h> -#define MALLOC_DEBUG -#define MALLOC_NOTE_SIZE 32 - #ifndef SIZE_MAX #define SIZE_MAX ULONG_MAX #endif @@ -31,6 +28,8 @@ #define nitems(what) (sizeof((what)) / sizeof((what)[0])) #define member_size(type, member) sizeof(((type *)0)->member) +#define MALLOC_NOTE_SIZE 32 + #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define BOUND(a, min, max) ((a) > (max) ? (max) : ((a) < (min) ? (min) : (a))) @@ -100,8 +99,6 @@ void * xreallocarray(void *, size_t, size_t); char * xstrdup(const char *, char *note); short getline(char *str, size_t len, char **ret); -size_t strlcpy(char *dst, const char *src, size_t dsize); -size_t strlcat(char *dst, const char *src, size_t dsize); const char * ordinal(unsigned short n); size_t rtrim(char *str, char *chars); long strpos_quoted(char *str, char c); @@ -155,5 +152,12 @@ void PasswordDialogFieldFinish(void); pascal void NullCaretHook(void); void HideMenuBar(void); void RestoreHiddenMenuBar(void); + +size_t strlcat(char *dst, const char *src, size_t dsize); +size_t strlcpy(char *dst, const char *src, size_t dsize); +char * strndup(const char *str, size_t maxlen); +int snprintf(char *s, size_t size, const char *fmt, ...); +int vsnprintf(char *s, size_t size, const char *fmt, void *p); + #endif