Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/indent indent: add nonnull memory allocation functions
details: https://anonhg.NetBSD.org/src/rev/8998c1857c91
branches: trunk
changeset: 1023734:8998c1857c91
user: rillig <rillig%NetBSD.org@localhost>
date: Sat Sep 25 08:23:31 2021 +0000
description:
indent: add nonnull memory allocation functions
The only functional change is a single error message.
diffstat:
usr.bin/indent/indent.c | 58 +++++++++++++++++++++++++++-----------------
usr.bin/indent/indent.h | 6 +++-
usr.bin/indent/io.c | 8 ++---
usr.bin/indent/lexi.c | 26 ++++++-------------
usr.bin/indent/pr_comment.c | 8 ++---
5 files changed, 54 insertions(+), 52 deletions(-)
diffs (254 lines):
diff -r 7c31385b828a -r 8998c1857c91 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Sat Sep 25 08:04:13 2021 +0000
+++ b/usr.bin/indent/indent.c Sat Sep 25 08:23:31 2021 +0000
@@ -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 @@
#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 @@
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 @@
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 @@
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 @@
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_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));
+}
diff -r 7c31385b828a -r 8998c1857c91 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h Sat Sep 25 08:04:13 2021 +0000
+++ b/usr.bin/indent/indent.h Sat Sep 25 08:23:31 2021 +0000
@@ -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 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 *);
diff -r 7c31385b828a -r 8998c1857c91 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c Sat Sep 25 08:04:13 2021 +0000
+++ b/usr.bin/indent/io.c Sat Sep 25 08:23:31 2021 +0000
@@ -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 @@
#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 @@
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;
}
diff -r 7c31385b828a -r 8998c1857c91 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c Sat Sep 25 08:04:13 2021 +0000
+++ b/usr.bin/indent/lexi.c Sat Sep 25 08:23:31 2021 +0000
@@ -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 @@
#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 @@
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 @@
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 @@
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);
}
diff -r 7c31385b828a -r 8998c1857c91 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c Sat Sep 25 08:04:13 2021 +0000
+++ b/usr.bin/indent/pr_comment.c Sat Sep 25 08:23:31 2021 +0000
@@ -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 @@
#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 @@
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;
Home |
Main Index |
Thread Index |
Old Index