Module Name:    src
Committed By:   rillig
Date:           Sat Sep 25 08:23:32 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c indent.h io.c lexi.c pr_comment.c

Log Message:
indent: add nonnull memory allocation functions

The only functional change is a single error message.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.16 -r1.17 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.56 -r1.57 src/usr.bin/indent/io.c
cvs rdiff -u -r1.50 -r1.51 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.67 src/usr.bin/indent/indent.c:1.68
--- src/usr.bin/indent/indent.c:1.67	Sat Sep 25 08:04:13 2021
+++ src/usr.bin/indent/indent.c	Sat Sep 25 08:23:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.67 2021/09/25 08:04:13 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.68 2021/09/25 08:23:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)indent.c	5.1
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.67 2021/09/25 08:04:13 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.68 2021/09/25 08:23:31 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -126,9 +126,7 @@ check_size_code(size_t desired_size)
 
     size_t nsize = code.l - code.s + 400 + desired_size;
     size_t code_len = code.e - code.s;
-    code.buf = realloc(code.buf, nsize);
-    if (code.buf == NULL)
-	err(1, NULL);
+    code.buf = xrealloc(code.buf, nsize);
     code.e = code.buf + code_len + 1;
     code.l = code.buf + nsize - 5;
     code.s = code.buf + 1;
@@ -142,9 +140,7 @@ check_size_label(size_t desired_size)
 
     size_t nsize = lab.l - lab.s + 400 + desired_size;
     size_t label_len = lab.e - lab.s;
-    lab.buf = realloc(lab.buf, nsize);
-    if (lab.buf == NULL)
-	err(1, NULL);
+    lab.buf = xrealloc(lab.buf, nsize);
     lab.e = lab.buf + label_len + 1;
     lab.l = lab.buf + nsize - 5;
     lab.s = lab.buf + 1;
@@ -362,18 +358,10 @@ main_init_globals(void)
     ps.last_nl = true;		/* this is true if the last thing scanned was
 				 * a newline */
     ps.last_token = semicolon;
-    com.buf = malloc(bufsize);
-    if (com.buf == NULL)
-	err(1, NULL);
-    lab.buf = malloc(bufsize);
-    if (lab.buf == NULL)
-	err(1, NULL);
-    code.buf = malloc(bufsize);
-    if (code.buf == NULL)
-	err(1, NULL);
-    token.buf = malloc(bufsize);
-    if (token.buf == NULL)
-	err(1, NULL);
+    com.buf = xmalloc(bufsize);
+    lab.buf = xmalloc(bufsize);
+    code.buf = xmalloc(bufsize);
+    token.buf = xmalloc(bufsize);
     alloc_typenames();
     init_constant_tt();
     com.l = com.buf + bufsize - 5;
@@ -389,9 +377,7 @@ main_init_globals(void)
     com.s = com.e = com.buf + 1;
     token.s = token.e = token.buf + 1;
 
-    in_buffer = malloc(10);
-    if (in_buffer == NULL)
-	err(1, NULL);
+    in_buffer = xmalloc(10);
     in_buffer_limit = in_buffer + 8;
     buf_ptr = buf_end = in_buffer;
     line_no = 1;
@@ -1571,3 +1557,29 @@ debug_vis_range(const char *prefix, cons
     debug_printf("%s", suffix);
 }
 #endif
+
+static void *
+nonnull(void *p)
+{
+    if (p == NULL)
+	err(EXIT_FAILURE, NULL);
+    return p;
+}
+
+void *
+xmalloc(size_t size)
+{
+    return nonnull(malloc(size));
+}
+
+void *
+xrealloc(void *p, size_t new_size)
+{
+    return nonnull(realloc(p, new_size));
+}
+
+char *
+xstrdup(const char *s)
+{
+    return nonnull(strdup(s));
+}

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.16 src/usr.bin/indent/indent.h:1.17
--- src/usr.bin/indent/indent.h:1.16	Sun Mar 14 00:33:25 2021
+++ src/usr.bin/indent/indent.h	Sat Sep 25 08:23:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.16 2021/03/14 00:33:25 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.17 2021/09/25 08:23:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -68,3 +68,7 @@ void		process_comment(void);
 void		set_defaults(void);
 void		set_option(char *);
 void		set_profile(const char *);
+
+void		*xmalloc(size_t);
+void		*xrealloc(void *, size_t);
+char		*xstrdup(const char *);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.56 src/usr.bin/indent/io.c:1.57
--- src/usr.bin/indent/io.c:1.56	Sat Sep 25 08:04:13 2021
+++ src/usr.bin/indent/io.c	Sat Sep 25 08:23:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.56 2021/09/25 08:04:13 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.57 2021/09/25 08:23:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.56 2021/09/25 08:04:13 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.57 2021/09/25 08:23:31 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -413,9 +413,7 @@ fill_buffer(void)
 	if (p >= in_buffer_limit) {
 	    size_t size = (in_buffer_limit - in_buffer) * 2 + 10;
 	    size_t offset = p - in_buffer;
-	    in_buffer = realloc(in_buffer, size);
-	    if (in_buffer == NULL)
-		errx(1, "input line too long");
+	    in_buffer = xrealloc(in_buffer, size);
 	    p = in_buffer + offset;
 	    in_buffer_limit = in_buffer + size - 2;
 	}

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.50 src/usr.bin/indent/lexi.c:1.51
--- src/usr.bin/indent/lexi.c:1.50	Sat Sep 25 08:04:13 2021
+++ src/usr.bin/indent/lexi.c	Sat Sep 25 08:23:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.50 2021/09/25 08:04:13 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.51 2021/09/25 08:23:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.50 2021/09/25 08:04:13 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.51 2021/09/25 08:23:31 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -211,9 +211,7 @@ check_size_token(size_t desired_size)
 
     size_t nsize = token.l - token.s + 400 + desired_size;
     size_t token_len = token.e - token.s;
-    token.buf = realloc(token.buf, nsize);
-    if (token.buf == NULL)
-	err(1, NULL);
+    token.buf = xrealloc(token.buf, nsize);
     token.e = token.buf + token_len + 1;
     token.l = token.buf + nsize - 5;
     token.s = token.buf + 1;
@@ -681,30 +679,25 @@ void
 alloc_typenames(void)
 {
 
-    typenames = malloc(sizeof(typenames[0]) * (typename_count = 16));
-    if (typenames == NULL)
-	err(1, NULL);
+    typenames = xmalloc(sizeof(typenames[0]) * (typename_count = 16));
 }
 
 void
 add_typename(const char *key)
 {
     int comparison;
-    const char *copy;
 
     if (typename_top + 1 >= typename_count) {
-	typenames = realloc((void *)typenames,
+	typenames = xrealloc((void *)typenames,
 	    sizeof(typenames[0]) * (typename_count *= 2));
-	if (typenames == NULL)
-	    err(1, NULL);
     }
     if (typename_top == -1)
-	typenames[++typename_top] = copy = strdup(key);
+	typenames[++typename_top] = xstrdup(key);
     else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
 	/* take advantage of sorted input */
 	if (comparison == 0)	/* remove duplicates */
 	    return;
-	typenames[++typename_top] = copy = strdup(key);
+	typenames[++typename_top] = xstrdup(key);
     } else {
 	int p;
 
@@ -714,9 +707,6 @@ add_typename(const char *key)
 	    return;
 	memmove(&typenames[p + 1], &typenames[p],
 	    sizeof(typenames[0]) * (++typename_top - p));
-	typenames[p] = copy = strdup(key);
+	typenames[p] = xstrdup(key);
     }
-
-    if (copy == NULL)
-	err(1, NULL);
 }

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.39 src/usr.bin/indent/pr_comment.c:1.40
--- src/usr.bin/indent/pr_comment.c:1.39	Sat Sep 25 08:04:13 2021
+++ src/usr.bin/indent/pr_comment.c	Sat Sep 25 08:23:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.39 2021/09/25 08:04:13 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.40 2021/09/25 08:23:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)pr_comment.c
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.39 2021/09/25 08:04:13 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.40 2021/09/25 08:23:31 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -67,9 +67,7 @@ check_size_comment(size_t desired_size)
 
     size_t nsize = com.l - com.s + 400 + desired_size;
     size_t com_len = com.e - com.s;
-    com.buf = realloc(com.buf, nsize);
-    if (com.buf == NULL)
-	err(1, NULL);
+    com.buf = xrealloc(com.buf, nsize);
     com.s = com.buf + 1;
     com.e = com.s + com_len;
     com.l = com.buf + nsize - 5;

Reply via email to