shithub: neatroff

Download patch

ref: 8f4d430fa2cfb6c56feb0eacec4cbc9590961d78
parent: 93691381ddd563a675b962970ea135bc89489dd4
author: Ali Gholami Rudi <ali@rudi.ir>
date: Wed Jul 2 05:16:25 EDT 2014

roff: call xmalloc() to report when neatroff is out of memory

--- a/dict.c
+++ b/dict.c
@@ -19,12 +19,12 @@
 	d->n = 1;
 	d->level2 = level2;
 	d->notfound = notfound;
-	d->key = malloc(size * sizeof(d->key[0]));
-	d->val = malloc(size * sizeof(d->val[0]));
-	d->next = malloc(size * sizeof(d->next[0]));
-	d->head = malloc((level2 ? 256 * 256 : 256) * sizeof(d->next[0]));
+	d->key = xmalloc(size * sizeof(d->key[0]));
+	d->val = xmalloc(size * sizeof(d->val[0]));
+	d->next = xmalloc(size * sizeof(d->next[0]));
+	d->head = xmalloc((level2 ? 256 * 256 : 256) * sizeof(d->next[0]));
 	if (dupkeys)
-		d->buf = malloc(size * NMLEN);
+		d->buf = xmalloc(size * NMLEN);
 }
 
 void dict_done(struct dict *d)
--- a/fmt.c
+++ b/fmt.c
@@ -233,7 +233,7 @@
 			int hy, int str, int gap)
 {
 	int len = strlen(wb_buf(wb));
-	word->s = malloc(len + 1);
+	word->s = xmalloc(len + 1);
 	memcpy(word->s, wb_buf(wb), len + 1);
 	word->wid = wb_wid(wb);
 	word->elsn = wb->els_neg;
@@ -508,7 +508,7 @@
 
 struct fmt *fmt_alloc(void)
 {
-	struct fmt *fmt = malloc(sizeof(*fmt));
+	struct fmt *fmt = xmalloc(sizeof(*fmt));
 	memset(fmt, 0, sizeof(*fmt));
 	return fmt;
 }
--- a/font.c
+++ b/font.c
@@ -186,7 +186,7 @@
 	fin = fopen(path, "r");
 	if (!fin)
 		return NULL;
-	fn = malloc(sizeof(*fn));
+	fn = xmalloc(sizeof(*fn));
 	if (!fn) {
 		fclose(fin);
 		return NULL;
--- a/in.c
+++ b/in.c
@@ -29,7 +29,7 @@
 
 static void in_new(void)
 {
-	struct inbuf *next = malloc(sizeof(*next));
+	struct inbuf *next = xmalloc(sizeof(*next));
 	memset(next, 0, sizeof(*next));
 	next->prev = buf;
 	buf = next;
@@ -39,7 +39,7 @@
 {
 	int len = strlen(s);
 	in_new();
-	buf->buf = malloc(len + 1);
+	buf->buf = xmalloc(len + 1);
 	buf->len = len;
 	strcpy(buf->buf, s);
 	buf->args = args ? args_init(args) : NULL;
@@ -194,13 +194,13 @@
 
 static char **args_init(char **args)
 {
-	char **out = malloc(NARGS * sizeof(*out));
+	char **out = xmalloc(NARGS * sizeof(*out));
 	int i;
 	for (i = 0; i < NARGS; i++) {
 		out[i] = NULL;
 		if (args[i]) {
 			int len = strlen(args[i]) + 1;
-			out[i] = malloc(len);
+			out[i] = xmalloc(len);
 			memcpy(out[i], args[i], len);
 		}
 	}
--- a/reg.c
+++ b/reg.c
@@ -127,7 +127,7 @@
 	int len = strlen(s) + 1;
 	if (sregs[id])
 		free(sregs[id]);
-	sregs[id] = malloc(len);
+	sregs[id] = xmalloc(len);
 	memcpy(sregs[id], s, len);
 	sregs_dat[id] = NULL;
 }
@@ -166,7 +166,7 @@
 
 static struct env *env_alloc(void)
 {
-	struct env *env = malloc(sizeof(*env));
+	struct env *env = xmalloc(sizeof(*env));
 	memset(env, 0, sizeof(*env));
 	wb_init(&env->wb);
 	env->fmt = fmt_alloc();
--- a/roff.c
+++ b/roff.c
@@ -25,6 +25,14 @@
 	exit(1);
 }
 
+void *xmalloc(long len)
+{
+	void *m = malloc(len);
+	if (!m)
+		errdie("neatroff: malloc() failed\n");
+	return m;
+}
+
 static char *usage =
 	"Usage: neatroff [options] input\n\n"
 	"Options:\n"
--- a/roff.h
+++ b/roff.h
@@ -411,6 +411,7 @@
 /* helpers */
 void errmsg(char *msg, ...);
 void errdie(char *msg);
+void *xmalloc(long len);
 int utf8len(int c);
 int utf8next(char *s, int (*next)(void));
 int utf8read(char **s, char *d);
--- a/sbuf.c
+++ b/sbuf.c
@@ -11,7 +11,7 @@
 {
 	char *s = sbuf->s;
 	sbuf->sz = (MAX(1, amount) + SBUF_SZ - 1) & ~(SBUF_SZ - 1);
-	sbuf->s = malloc(sbuf->sz);
+	sbuf->s = xmalloc(sbuf->sz);
 	if (sbuf->n)
 		memcpy(sbuf->s, s, sbuf->n);
 	free(s);
--- a/tr.c
+++ b/tr.c
@@ -79,7 +79,7 @@
 	reg = map(args[1]);
 	s1 = str_get(reg) ? str_get(reg) : "";
 	s2 = args[2];
-	s = malloc(strlen(s1) + strlen(s2) + 1);
+	s = xmalloc(strlen(s1) + strlen(s2) + 1);
 	strcpy(s, s1);
 	strcat(s, s2);
 	str_set(reg, s);
@@ -626,7 +626,7 @@
 		i = cdef_n++;
 	if (i >= 0) {
 		strncpy(cdef_src[i], c, sizeof(cdef_src[i]) - 1);
-		cdef_dst[i] = malloc(strlen(def) + 1);
+		cdef_dst[i] = xmalloc(strlen(def) + 1);
 		strcpy(cdef_dst[i], def);
 		cdef_fn[i] = fn ? dev_pos(fn) : 0;
 	}