[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 07/11: [grn]: Add and use `malloc()` wrapper.
From: |
G. Branden Robinson |
Subject: |
[groff] 07/11: [grn]: Add and use `malloc()` wrapper. |
Date: |
Mon, 16 Aug 2021 00:01:45 -0400 (EDT) |
gbranden pushed a commit to branch master
in repository groff.
commit 0d56933b896e7bc2c7193daf7b740fe06e9196f6
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Aug 16 10:19:39 2021 +1000
[grn]: Add and use `malloc()` wrapper.
* /src/preproc/grn/main.cpp (grnmalloc): New function takes argument of
`size_t` type and constant string argument to describe what is being
allocated. Return non-null pointer from `malloc()`, otherwise call
`fatal()`, describing what was being allocated and the problem
reported by the system.
* src/preproc/grn/hdb.cpp (DBCreateElt):
* src/preproc/grn/hpoint.cpp (PTMakePoint):
* /src/preproc/grn/main.cpp (main, interpret): Migrate `malloc()`
callers to `grnmalloc()`.
---
ChangeLog | 15 +++++++++++++++
src/preproc/grn/hdb.cpp | 11 ++++++++---
src/preproc/grn/hpoint.cpp | 12 +++++++++---
src/preproc/grn/main.cpp | 33 ++++++++++++++++++++++++---------
4 files changed, 56 insertions(+), 15 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 7bdee9a..7959f83 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2021-08-16 G. Branden Robinson <g.branden.robinson@gmail.com>
+ [grn]: Add and use `malloc()` wrapper.
+
+ * /src/preproc/grn/main.cpp (grnmalloc): New function takes
+ argument of `size_t` type and constant string argument to
+ describe what is being allocated. Return non-null pointer from
+ `malloc()`, otherwise call `fatal()`, describing what was being
+ allocated and the problem reported by the system.
+
+ * src/preproc/grn/hdb.cpp (DBCreateElt):
+ * src/preproc/grn/hpoint.cpp (PTMakePoint):
+ * /src/preproc/grn/main.cpp (main, interpret): Migrate
+ `malloc()` callers to `grnmalloc()`.
+
+2021-08-16 G. Branden Robinson <g.branden.robinson@gmail.com>
+
* src/preproc/grn/hdb.cpp (DBRead): Check return value of
`sscanf()` and call `fatal()` if no conversions succeeded. The
blithe discard of a useful return value is bad enough, but this
diff --git a/src/preproc/grn/hdb.cpp b/src/preproc/grn/hdb.cpp
index 0310d7a..77d3ba6 100644
--- a/src/preproc/grn/hdb.cpp
+++ b/src/preproc/grn/hdb.cpp
@@ -27,6 +27,7 @@ extern int linenum; /* current line number in input
file */
extern char gremlinfile[]; /* name of file currently reading */
extern int SUNFILE; /* TRUE if SUN gremlin file */
extern int compatibility_flag; /* TRUE if in compatibility mode */
+extern void *grnmalloc(size_t size, const char *what);
extern void savebounds(double x, double y);
/* imports from hpoint.cpp */
@@ -63,7 +64,7 @@ DBCreateElt(int type,
{
register ELT *temp;
- temp = (ELT *) malloc(sizeof(ELT));
+ temp = (ELT *) grnmalloc(sizeof(ELT), "picture element");
temp->nextelt = *db;
temp->type = type;
temp->ptlist = pointlist;
@@ -192,7 +193,7 @@ DBRead(register FILE *file)
(void) fscanf(file, "%d%d\n", &brush, &size);
(void) fscanf(file, "%d", &len); /* text length */
(void) getc(file); /* eat blank */
- txt = (char *) malloc((unsigned) len + 1);
+ txt = (char *) grnmalloc((unsigned) len + 1, "element text");
for (i = 0; i < len; ++i) { /* read text */
int c = getc(file);
if (c == EOF)
@@ -354,4 +355,8 @@ xscanf(FILE *f,
}
#endif /* UW_FASTSCAN */
-/* EOF */
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
diff --git a/src/preproc/grn/hpoint.cpp b/src/preproc/grn/hpoint.cpp
index b581cb0..94779c1 100644
--- a/src/preproc/grn/hpoint.cpp
+++ b/src/preproc/grn/hpoint.cpp
@@ -12,6 +12,8 @@
#include <stdlib.h>
#include "gprint.h"
+/* imports from main.cpp */
+extern void *grnmalloc(size_t size, const char *what);
/*
* Return pointer to empty point list.
@@ -35,12 +37,12 @@ PTMakePoint(double x,
register POINT *pt;
if (Nullpoint(pt = *pplist)) { /* empty list */
- *pplist = (POINT *) malloc(sizeof(POINT));
+ *pplist = (POINT *) grnmalloc(sizeof(POINT), "initial point");
pt = *pplist;
} else {
while (!Nullpoint(pt->nextpt))
pt = pt->nextpt;
- pt->nextpt = (POINT *) malloc(sizeof(POINT));
+ pt->nextpt = (POINT *) grnmalloc(sizeof(POINT), "subsequent point");
pt = pt->nextpt;
}
@@ -50,4 +52,8 @@ PTMakePoint(double x,
return (pt);
} /* end PTMakePoint */
-/* EOF */
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
diff --git a/src/preproc/grn/main.cpp b/src/preproc/grn/main.cpp
index 9752a16..8b7f198 100644
--- a/src/preproc/grn/main.cpp
+++ b/src/preproc/grn/main.cpp
@@ -71,6 +71,7 @@
#include <ctype.h>
#include <stdlib.h>
+#include <errno.h> // errno
#include "gprint.h"
#include "device.h"
@@ -236,6 +237,17 @@ void savestate();
int has_polygon(register ELT *elist);
void interpret(char *line);
+void *
+grnmalloc(size_t size,
+ const char *what)
+{
+ void *ptr = 0;
+ ptr = malloc(size);
+ if (!ptr) {
+ fatal("memory allocation failed for %1: %2", what, strerror(errno));
+ }
+ return ptr;
+}
void
usage(FILE *stream)
@@ -291,9 +303,8 @@ main(int argc,
int file_cur_size = INIT_FILE_SIZE;
char *operand(int *argcp, char ***argvp);
- if ((file = (char **) malloc(file_cur_size * sizeof(char *))) == NULL) {
- fatal("unable to create file array");
- }
+ file = (char **) grnmalloc(file_cur_size * sizeof(char *),
+ "file array");
while (--argc) {
if (**++argv != '-')
file = add_file(file, *argv, &gfil, &file_cur_size);
@@ -787,21 +798,21 @@ interpret(char *line)
case 'r': /* roman */
if (str2[0] < '0')
goto nofont;
- tfont[0] = (char *) malloc(strlen(str2) + 1);
+ tfont[0] = (char *) grnmalloc(strlen(str2) + 1, "roman command");
strcpy(tfont[0], str2);
break;
case 'i': /* italics */
if (str2[0] < '0')
goto nofont;
- tfont[1] = (char *) malloc(strlen(str2) + 1);
+ tfont[1] = (char *) grnmalloc(strlen(str2) + 1, "italics command");
strcpy(tfont[1], str2);
break;
case 'b': /* bold */
if (str2[0] < '0')
goto nofont;
- tfont[2] = (char *) malloc(strlen(str2) + 1);
+ tfont[2] = (char *) grnmalloc(strlen(str2) + 1, "bold command");
strcpy(tfont[2], str2);
break;
@@ -817,7 +828,7 @@ interpret(char *line)
if (str1[1] == 't')
goto stipplecommand; /* or stipple */
- tfont[3] = (char *) malloc(strlen(str2) + 1);
+ tfont[3] = (char *) grnmalloc(strlen(str2) + 1, "special command");
strcpy(tfont[3], str2);
break;
@@ -840,7 +851,7 @@ interpret(char *line)
}
stipplecommand: /* set stipple name */
- stipple = (char *) malloc(strlen(str2) + 1);
+ stipple = (char *) grnmalloc(strlen(str2) + 1, "stipple command");
strcpy(stipple, str2);
/* if it's a 'known' font (currently only 'cf'), set indices */
if (strcmp(stipple, "cf") == 0)
@@ -946,4 +957,8 @@ has_polygon(register ELT *elist)
return (0);
}
-/* EOF */
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 07/11: [grn]: Add and use `malloc()` wrapper.,
G. Branden Robinson <=