diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2008-10-16 16:34:33 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2008-10-16 16:34:33 +0000 |
commit | 9fbaff40cbb4877e7c7474414a952948fbbf1da8 (patch) | |
tree | 2779ec8bd21910c02c9d15b1400f0cf954b9074c /usr.bin/sed | |
parent | 9841add7a7a14406c258116c4a6c8bd5fdc401af (diff) |
Move memory allocation closer to where it is needed. Also make
a distinction between len and size. OK deraadt@
Diffstat (limited to 'usr.bin/sed')
-rw-r--r-- | usr.bin/sed/compile.c | 52 | ||||
-rw-r--r-- | usr.bin/sed/defs.h | 8 | ||||
-rw-r--r-- | usr.bin/sed/main.c | 32 |
3 files changed, 46 insertions, 46 deletions
diff --git a/usr.bin/sed/compile.c b/usr.bin/sed/compile.c index 5af4f8f62e5..bc40a514581 100644 --- a/usr.bin/sed/compile.c +++ b/usr.bin/sed/compile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: compile.c,v 1.27 2008/10/09 21:14:58 millert Exp $ */ +/* $OpenBSD: compile.c,v 1.28 2008/10/16 16:34:32 millert Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -35,7 +35,7 @@ #ifndef lint /* from: static char sccsid[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95"; */ -static const char rcsid[] = "$OpenBSD: compile.c,v 1.27 2008/10/09 21:14:58 millert Exp $"; +static const char rcsid[] = "$OpenBSD: compile.c,v 1.28 2008/10/16 16:34:32 millert Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -148,16 +148,14 @@ compile_stream(struct s_command **link) { char *p; static char *lbuf; /* To avoid excessive malloc calls */ - static size_t len = _POSIX2_LINE_MAX; + static size_t bufsize; struct s_command *cmd, *cmd2, *stack; struct s_format *fp; int naddr; /* Number of addresses */ stack = 0; - if (!lbuf) - lbuf = xmalloc(len); for (;;) { - if ((p = cu_fgets(&lbuf, &len)) == NULL) { + if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) { if (stack != 0) err(COMPILE, "unexpected EOF (pending }'s)"); return (link); @@ -458,7 +456,7 @@ static char * compile_subst(char *p, struct s_subst *s) { static char *lbuf; - static size_t len; + static size_t bufsize; int asize, ref, size; char c, *text, *op, *sp; int sawesc = 0; @@ -467,22 +465,16 @@ compile_subst(char *p, struct s_subst *s) if (c == '\0') return (NULL); - len = strlen(p); - if (len < _POSIX2_LINE_MAX) - len = _POSIX2_LINE_MAX; - if (!lbuf) - lbuf = xmalloc(len); - s->maxbref = 0; s->linenum = linenum; - asize = 2 * len + 1; - text = xmalloc(asize); - size = 0; + text = NULL; + asize = size = 0; do { - if (asize - size < len + 1) { + size_t len = ROUNDLEN(strlen(p) + 1); + if (asize - size < len) { do { - asize *= 2; - } while (asize - size < len + 1); + asize += len; + } while (asize - size < len); text = xrealloc(text, asize); } op = sp = text + size; @@ -534,7 +526,7 @@ compile_subst(char *p, struct s_subst *s) *sp++ = *p; } size += sp - op; - } while ((p = cu_fgets(&lbuf, &len))); + } while ((p = cu_fgets(&lbuf, &bufsize))); err(COMPILE, "unterminated substitute in regular expression"); /* NOTREACHED */ } @@ -670,21 +662,19 @@ compile_text(void) { int asize, esc_nl, size; char *lbuf, *text, *p, *op, *s; - size_t len = _POSIX2_LINE_MAX; - - lbuf = xmalloc(len); - asize = 2 * len + 1; - text = xmalloc(asize); - size = 0; - while (cu_fgets(&lbuf, &len)) { - if (asize - size < len + 1) { + size_t bufsize; + + lbuf = text = NULL; + asize = size = 0; + while ((p = cu_fgets(&lbuf, &bufsize))) { + size_t len = ROUNDLEN(strlen(p) + 1); + if (asize - size < len) { do { - asize *= 2; - } while (asize - size < len + 1); + asize += len; + } while (asize - size < len); text = xrealloc(text, asize); } op = s = text + size; - p = lbuf; EATSPACE(); for (esc_nl = 0; *p != '\0'; p++) { if (*p == '\\' && p[1] != '\0' && *++p == '\n') diff --git a/usr.bin/sed/defs.h b/usr.bin/sed/defs.h index a8259dcb0f6..90c01584268 100644 --- a/usr.bin/sed/defs.h +++ b/usr.bin/sed/defs.h @@ -1,4 +1,4 @@ -/* * $OpenBSD: defs.h,v 1.3 2003/06/03 02:56:16 millert Exp $*/ +/* * $OpenBSD: defs.h,v 1.4 2008/10/16 16:34:32 millert Exp $*/ /*- * Copyright (c) 1992 Diomidis Spinellis. * Copyright (c) 1992, 1993 @@ -140,3 +140,9 @@ typedef struct { #define WARNING 2 /* Just print the warning */ #define COMPILE 3 /* Print error, count and finish script */ #define COMPILE2 3 /* Print error, count and finish script */ + +/* + * Round up to the nearest multiple of _POSIX2_LINE_MAX + */ +#define ROUNDLEN(x) \ + (((x) + _POSIX2_LINE_MAX - 1) & ~(_POSIX2_LINE_MAX - 1)) diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index f1ef6e0713f..fe2158ba4d1 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.14 2008/10/09 10:58:32 millert Exp $ */ +/* $OpenBSD: main.c,v 1.15 2008/10/16 16:34:32 millert Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -38,7 +38,7 @@ static const char copyright[] = "@(#) Copyright (c) 1992, 1993\n\ The Regents of the University of California. All rights reserved.\n"; /* from: static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; */ -static const char rcsid[] = "$OpenBSD: main.c,v 1.14 2008/10/09 10:58:32 millert Exp $"; +static const char rcsid[] = "$OpenBSD: main.c,v 1.15 2008/10/16 16:34:32 millert Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -46,6 +46,7 @@ static const char rcsid[] = "$OpenBSD: main.c,v 1.14 2008/10/09 10:58:32 millert #include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <limits.h> #include <regex.h> #include <stddef.h> #include <stdio.h> @@ -159,7 +160,7 @@ main(int argc, char *argv[]) * together. Empty strings and files are ignored. */ char * -cu_fgets(char **outbuf, size_t *outlen) +cu_fgets(char **outbuf, size_t *outsize) { static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; static FILE *f; /* Current open file */ @@ -168,6 +169,9 @@ cu_fgets(char **outbuf, size_t *outlen) size_t len; char *p; + if (*outbuf == NULL) + *outsize = 0; + again: switch (state) { case ST_EOF: @@ -196,12 +200,10 @@ again: case ST_FILE: if ((p = fgetln(f, &len)) != NULL) { linenum++; - if (len >= *outlen) { - do { - *outlen *= 2; - } while (len >= *outlen); + if (len >= *outsize) { free(*outbuf); - *outbuf = xmalloc(*outlen); + *outsize = ROUNDLEN(len + 1); + *outbuf = xmalloc(*outsize); } memcpy(*outbuf, p, len); (*outbuf)[len] = '\0'; @@ -217,13 +219,14 @@ again: if (linenum == 0 && s[0] == '#' && s[1] == 'n') nflag = 1; p = *outbuf; - len = *outlen; + len = *outsize; for (;;) { - if (len-- <= 1) { - *outbuf = xrealloc(*outbuf, *outlen * 2); - p = *outbuf + *outlen - len - 1; - len += *outlen; - *outlen *= 2; + if (len <= 1) { + *outbuf = xrealloc(*outbuf, + *outsize + _POSIX2_LINE_MAX); + p = *outbuf + *outsize - len; + len += _POSIX2_LINE_MAX; + *outsize += _POSIX2_LINE_MAX; } switch (*s) { case '\0': @@ -245,6 +248,7 @@ again: return (*outbuf); default: *p++ = *s++; + len--; } } } |