summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-05-08 11:24:31 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-05-08 11:24:31 -0700
commit1aeb6a2d1673c1ae23763f55f09fb18d8e188fe0 (patch)
treec6693b45c8bb9889606f94e6f7d8e6bdad0e10d4
parent3ff95c472abe8eb8fe1ccda5cd40d99407f1f9a4 (diff)
Convert code to use Xmallocarray() & Xreallocarray()
Provides automatic integer overflow checking in allocation size calculations Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--src/encparse.c17
-rw-r--r--src/fontenc.c3
2 files changed, 11 insertions, 9 deletions
diff --git a/src/encparse.c b/src/encparse.c
index 548110e..b50b193 100644
--- a/src/encparse.c
+++ b/src/encparse.c
@@ -45,6 +45,7 @@ typedef gzFile FontFilePtr;
#include <X11/fonts/fontenc.h>
#include "fontencI.h"
+#include "reallocarray.h"
#define MAXALIASES 20
@@ -453,7 +454,7 @@ setCode(unsigned from, unsigned to, unsigned row_size,
return 0;
if (*encsize == 0) {
*encsize = (index < 256) ? 256 : 0x10000;
- *enc = malloc((*encsize) * sizeof(unsigned short));
+ *enc = Xmallocarray(*encsize, sizeof(unsigned short));
if (*enc == NULL) {
*encsize = 0;
return 1;
@@ -461,8 +462,8 @@ setCode(unsigned from, unsigned to, unsigned row_size,
}
else if (*encsize <= index) {
*encsize = 0x10000;
- if ((newenc =
- realloc(*enc, (*encsize) * sizeof(unsigned short))) == NULL)
+ newenc = Xreallocarray(*enc, *encsize, sizeof(unsigned short));
+ if (newenc == NULL)
return 1;
*enc = newenc;
}
@@ -634,7 +635,7 @@ parseEncodingFile(FontFilePtr f, int headerOnly)
sm->first = first;
sm->len = last - first + 1;
- newmap = malloc(sm->len * sizeof(unsigned short));
+ newmap = Xmallocarray(sm->len, sizeof(unsigned short));
if (newmap == NULL) {
free(sm);
mapping->client_data = sm = NULL;
@@ -719,7 +720,7 @@ parseEncodingFile(FontFilePtr f, int headerOnly)
}
sn->first = first;
sn->len = last - first + 1;
- sn->map = malloc(sn->len * sizeof(char *));
+ sn->map = Xmallocarray(sn->len, sizeof(char *));
if (sn->map == NULL) {
free(sn);
mapping->client_data = sn = NULL;
@@ -737,7 +738,7 @@ parseEncodingFile(FontFilePtr f, int headerOnly)
goto string_mapping;
if (namsize == 0) {
namsize = (value1) < 256 ? 256 : 0x10000;
- nam = malloc(namsize * sizeof(char *));
+ nam = Xmallocarray(namsize, sizeof(char *));
if (nam == NULL) {
namsize = 0;
goto error;
@@ -786,7 +787,7 @@ parseEncodingFile(FontFilePtr f, int headerOnly)
encoding->aliases = NULL;
if (numaliases) {
- encoding->aliases = malloc((numaliases + 1) * sizeof(char *));
+ encoding->aliases = Xmallocarray(numaliases + 1, sizeof(char *));
if (encoding->aliases == NULL)
goto error;
for (i = 0; i < numaliases; i++)
@@ -986,7 +987,7 @@ FontEncIdentify(const char *fileName)
for (alias = encoding->aliases; *alias; alias++)
numaliases++;
- names = malloc((numaliases + 2) * sizeof(char *));
+ names = Xmallocarray(numaliases + 2, sizeof(char *));
if (names == NULL) {
free(encoding->aliases);
free(encoding);
diff --git a/src/fontenc.c b/src/fontenc.c
index c4ccd5e..f5675d8 100644
--- a/src/fontenc.c
+++ b/src/fontenc.c
@@ -33,6 +33,7 @@ THE SOFTWARE.
#include <X11/fonts/fontenc.h>
#include "fontencI.h"
+#include "reallocarray.h"
/* Functions local to this file */
@@ -808,7 +809,7 @@ FontEncLoad(const char *encoding_name, const char *filename)
for (alias = encoding->aliases; *alias; alias++)
numaliases++;
}
- new_aliases = malloc((numaliases + 2) * sizeof(char *));
+ new_aliases = Xmallocarray(numaliases + 2, sizeof(char *));
if (new_aliases == NULL) {
free(new_name);
return NULL;