source@mandoc.bsd.lv
 help / color / Atom feed
* docbook2mdoc: In this program, there is never a need to survive memory 
@ 2019-04-28 15:03 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-04-28 15:03 UTC (permalink / raw)
  To: source

Log Message:
-----------
In this program, there is never a need to survive memory allocation
failure, and there are many places allocating memory.  Consequently,
the code can be simplified providing memory allocation functions
that error out on failure, in the conventional way.

Modified Files:
--------------
    docbook2mdoc:
        Makefile
        docbook2mdoc.c
        main.c
        node.c
        parse.c
        statistics.c

Added Files:
-----------
    docbook2mdoc:
        xmalloc.c
        xmalloc.h

Revision Data
-------------
--- /dev/null
+++ xmalloc.c
@@ -0,0 +1,101 @@
+/* $Id: xmalloc.c,v 1.1 2019/04/28 15:03:29 schwarze Exp $ */
+/*
+ * Copyright (c) 2019 Ingo Schwarze <schwarze@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.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <sys/types.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "xmalloc.h"
+
+/*
+ * The implementation of the exiting allocator functions.
+ */
+
+void *
+xcalloc(size_t nmemb, size_t size)
+{
+	void	*p;
+
+	if ((p = calloc(nmemb, size)) == NULL) {
+		perror(NULL);
+		exit(6);
+	}
+	return p;
+}
+
+void *
+xrealloc(void *p, size_t size)
+{
+	if ((p = realloc(p, size)) == NULL) {
+		perror(NULL);
+		exit(6);
+	}
+	return p;
+}
+
+void *
+xreallocarray(void *p, size_t nmemb, size_t size)
+{
+	if ((p = reallocarray(p, nmemb, size)) == NULL) {
+		perror(NULL);
+		exit(6);
+	}
+	return p;
+}
+
+char *
+xstrdup(const char *in)
+{
+	char	*out;
+
+	if ((out = strdup(in)) == NULL) {
+		perror(NULL);
+		exit(6);
+	}
+	return out;
+}
+
+char *
+xstrndup(const char *in, size_t maxlen)
+{
+	char	*out;
+
+	if ((out = strndup(in, maxlen)) == NULL) {
+		perror(NULL);
+		exit(6);
+	}
+	return out;
+}
+
+int
+xasprintf(char **dest, const char *fmt, ...)
+{
+	va_list	 ap;
+	int	 ret;
+
+	va_start(ap, fmt);
+	ret = vasprintf(dest, fmt, ap);
+	va_end(ap);
+
+	if (ret == -1) {
+		perror(NULL);
+		exit(6);
+	}
+	return ret;
+}
Index: statistics.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/statistics.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Lstatistics.c -Lstatistics.c -u -p -r1.36 -r1.37
--- statistics.c
+++ statistics.c
@@ -14,6 +14,8 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+#include <sys/types.h>
+
 #include <assert.h>
 #include <ctype.h>
 #include <err.h>
@@ -24,6 +26,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "xmalloc.h"
+
 /*
  * Count parent-child element relations in a corpus of DocBook documents.
  *
@@ -125,19 +129,13 @@ table_add(const char *parent, const char
 
 	if (tablei == tablesz) {
 		tablesz += 64;
-		table = reallocarray(table, tablesz, sizeof(*table));
-		if (table == NULL)
-			err(1, NULL);
+		table = xreallocarray(table, tablesz, sizeof(*table));
 	}
 
 	/* Add a new entry to the table. */
 
-	if ((table[tablei].parent = strdup(parent)) == NULL)
-		err(1, NULL);
-	if (child == NULL)
-		table[tablei].child = NULL;
-	else if ((table[tablei].child = strdup(child)) == NULL)
-		err(1, NULL);
+	table[tablei].parent = xstrdup(parent);
+	table[tablei].child = child == NULL ? NULL : xstrdup(child);
 	table[tablei++].count = init_done ? 1 : -1;
 }
 
@@ -152,12 +150,9 @@ stack_push(const char *name)
 
 	if (stacki == stacksz) {
 		stacksz += 8;
-		stack = reallocarray(stack, stacksz, sizeof(*stack));
-		if (stack == NULL)
-			err(1, NULL);
+		stack = xreallocarray(stack, stacksz, sizeof(*stack));
 	}
-	if ((stack[stacki++] = strdup(name)) == NULL)
-		err(1, NULL);
+	stack[stacki++] = xstrdup(name);
 }
 
 /*
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/main.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lmain.c -Lmain.c -u -p -r1.6 -r1.7
--- main.c
+++ main.c
@@ -94,10 +94,7 @@ main(int argc, char *argv[])
 
 	/* Parse. */
 
-	if ((parser = parse_alloc(warn)) == NULL) {
-		perror(NULL);
-		return 6;
-	}
+	parser = parse_alloc(warn);
 	tree = parse_file(parser, fd, fname);
 	rc = tree->flags & TREE_ERROR ? 3 : tree->flags & TREE_WARN ? 2 : 0;
 
Index: node.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/node.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -Lnode.c -Lnode.c -u -p -r1.23 -r1.24
--- node.c
+++ node.c
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "xmalloc.h"
 #include "node.h"
 
 /*
@@ -252,12 +253,11 @@ pnode_alloc(struct pnode *np)
 {
 	struct pnode	*n;
 
-	if ((n = calloc(1, sizeof(*n))) != NULL) {
-		TAILQ_INIT(&n->childq);
-		TAILQ_INIT(&n->attrq);
-		if ((n->parent = np) != NULL)
-			TAILQ_INSERT_TAIL(&np->childq, n, child);
-	}
+	n = xcalloc(1, sizeof(*n));
+	TAILQ_INIT(&n->childq);
+	TAILQ_INIT(&n->attrq);
+	if ((n->parent = np) != NULL)
+		TAILQ_INSERT_TAIL(&np->childq, n, child);
 	return n;
 }
 
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/Makefile,v
retrieving revision 1.26
retrieving revision 1.27
diff -LMakefile -LMakefile -u -p -r1.26 -r1.27
--- Makefile
+++ Makefile
@@ -3,9 +3,9 @@ CFLAGS += -g -W -Wall -Wstrict-prototype
 WWWPREFIX = /var/www/vhosts/mdocml.bsd.lv/htdocs/docbook2mdoc
 PREFIX = /usr/local
 
-HEADS =	node.h parse.h macro.h format.h
-SRCS =	node.c parse.c macro.c docbook2mdoc.c tree.c main.c
-OBJS =	node.o parse.o macro.o docbook2mdoc.o tree.o main.o
+HEADS =	xmalloc.h node.h parse.h macro.h format.h
+SRCS =	xmalloc.c node.c parse.c macro.c docbook2mdoc.c tree.c main.c
+OBJS =	xmalloc.o node.o parse.o macro.o docbook2mdoc.o tree.o main.o
 DISTFILES = Makefile NEWS docbook2mdoc.1
 
 all: docbook2mdoc
@@ -13,8 +13,8 @@ all: docbook2mdoc
 docbook2mdoc: $(OBJS)
 	$(CC) -g -o $@ $(OBJS)
 
-statistics: statistics.o
-	$(CC) -g -o $@ statistics.c
+statistics: statistics.o xmalloc.o
+	$(CC) -g -o $@ statistics.o xmalloc.o
 
 www: docbook2mdoc.1.html docbook2mdoc-$(VERSION).tgz README.txt
 
@@ -40,12 +40,14 @@ docbook2mdoc-$(VERSION).tgz:
 	(cd .dist && tar zcf ../$@ docbook2mdoc-$(VERSION))
 	rm -rf .dist
 
-node.o: node.h
-parse.o: node.h parse.h
+xmalloc.o: xmalloc.h
+node.o: xmalloc.h node.h
+parse.o: xmalloc.h node.h parse.h
 macro.o: node.h macro.h
-docbook2mdoc.o: node.h macro.h format.h
+docbook2mdoc.o: xmalloc.h node.h macro.h format.h
 tree.o: node.h format.h
 main.o: node.h parse.h format.h
+statistics.c: xmalloc.h
 
 docbook2mdoc.1.html: docbook2mdoc.1
 	mandoc -T html -O style=/mandoc.css docbook2mdoc.1 >$@
--- /dev/null
+++ xmalloc.h
@@ -0,0 +1,29 @@
+/* $Id: xmalloc.h,v 1.1 2019/04/28 15:03:29 schwarze Exp $ */
+/*
+ * Copyright (c) 2019 Ingo Schwarze <schwarze@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.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * The interface of the exiting allocator functions.
+ */
+
+void	*xcalloc(size_t, size_t);
+void	*xrealloc(void *, size_t);
+void	*xreallocarray(void *, size_t, size_t);
+
+char	*xstrdup(const char *);
+char	*xstrndup(const char *, size_t);
+
+int	 xasprintf(char **, const char *, ...);
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.52
retrieving revision 1.53
diff -Lparse.c -Lparse.c -u -p -r1.52 -r1.53
--- parse.c
+++ parse.c
@@ -15,6 +15,8 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+#include <sys/types.h>
+
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
@@ -26,6 +28,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "xmalloc.h"
 #include "node.h"
 #include "parse.h"
 
@@ -192,14 +195,6 @@ static void	 parse_fd(struct parse *, in
 
 
 static void
-fatal(struct parse *p)
-{
-	fprintf(stderr, "%s:%d:%d: FATAL: ", p->fname, p->line, p->col);
-	perror(NULL);
-	exit(6);
-}
-
-static void
 error_msg(struct parse *p, const char *fmt, ...)
 {
 	va_list		 ap;
@@ -257,8 +252,7 @@ xml_text(struct parse *p, const char *wo
 		newsz = oldsz + sz;
 		if (oldsz && (p->flags & PFLAG_SPC))
 			newsz++;
-		if ((n->b = realloc(n->b, newsz + 1)) == NULL)
-			fatal(p);
+		n->b = xrealloc(n->b, newsz + 1);
 		if (oldsz && (p->flags & PFLAG_SPC))
 			n->b[oldsz++] = ' ';
 		memcpy(n->b + oldsz, word, sz);
@@ -272,8 +266,7 @@ xml_text(struct parse *p, const char *wo
 
 	/* Create a new text node. */
 
-	if ((n = pnode_alloc(p->cur)) == NULL)
-		fatal(p);
+	n = pnode_alloc(p->cur);
 	n->node = NODE_TEXT;
 	n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) |
 	    ((p->flags & PFLAG_SPC) ? NFLAG_SPC : 0);
@@ -307,8 +300,7 @@ xml_text(struct parse *p, const char *wo
 		i = 0;
 		while (i < sz && !isspace((unsigned char)word[i]))
 			i++;
-		if ((n->b = strndup(word, i)) == NULL)
-			fatal(p);
+		n->b = xstrndup(word, i);
 		if (i == sz)
 			return;
 		while (i < sz && isspace((unsigned char)word[i]))
@@ -320,15 +312,13 @@ xml_text(struct parse *p, const char *wo
 
 		/* Put any remaining text into a second node. */
 
-		if ((n = pnode_alloc(p->cur)) == NULL)
-			fatal(p);
+		n = pnode_alloc(p->cur);
 		n->node = NODE_TEXT;
 		n->flags |= NFLAG_SPC;
 		word += i;
 		sz -= i;
 	}
-	if ((n->b = strndup(word, sz)) == NULL)
-		fatal(p);
+	n->b = xstrndup(word, sz);
 
 	/* The new node remains open for later pnode_closetext(). */
 
@@ -371,12 +361,10 @@ pnode_closetext(struct parse *p, int che
 
 	/* Move the last word into its own node, for use with .Pf. */
 
-	if ((n = pnode_alloc(p->cur)) == NULL)
-		fatal(p);
+	n = pnode_alloc(p->cur);
 	n->node = NODE_TEXT;
 	n->flags |= NFLAG_SPC;
-	if ((n->b = strdup(last_word)) == NULL)
-		fatal(p);
+	n->b = xstrdup(last_word);
 }
 
 static void
@@ -422,8 +410,7 @@ xml_entity(struct parse *p, const char *
 				if ((ccp = pnode_getattr_raw(n,
 				     ATTRKEY_DEFINITION, NULL)) == NULL)
 					continue;
-				if ((cp = strdup(ccp)) == NULL)
-					fatal(p);
+				cp = xstrdup(ccp);
 				pstate = PARSE_ELEM;
 				parse_string(p, cp, strlen(cp), &pstate, 0);
 				p->flags &= ~(PFLAG_LINE | PFLAG_SPC);
@@ -434,10 +421,8 @@ xml_entity(struct parse *p, const char *
 		if (*name == '#') {
 			codepoint = strtonum(name + 1, 0, 0x10ffff, &ccp);
 			if (ccp == NULL) {
-				if ((n = pnode_alloc(p->cur)) == NULL ||
-				    asprintf(&n->b, "\\[u%4.4X]",
-				    codepoint) < 0)
-					fatal(p);
+				n = pnode_alloc(p->cur);
+				xasprintf(&n->b, "\\[u%4.4X]", codepoint);
 				goto done;
 			}
 		}
@@ -446,9 +431,8 @@ xml_entity(struct parse *p, const char *
 	}
 
 	/* Create, append, and close out an entity node. */
-	if ((n = pnode_alloc(p->cur)) == NULL ||
-	    (n->b = strdup(entity->roff)) == NULL)
-		fatal(p);
+	n = pnode_alloc(p->cur);
+	n->b = xstrdup(entity->roff);
 done:
 	n->node = NODE_ESCAPE;
 	n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) |
@@ -523,8 +507,7 @@ xml_elem_start(struct parse *p, const ch
 		break;
 	}
 
-	if ((n = pnode_alloc(p->cur)) == NULL)
-		fatal(p);
+	n = pnode_alloc(p->cur);
 
 	/*
 	 * Some elements are self-closing.
@@ -586,17 +569,14 @@ xml_attrkey(struct parse *p, const char 
 		p->flags &= ~PFLAG_ATTR;
 		return;
 	}
-	if ((a = calloc(1, sizeof(*a))) == NULL)
-		fatal(p);
-
+	a = xcalloc(1, sizeof(*a));
 	a->key = key;
 	a->val = ATTRVAL__MAX;
 	if (value == NULL) {
 		a->rawval = NULL;
 		p->flags |= PFLAG_ATTR;
 	} else {
-		if ((a->rawval = strdup(value)) == NULL)
-			fatal(p);
+		a->rawval = xstrdup(value);
 		p->flags &= ~PFLAG_ATTR;
 	}
 	TAILQ_INSERT_TAIL(&p->cur->attrq, a, child);
@@ -614,9 +594,8 @@ xml_attrval(struct parse *p, const char 
 		return;
 	if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL)
 		return;
-	if ((a->val = attrval_parse(name)) == ATTRVAL__MAX &&
-	    (a->rawval = strdup(name)) == NULL)
-		fatal(p);
+	if ((a->val = attrval_parse(name)) == ATTRVAL__MAX)
+		a->rawval = xstrdup(name);
 	p->flags &= ~PFLAG_ATTR;
 }
 
@@ -711,13 +690,8 @@ parse_alloc(int warn)
 {
 	struct parse	*p;
 
-	if ((p = calloc(1, sizeof(*p))) == NULL)
-		return NULL;
-
-	if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) {
-		free(p);
-		return NULL;
-	}
+	p = xcalloc(1, sizeof(*p));
+	p->tree = xcalloc(1, sizeof(*p->tree));
 	if (warn)
 		p->flags |= PFLAG_WARN;
 	else
Index: docbook2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.138
retrieving revision 1.139
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.138 -r1.139
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "xmalloc.h"
 #include "node.h"
 #include "macro.h"
 #include "format.h"
@@ -888,30 +889,18 @@ pnode_printrefentry(struct format *f, st
 	 */
 
 	if (match == NULL) {
-		if ((match = calloc(1, sizeof(*match))) == NULL) {
-			perror(NULL);
-			exit(1);
-		}
+		match = xcalloc(1, sizeof(*match));
 		match->node = NODE_SECTION;
 		match->flags |= NFLAG_SPC;
 		match->parent = n;
 		TAILQ_INIT(&match->childq);
 		TAILQ_INIT(&match->attrq);
-		if ((nc = pnode_alloc(match)) == NULL) {
-			perror(NULL);
-			exit(1);
-		}
+		nc = pnode_alloc(match);
 		nc->node = NODE_TITLE;
 		nc->flags |= NFLAG_SPC;
-		if ((nc = pnode_alloc(nc)) == NULL) {
-			perror(NULL);
-			exit(1);
-		}
+		nc = pnode_alloc(nc);
 		nc->node = NODE_TEXT;
-		if ((nc->b = strdup("AUTHORS")) == NULL) {
-			perror(NULL);
-			exit(1);
-		}
+		nc->b = xstrdup("AUTHORS");
 		nc->flags |= NFLAG_SPC;
 		if (later == NULL)
 			TAILQ_INSERT_TAIL(&n->childq, match, child);
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-28 15:03 docbook2mdoc: In this program, there is never a need to survive memory schwarze

source@mandoc.bsd.lv

Archives are clonable: git clone --mirror http://inbox.vuxu.org/mandoc-source

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.mandoc.source


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git