shithub: trueawk

Download patch

ref: 6589208eaf7e7da73a5a3fe8420c37c970206edc
parent: 1633ba1c8830ccd6592f27c732980a60579d81f2
author: zoulasc <zoulasc@users.noreply.github.com>
date: Thu Oct 24 05:40:15 EDT 2019

More cleanups: (#53)

- sprinkle const
- add a macro (setptr) that cheats const to temporarily NUL terminate strings
  remove casts from allocations
- use strdup instead of strlen+strcpy
- use x = malloc(sizeof(*x)) instead of x = malloc(sizeof(type of *x)))
- add -Wcast-qual (and casts through unitptr_t in the two macros we
  cheat (xfree, setptr)).

--- a/awk.h
+++ b/awk.h
@@ -30,8 +30,13 @@
 
 typedef	unsigned char uschar;
 
-#define	xfree(a)	{ if ((a) != NULL) { free((void *) (a)); (a) = NULL; } }
-
+#define	xfree(a)	{ if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
+/*
+ * We sometimes cheat writing read-only pointers to NUL-terminate them
+ * and then put back the original value
+ */
+#define setptr(ptr, a)	(*(char *)(intptr_t)(ptr)) = (a)
+
 #define	NN(p)	((p) ? (p) : "(null)")	/* guaranteed non-null for dprintf
 */
 #define	DEBUG
@@ -70,7 +75,7 @@
 
 extern int	dbg;
 
-extern	char	*patbeg;	/* beginning of pattern matched */
+extern const char *patbeg;	/* beginning of pattern matched */
 extern	int	patlen;		/* length of pattern matched.  set in b.c */
 
 /* Cell:  all information about a variable or constant */
--- a/b.c
+++ b/b.c
@@ -61,19 +61,19 @@
 
 int	rtok;		/* next token in current re */
 int	rlxval;
-static uschar	*rlxstr;
-static uschar	*prestr;	/* current position in current re */
-static uschar	*lastre;	/* origin of last re */
-static uschar	*lastatom;	/* origin of last Atom */
-static uschar	*starttok;
-static uschar 	*basestr;	/* starts with original, replaced during
+static const uschar	*rlxstr;
+static const uschar	*prestr;	/* current position in current re */
+static const uschar	*lastre;	/* origin of last re */
+static const uschar	*lastatom;	/* origin of last Atom */
+static const uschar	*starttok;
+static const uschar 	*basestr;	/* starts with original, replaced during
 				   repetition processing */
-static uschar 	*firstbasestr;
+static const uschar 	*firstbasestr;
 
 static	int setcnt;
 static	int poscnt;
 
-char	*patbeg;
+const char	*patbeg;
 int	patlen;
 
 #define	NFA	128	/* cache this many dynamic fa's */
@@ -185,7 +185,7 @@
 	Node *p, *p1;
 	fa *f;
 
-	firstbasestr = (uschar *) s;
+	firstbasestr = (const uschar *) s;
 	basestr = firstbasestr;
 	p = reparse(s);
 	p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p);
@@ -195,7 +195,7 @@
 
 	poscnt = 0;
 	penter(p1);	/* enter parent pointers and leaf indices */
-	if ((f = (fa *) calloc(1, sizeof(fa) + poscnt*sizeof(rrow))) == NULL)
+	if ((f = calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
 		overflo(__func__);
 	f->accept = poscnt-1;	/* penter has computed number of positions in re */
 	cfoll(f, p1);	/* set up follow sets */
@@ -295,13 +295,13 @@
 /* in the parsing of regular expressions, metacharacters like . have */
 /* to be seen literally;  \056 is not a metacharacter. */
 
-int hexstr(uschar **pp)	/* find and eval hex string at pp, return new p */
+int hexstr(const uschar **pp)	/* find and eval hex string at pp, return new p */
 {			/* only pick up one 8-bit byte (2 chars) */
-	uschar *p;
+	const uschar *p;
 	int n = 0;
 	int i;
 
-	for (i = 0, p = (uschar *) *pp; i < 2 && isxdigit(*p); i++, p++) {
+	for (i = 0, p = *pp; i < 2 && isxdigit(*p); i++, p++) {
 		if (isdigit(*p))
 			n = 16 * n + *p - '0';
 		else if (*p >= 'a' && *p <= 'f')
@@ -309,16 +309,16 @@
 		else if (*p >= 'A' && *p <= 'F')
 			n = 16 * n + *p - 'A' + 10;
 	}
-	*pp = (uschar *) p;
+	*pp = p;
 	return n;
 }
 
 #define isoctdigit(c) ((c) >= '0' && (c) <= '7')	/* multiple use of arg */
 
-int quoted(uschar **pp)	/* pick up next thing after a \\ */
+int quoted(const uschar **pp)	/* pick up next thing after a \\ */
 			/* and increment *pp */
 {
-	uschar *p = *pp;
+	const uschar *p = *pp;
 	int c;
 
 	if ((c = *p++) == 't')
@@ -356,8 +356,8 @@
 char *cclenter(const char *argp)	/* add a character class */
 {
 	int i, c, c2;
-	uschar *p = (uschar *) argp;
-	uschar *op, *bp;
+	const uschar *op, *p = (const uschar *) argp;
+	uschar *bp;
 	static uschar *buf = NULL;
 	static int bufsz = 100;
 
@@ -516,7 +516,7 @@
 
 int member(int c, const char *sarg)	/* is c in s? */
 {
-	uschar *s = (uschar *) sarg;
+	const uschar *s = (const uschar *) sarg;
 
 	while (*s)
 		if (c == *s++)
@@ -527,7 +527,7 @@
 int match(fa *f, const char *p0)	/* shortest match ? */
 {
 	int s, ns;
-	uschar *p = (uschar *) p0;
+	const uschar *p = (const uschar *) p0;
 
 	s = f->initstat;
 	assert (s < f->state_count);
@@ -549,13 +549,13 @@
 int pmatch(fa *f, const char *p0)	/* longest match, for sub */
 {
 	int s, ns;
-	uschar *p = (uschar *) p0;
-	uschar *q;
+	const uschar *p = (const uschar *) p0;
+	const uschar *q;
 
 	s = f->initstat;
 	assert(s < f->state_count);
 
-	patbeg = (char *)p;
+	patbeg = (const char *)p;
 	patlen = -1;
 	do {
 		q = p;
@@ -572,7 +572,7 @@
 
 			if (s == 1) {	/* no transition */
 				if (patlen >= 0) {
-					patbeg = (char *) p;
+					patbeg = (const char *) p;
 					return(1);
 				}
 				else
@@ -582,7 +582,7 @@
 		if (f->out[s])
 			patlen = q-p-1;	/* don't count $ */
 		if (patlen >= 0) {
-			patbeg = (char *) p;
+			patbeg = (const char *) p;
 			return(1);
 		}
 	nextin:
@@ -594,13 +594,13 @@
 int nematch(fa *f, const char *p0)	/* non-empty match, for sub */
 {
 	int s, ns;
-	uschar *p = (uschar *) p0;
-	uschar *q;
+	const uschar *p = (const uschar *) p0;
+	const uschar *q;
 
 	s = f->initstat;
 	assert(s < f->state_count);
 
-	patbeg = (char *)p;
+	patbeg = (const char *)p;
 	patlen = -1;
 	while (*p) {
 		q = p;
@@ -614,7 +614,7 @@
 				s = cgoto(f, s, *q);
 			if (s == 1) {	/* no transition */
 				if (patlen > 0) {
-					patbeg = (char *) p;
+					patbeg = (const char *) p;
 					return(1);
 				} else
 					goto nnextin;	/* no nonempty match */
@@ -623,7 +623,7 @@
 		if (f->out[s])
 			patlen = q-p-1;	/* don't count $ */
 		if (patlen > 0 ) {
-			patbeg = (char *) p;
+			patbeg = (const char *) p;
 			return(1);
 		}
 	nnextin:
@@ -726,7 +726,7 @@
 	Node *np;
 
 	dprintf( ("reparse <%s>\n", p) );
-	lastre = prestr = (uschar *) p;	/* prestr points to string to be parsed */
+	lastre = prestr = (const uschar *) p;	/* prestr points to string to be parsed */
 	rtok = relex();
 	/* GNU compatibility: an empty regexp matches anything */
 	if (rtok == '\0') {
@@ -766,12 +766,12 @@
 		rtok = relex();
 		return (unary(op2(DOT, NIL, NIL)));
 	case CCL:
-		np = op2(CCL, NIL, (Node*) cclenter((char *) rlxstr));
+		np = op2(CCL, NIL, (Node*) cclenter((const char *) rlxstr));
 		lastatom = starttok;
 		rtok = relex();
 		return (unary(np));
 	case NCCL:
-		np = op2(NCCL, NIL, (Node *) cclenter((char *) rlxstr));
+		np = op2(NCCL, NIL, (Node *) cclenter((const char *) rlxstr));
 		lastatom = starttok;
 		rtok = relex();
 		return (unary(np));
@@ -911,7 +911,7 @@
 	int init_q = (firstnum==0);		/* first added char will be ? */
 	int n_q_reps = secondnum-firstnum;	/* m>n, so reduce until {1,m-n} left  */
 	int prefix_length = reptok - basestr;	/* prefix includes first rep	*/
-	int suffix_length = strlen((char *) reptok) - reptoklen;	/* string after rep specifier	*/
+	int suffix_length = strlen((const char *) reptok) - reptoklen;	/* string after rep specifier	*/
 	int size = prefix_length +  suffix_length;
 
 	if (firstnum > 1) {	/* add room for reps 2 through firstnum */
--- a/lex.c
+++ b/lex.c
@@ -173,7 +173,7 @@
 	static char *buf = NULL;
 	static int bufsize = 5; /* BUG: setting this small causes core dump! */
 
-	if (buf == NULL && (buf = (char *) malloc(bufsize)) == NULL)
+	if (buf == NULL && (buf = malloc(bufsize)) == NULL)
 		FATAL( "out of space in yylex" );
 	if (sc) {
 		sc = 0;
@@ -363,7 +363,7 @@
 	static char *buf = NULL;
 	static int bufsz = 500;
 
-	if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
+	if (buf == NULL && (buf = malloc(bufsz)) == NULL)
 		FATAL("out of space for strings");
 	for (bp = buf; (c = input()) != '"'; ) {
 		if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, "string"))
@@ -510,7 +510,7 @@
 	static int bufsz = 500;
 	char *bp;
 
-	if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
+	if (buf == NULL && (buf = malloc(bufsz)) == NULL)
 		FATAL("out of space for rex expr");
 	bp = buf;
 	for ( ; (c = input()) != '/' && c != 0; ) {
--- a/lib.c
+++ b/lib.c
@@ -57,10 +57,10 @@
 
 void recinit(unsigned int n)
 {
-	if ( (record = (char *) malloc(n)) == NULL
-	  || (fields = (char *) malloc(n+1)) == NULL
-	  || (fldtab = (Cell **) malloc((nfields+2) * sizeof(Cell *))) == NULL
-	  || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL )
+	if ( (record = malloc(n)) == NULL
+	  || (fields = malloc(n+1)) == NULL
+	  || (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
+	  || (fldtab[0] = malloc(sizeof(**fldtab))) == NULL)
 		FATAL("out of space for $0 and fields");
 	*record = '\0';
 	*fldtab[0] = dollar0;
@@ -75,7 +75,7 @@
 	int i;
 
 	for (i = n1; i <= n2; i++) {
-		fldtab[i] = (Cell *) malloc(sizeof (struct Cell));
+		fldtab[i] = malloc(sizeof(**fldtab));
 		if (fldtab[i] == NULL)
 			FATAL("out of space in makefields %d", i);
 		*fldtab[i] = dollar1;
@@ -215,7 +215,7 @@
 		fa *pfa = makedfa(rs, 1);
 		found = fnematch(pfa, inf, &buf, &bufsize, recsize);
 		if (found)
-			*patbeg = 0;
+			setptr(patbeg, '\0');
 	} else {
 		if ((sep = *rs) == 0) {
 			sep = '\n';
@@ -304,7 +304,7 @@
 	n = strlen(r);
 	if (n > fieldssize) {
 		xfree(fields);
-		if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
+		if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
 			FATAL("out of space for fields in fldbld %d", n);
 		fieldssize = n;
 	}
@@ -447,7 +447,7 @@
 		nf = n;
 	s = (nf+1) * (sizeof (struct Cell *));  /* freebsd: how much do we need? */
 	if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */
-		fldtab = (Cell **) realloc(fldtab, s);
+		fldtab = realloc(fldtab, s);
 	else					/* overflow sizeof int */
 		xfree(fldtab);	/* make it null */
 	if (fldtab == NULL)
@@ -467,7 +467,7 @@
 	n = strlen(rec);
 	if (n > fieldssize) {
 		xfree(fields);
-		if ((fields = (char *) malloc(n+1)) == NULL)
+		if ((fields = malloc(n+1)) == NULL)
 			FATAL("out of space for fields in refldbld %d", n);
 		fieldssize = n;
 	}
--- a/maketab.c
+++ b/maketab.c
@@ -145,8 +145,7 @@
 			/* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
 			continue;
 		}
-		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
-		strcpy(names[tok-FIRSTTOKEN], name);
+		names[tok-FIRSTTOKEN] = strdup(name);
 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
 		i++;
 	}
--- a/parse.c
+++ b/parse.c
@@ -33,7 +33,7 @@
 {
 	Node *x;
 
-	x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
+	x = malloc(sizeof(*x) + (n-1) * sizeof(x));
 	if (x == NULL)
 		FATAL("out of space in nodealloc");
 	x->nnext = NULL;
--- a/proto.h
+++ b/proto.h
@@ -43,8 +43,8 @@
 extern	int	makeinit(fa *, int);
 extern	void	penter(Node *);
 extern	void	freetr(Node *);
-extern	int	hexstr(uschar **);
-extern	int	quoted(uschar **);
+extern	int	hexstr(const uschar **);
+extern	int	quoted(const uschar **);
 extern	char	*cclenter(const char *);
 extern	void	overflo(const char *) __attribute__((__noreturn__));
 extern	void	cfoll(fa *, Node *);
--- a/run.c
+++ b/run.c
@@ -113,8 +113,8 @@
 		/* round up to next multiple of quantum */
 		if (rminlen)
 			minlen += quantum - rminlen;
-		tbuf = (char *) realloc(*pbuf, minlen);
-		dprintf( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void *) *pbuf, (void *) tbuf) );
+		tbuf = realloc(*pbuf, minlen);
+		dprintf( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, *pbuf, tbuf) );
 		if (tbuf == NULL) {
 			if (whatrtn)
 				FATAL("out of memory in %s", whatrtn);
@@ -236,7 +236,7 @@
 	if (!isfcn(fcn))
 		FATAL("calling undefined function %s", s);
 	if (frame == NULL) {
-		fp = frame = (struct Frame *) calloc(nframe += 100, sizeof(struct Frame));
+		fp = frame = calloc(nframe += 100, sizeof(*frame));
 		if (frame == NULL)
 			FATAL("out of space for stack frames calling %s", s);
 	}
@@ -270,8 +270,7 @@
 	fp++;	/* now ok to up frame */
 	if (fp >= frame + nframe) {
 		int dfp = fp - frame;	/* old index */
-		frame = (struct Frame *)
-			realloc((char *) frame, (nframe += 100) * sizeof(struct Frame));
+		frame = realloc(frame, (nframe += 100) * sizeof(*frame));
 		if (frame == NULL)
 			FATAL("out of space for stack frames in %s", s);
 		fp = frame + dfp;
@@ -404,7 +403,7 @@
 	int bufsize = recsize;
 	int mode;
 
-	if ((buf = (char *) malloc(bufsize)) == NULL)
+	if ((buf = malloc(bufsize)) == NULL)
 		FATAL("out of memory in getline");
 
 	fflush(stdout);	/* in case someone is waiting for a prompt */
@@ -472,7 +471,7 @@
 	int bufsz = recsize;
 	int nsub;
 
-	if ((buf = (char *) malloc(bufsz)) == NULL)
+	if ((buf = malloc(bufsz)) == NULL)
 		FATAL("out of memory in array");
 
 	x = execute(a[0]);	/* Cell* for symbol table */
@@ -525,7 +524,7 @@
 	} else {
 		int bufsz = recsize;
 		char *buf;
-		if ((buf = (char *) malloc(bufsz)) == NULL)
+		if ((buf = malloc(bufsz)) == NULL)
 			FATAL("out of memory in adelete");
 		buf[0] = 0;
 		for (np = a[1]; np; np = np->nnext) {
@@ -564,7 +563,7 @@
 		ap->tval |= ARR;
 		ap->sval = (char *) makesymtab(NSYMTAB);
 	}
-	if ((buf = (char *) malloc(bufsz)) == NULL) {
+	if ((buf = malloc(bufsz)) == NULL) {
 		FATAL("out of memory in intest");
 	}
 	buf[0] = 0;
@@ -714,7 +713,7 @@
 	Cell *x;
 
 	if (!tmps) {
-		tmps = (Cell *) calloc(100, sizeof(Cell));
+		tmps = calloc(100, sizeof(*tmps));
 		if (!tmps)
 			FATAL("out of space for temporaries");
 		for (i = 1; i < 100; i++)
@@ -850,7 +849,7 @@
 
 	os = s;
 	p = buf;
-	if ((fmt = (char *) malloc(fmtsz)) == NULL)
+	if ((fmt = malloc(fmtsz)) == NULL)
 		FATAL("out of memory in format()");
 	while (*s) {
 		adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
@@ -991,7 +990,7 @@
 	char *buf;
 	int bufsz=3*recsize;
 
-	if ((buf = (char *) malloc(bufsz)) == NULL)
+	if ((buf = malloc(bufsz)) == NULL)
 		FATAL("out of memory in awksprintf");
 	y = a[0]->nnext;
 	x = execute(a[0]);
@@ -1014,7 +1013,7 @@
 	int len;
 	int bufsz=3*recsize;
 
-	if ((buf = (char *) malloc(bufsz)) == NULL)
+	if ((buf = malloc(bufsz)) == NULL)
 		FATAL("out of memory in awkprintf");
 	y = a[0]->nnext;
 	x = execute(a[0]);
@@ -1257,10 +1256,10 @@
 Cell *split(Node **a, int nnn)	/* split(a[0], a[1], a[2]); a[3] is type */
 {
 	Cell *x = NULL, *y, *ap;
-	char *s, *origs;
+	const char *s, *origs, *t;
 	char *fs = NULL, *origfs = NULL;
 	int sep;
-	char *t, temp, num[50];
+	char temp, num[50];
 	int n, tempstat, arg3type;
 
 	y = execute(a[0]);	/* source string */
@@ -1305,12 +1304,12 @@
 				n++;
 				sprintf(num, "%d", n);
 				temp = *patbeg;
-				*patbeg = '\0';
+				setptr(patbeg, '\0');
 				if (is_number(s))
 					setsymtab(num, s, atof(s), STR|NUM, (Array *) ap->sval);
 				else
 					setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
-				*patbeg = temp;
+				setptr(patbeg, temp);
 				s = patbeg + patlen;
 				if (*(patbeg+patlen-1) == 0 || *s == 0) {
 					n++;
@@ -1343,13 +1342,13 @@
 				s++;
 			while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
 			temp = *s;
-			*s = '\0';
+			setptr(s, '\0');
 			sprintf(num, "%d", n);
 			if (is_number(t))
 				setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
 			else
 				setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
-			*s = temp;
+			setptr(s, temp);
 			if (*s != 0)
 				s++;
 		}
@@ -1372,13 +1371,13 @@
 			while (*s != sep && *s != '\n' && *s != '\0')
 				s++;
 			temp = *s;
-			*s = '\0';
+			setptr(s, '\0');
 			sprintf(num, "%d", n);
 			if (is_number(t))
 				setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
 			else
 				setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
-			*s = temp;
+			setptr(s, temp);
 			if (*s++ == 0)
 				break;
 		}
@@ -1385,8 +1384,8 @@
 	}
 	tempfree(ap);
 	tempfree(y);
-	free(origs);
-	free(origfs);
+	xfree(origs);
+	xfree(origfs);
 	x = gettemp();
 	x->tval = NUM;
 	x->fval = n;
@@ -1828,13 +1827,13 @@
 			fflush(files[i].fp);
 }
 
-void backsub(char **pb_ptr, char **sptr_ptr);
+void backsub(char **pb_ptr, const char **sptr_ptr);
 
 Cell *sub(Node **a, int nnn)	/* substitute command */
 {
-	char *sptr, *pb, *q;
+	const char *sptr, *q;
 	Cell *x, *y, *result;
-	char *t, *buf;
+	char *t, *buf, *pb;
 	fa *pfa;
 	int bufsz = recsize;
 
@@ -1893,13 +1892,14 @@
 Cell *gsub(Node **a, int nnn)	/* global substitute */
 {
 	Cell *x, *y;
-	char *rptr, *sptr, *t, *pb, *q;
+	char *rptr, *pb;
+	const char *q, *t, *sptr;
 	char *buf;
 	fa *pfa;
 	int mflag, tempstat, num;
 	int bufsz = recsize;
 
-	if ((buf = (char *) malloc(bufsz)) == NULL)
+	if ((buf = malloc(bufsz)) == NULL)
 		FATAL("out of memory in gsub");
 	mflag = 0;	/* if mflag == 0, can replace empty string */
 	num = 0;
@@ -1991,9 +1991,10 @@
 	return(x);
 }
 
-void backsub(char **pb_ptr, char **sptr_ptr)	/* handle \\& variations */
+void backsub(char **pb_ptr, const char **sptr_ptr)	/* handle \\& variations */
 {						/* sptr[0] == '\\' */
-	char *pb = *pb_ptr, *sptr = *sptr_ptr;
+	char *pb = *pb_ptr;
+	const char *sptr = *sptr_ptr;
 
 	if (sptr[1] == '\\') {
 		if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */
--- a/tran.c
+++ b/tran.c
@@ -164,8 +164,8 @@
 	Array *ap;
 	Cell **tp;
 
-	ap = (Array *) malloc(sizeof(Array));
-	tp = (Cell **) calloc(n, sizeof(Cell *));
+	ap = malloc(sizeof(*ap));
+	tp = calloc(n, sizeof(*tp));
 	if (ap == NULL || tp == NULL)
 		FATAL("out of space in makesymtab");
 	ap->nelem = 0;
@@ -235,7 +235,7 @@
 			(void*)p, NN(p->nval), NN(p->sval), p->fval, p->tval) );
 		return(p);
 	}
-	p = (Cell *) malloc(sizeof(Cell));
+	p = malloc(sizeof(*p));
 	if (p == NULL)
 		FATAL("out of space for symbol table at %s", n);
 	p->nval = tostring(n);
@@ -270,7 +270,7 @@
 	Cell *cp, *op, **np;
 
 	nsz = GROWTAB * tp->size;
-	np = (Cell **) calloc(nsz, sizeof(Cell *));
+	np = calloc(nsz, sizeof(*np));
 	if (np == NULL)		/* can't do it, but can keep running. */
 		return;		/* someone else will run out later. */
 	for (i = 0; i < tp->size; i++) {
@@ -359,7 +359,7 @@
 		fldno = atoi(vp->nval);
 		if (fldno > *NF)
 			newfld(fldno);
-		   dprintf( ("setting field %d to %s (%p)\n", fldno, s, (void *) s) );
+		   dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) );
 	} else if (isrec(vp)) {
 		donefld = 0;	/* mark $1... invalid */
 		donerec = 1;
@@ -376,7 +376,7 @@
 	vp->fmt = NULL;
 	setfree(vp);
 	   dprintf( ("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n",
-		(void*)vp, NN(vp->nval), t, (void *) t, vp->tval, donerec, donefld) );
+		(void*)vp, NN(vp->nval), t, t, vp->tval, donerec, donefld) );
 	vp->sval = t;
 	if (&vp->fval == NF) {
 		donerec = 0;	/* mark $0 invalid */
@@ -490,7 +490,7 @@
 	}
 done:
 	   dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n",
-		(void*)vp, NN(vp->nval), vp->sval, (void *) vp->sval, vp->tval) );
+		(void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) );
 	return(vp->sval);
 }
 
@@ -507,12 +507,9 @@
 
 char *tostring(const char *s)	/* make a copy of string s */
 {
-	char *p;
-
-	p = (char *) malloc(strlen(s)+1);
+	char *p = strdup(s);
 	if (p == NULL)
 		FATAL("out of space in tostring on %s", s);
-	strcpy(p, s);
 	return(p);
 }
 
@@ -536,10 +533,10 @@
 {
 	const char *os = is;
 	int c, n;
-	uschar *s = (uschar *) is;
+	const uschar *s = (const uschar *) is;
 	uschar *buf, *bp;
 
-	if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL)
+	if ((buf = malloc(strlen(is)+3)) == NULL)
 		FATAL( "out of space in qstring(%s)", s);
 	for (bp = buf; (c = *s) != delim; s++) {
 		if (c == '\n')
--