summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2019-08-03 19:29:05 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2019-08-04 11:23:20 -0700
commitddbee30d3525cdd66b84056affc407601680cc29 (patch)
treeae251a90a3e4cd3420d361edba93a4ea3656babc
parentf54470dab5b392380df61a22b4b4bef685b6cee2 (diff)
Convert multiplying malloc calls to use mallocarray instead
Introduces mallocarray as a macro calling reallocarray with a NULL pointer for the old allocation. Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--src/FreeType/ftfuncs.c6
-rw-r--r--src/bitmap/bdfread.c9
-rw-r--r--src/bitmap/bitmaputil.c3
-rw-r--r--src/bitmap/bitscale.c7
-rw-r--r--src/bitmap/pcfread.c13
-rw-r--r--src/bitmap/pcfwrite.c3
-rw-r--r--src/bitmap/snfread.c5
-rw-r--r--src/fc/fsconvert.c3
-rw-r--r--src/fc/fserve.c2
-rw-r--r--src/fontfile/fontdir.c2
-rw-r--r--src/util/fontnames.c4
-rw-r--r--src/util/fontutil.c4
-rw-r--r--src/util/private.c2
-rw-r--r--src/util/replace.h4
14 files changed, 39 insertions, 28 deletions
diff --git a/src/FreeType/ftfuncs.c b/src/FreeType/ftfuncs.c
index f86d8dd..e98f920 100644
--- a/src/FreeType/ftfuncs.c
+++ b/src/FreeType/ftfuncs.c
@@ -637,7 +637,7 @@ FreeTypeInstanceFindGlyph(unsigned idx_in, int flags, FTInstancePtr instance,
}
if((*glyphs)[segment] == NULL) {
- (*glyphs)[segment] = malloc(sizeof(CharInfoRec) * FONTSEGMENTSIZE);
+ (*glyphs)[segment] = mallocarray(sizeof(CharInfoRec), FONTSEGMENTSIZE);
if((*glyphs)[segment] == NULL)
return AllocError;
}
@@ -1623,7 +1623,7 @@ FreeTypeAddProperties(FTFontPtr font, FontScalablePtr vals, FontInfoPtr info,
( (font_properties && (post || t1info)) ? 3 : 0 ) +
2; /* type */
- info->props = malloc(maxprops * sizeof(FontPropRec));
+ info->props = mallocarray(maxprops, sizeof(FontPropRec));
if(info->props == NULL)
return AllocError;
@@ -2816,7 +2816,7 @@ FreeTypeLoadFont(FTFontPtr font, FontInfoPtr info, FTFacePtr face,
font->nranges = vals->nranges;
font->ranges = 0;
if(font->nranges) {
- font->ranges = malloc(vals->nranges*sizeof(fsRange));
+ font->ranges = mallocarray(vals->nranges, sizeof(fsRange));
if(font->ranges == NULL)
return AllocError;
memcpy((char*)font->ranges, (char*)vals->ranges,
diff --git a/src/bitmap/bdfread.c b/src/bitmap/bdfread.c
index f2b1e22..f4340b9 100644
--- a/src/bitmap/bdfread.c
+++ b/src/bitmap/bdfread.c
@@ -53,6 +53,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <ctype.h>
#include <X11/fonts/fntfilst.h>
@@ -107,7 +108,7 @@ bdfReadBitmap(CharInfoPtr pCI, FontFilePtr file, int bit, int byte,
widthBytes = BYTES_PER_ROW(widthBits, glyph);
if (widthBytes * height > 0) {
- picture = malloc(widthBytes * height);
+ picture = mallocarray(widthBytes, height);
if (!picture) {
bdfError("Couldn't allocate picture (%d*%d)\n", widthBytes, height);
goto BAILOUT;
@@ -313,7 +314,7 @@ bdfReadCharacters(FontFilePtr file, FontPtr pFont, bdfFileState *pState,
bitmapFont->metrics = ci;
if (bitmapExtra) {
- bitmapExtra->glyphNames = malloc(nchars * sizeof(Atom));
+ bitmapExtra->glyphNames = mallocarray(nchars, sizeof(Atom));
if (!bitmapExtra->glyphNames) {
bdfError("Couldn't allocate glyphNames (%d*%d)\n",
nchars, (int) sizeof(Atom));
@@ -321,7 +322,7 @@ bdfReadCharacters(FontFilePtr file, FontPtr pFont, bdfFileState *pState,
}
}
if (bitmapExtra) {
- bitmapExtra->sWidths = malloc(nchars * sizeof(int));
+ bitmapExtra->sWidths = mallocarray(nchars, sizeof(int));
if (!bitmapExtra->sWidths) {
bdfError("Couldn't allocate sWidth (%d *%d)\n",
nchars, (int) sizeof(int));
@@ -640,7 +641,7 @@ bdfReadProperties(FontFilePtr file, FontPtr pFont, bdfFileState *pState)
pFont->info.props = NULL;
pFont->info.nprops = 0;
- stringProps = malloc((nProps + BDF_GENPROPS) * sizeof(char));
+ stringProps = mallocarray((nProps + BDF_GENPROPS), sizeof(char));
pFont->info.isStringProp = stringProps;
if (stringProps == NULL) {
bdfError("Couldn't allocate stringProps (%d*%d)\n",
diff --git a/src/bitmap/bitmaputil.c b/src/bitmap/bitmaputil.c
index 232729f..c2135a0 100644
--- a/src/bitmap/bitmaputil.c
+++ b/src/bitmap/bitmaputil.c
@@ -30,6 +30,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <X11/fonts/fntfilst.h>
#include <X11/fonts/bitmap.h>
@@ -210,7 +211,7 @@ bitmapAddInkMetrics(FontPtr pFont)
int i;
bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
- bitmapFont->ink_metrics = malloc(bitmapFont->num_chars * sizeof(xCharInfo));
+ bitmapFont->ink_metrics = mallocarray(bitmapFont->num_chars, sizeof(xCharInfo));
if (!bitmapFont->ink_metrics) {
fprintf(stderr, "Error: Couldn't allocate ink_metrics (%d*%ld)\n",
bitmapFont->num_chars, (unsigned long)sizeof(xCharInfo));
diff --git a/src/bitmap/bitscale.c b/src/bitmap/bitscale.c
index 5f77635..12d40d1 100644
--- a/src/bitmap/bitscale.c
+++ b/src/bitmap/bitscale.c
@@ -34,6 +34,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <X11/fonts/fntfilst.h>
#include <X11/fonts/bitmap.h>
@@ -585,7 +586,7 @@ ComputeScaledProperties(FontInfoPtr sourceFontInfo, /* the font to be scaled */
}
nProps = NPROPS + 1 + sizeof(fontPropTable) / sizeof(fontProp) +
sizeof(rawFontPropTable) / sizeof(fontProp);
- fp = malloc(sizeof(FontPropRec) * nProps);
+ fp = mallocarray(sizeof(FontPropRec), nProps);
*pProps = fp;
if (!fp) {
fprintf(stderr, "Error: Couldn't allocate font properties (%ld*%d)\n",
@@ -859,7 +860,7 @@ ScaleFont(FontPtr opf, /* originating font */
bitmapFont->encoding = 0;
bitmapFont->bitmapExtra = 0;
bitmapFont->pDefault = 0;
- bitmapFont->metrics = malloc(nchars * sizeof(CharInfoRec));
+ bitmapFont->metrics = mallocarray(nchars, sizeof(CharInfoRec));
if (!bitmapFont->metrics) {
fprintf(stderr, "Error: Couldn't allocate metrics (%d*%ld)\n",
nchars, (unsigned long)sizeof(CharInfoRec));
@@ -1174,7 +1175,7 @@ ScaleBitmap(FontPtr pFont, CharInfoPtr opci, CharInfoPtr pci,
/* Looks like we need to anti-alias. Create a workspace to
contain the grayscale character plus an additional row and
column for scratch */
- char_grayscale = malloc((width + 1) * (height + 1));
+ char_grayscale = mallocarray((width + 1), (height + 1));
if (char_grayscale)
{
diffusion_workspace = calloc((newWidth + 2) * 2, sizeof(int));
diff --git a/src/bitmap/pcfread.c b/src/bitmap/pcfread.c
index ae34c28..bf64d6f 100644
--- a/src/bitmap/pcfread.c
+++ b/src/bitmap/pcfread.c
@@ -34,6 +34,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <X11/fonts/fntfilst.h>
#include <X11/fonts/bitmap.h>
@@ -138,7 +139,7 @@ pcfReadTOC(FontFilePtr file, int *countp)
pcfError("pcfReadTOC(): invalid file format\n");
return NULL;
}
- tables = malloc(count * sizeof(PCFTableRec));
+ tables = mallocarray(count, sizeof(PCFTableRec));
if (!tables) {
pcfError("pcfReadTOC(): Couldn't allocate tables (%d*%d)\n",
count, (int) sizeof(PCFTableRec));
@@ -263,13 +264,13 @@ pcfGetProperties(FontInfoPtr pFontInfo, FontFilePtr file,
goto Bail;
}
if (IS_EOF(file)) goto Bail;
- props = malloc(nprops * sizeof(FontPropRec));
+ props = mallocarray(nprops, sizeof(FontPropRec));
if (!props) {
pcfError("pcfGetProperties(): Couldn't allocate props (%d*%d)\n",
nprops, (int) sizeof(FontPropRec));
goto Bail;
}
- isStringProp = malloc(nprops * sizeof(char));
+ isStringProp = mallocarray(nprops, sizeof(char));
if (!isStringProp) {
pcfError("pcfGetProperties(): Couldn't allocate isStringProp (%d*%d)\n",
nprops, (int) sizeof(char));
@@ -456,7 +457,7 @@ pcfReadFont(FontPtr pFont, FontFilePtr file,
pcfError("pcfReadFont(): invalid file format\n");
goto Bail;
}
- metrics = malloc(nmetrics * sizeof(CharInfoRec));
+ metrics = mallocarray(nmetrics, sizeof(CharInfoRec));
if (!metrics) {
pcfError("pcfReadFont(): Couldn't allocate metrics (%d*%d)\n",
nmetrics, (int) sizeof(CharInfoRec));
@@ -483,7 +484,7 @@ pcfReadFont(FontPtr pFont, FontFilePtr file,
if (nbitmaps != nmetrics || IS_EOF(file))
goto Bail;
/* nmetrics is already ok, so nbitmap also is */
- offsets = malloc(nbitmaps * sizeof(CARD32));
+ offsets = mallocarray(nbitmaps, sizeof(CARD32));
if (!offsets) {
pcfError("pcfReadFont(): Couldn't allocate offsets (%d*%d)\n",
nbitmaps, (int) sizeof(CARD32));
@@ -573,7 +574,7 @@ pcfReadFont(FontPtr pFont, FontFilePtr file,
if (nink_metrics != nmetrics)
goto Bail;
/* nmetrics already checked */
- ink_metrics = malloc(nink_metrics * sizeof(xCharInfo));
+ ink_metrics = mallocarray(nink_metrics, sizeof(xCharInfo));
if (!ink_metrics) {
pcfError("pcfReadFont(): Couldn't allocate ink_metrics (%d*%d)\n",
nink_metrics, (int) sizeof(xCharInfo));
diff --git a/src/bitmap/pcfwrite.c b/src/bitmap/pcfwrite.c
index 61ae83d..de7936c 100644
--- a/src/bitmap/pcfwrite.c
+++ b/src/bitmap/pcfwrite.c
@@ -34,6 +34,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <X11/fonts/fntfilst.h>
#include <X11/fonts/bitmap.h>
@@ -235,7 +236,7 @@ pcfWriteFont(FontPtr pFont, FontFilePtr file)
ink_minbounds = &pFont->info.ink_minbounds;
ink_maxbounds = &pFont->info.ink_maxbounds;
}
- offsetProps = malloc(pFont->info.nprops * sizeof(FontPropRec));
+ offsetProps = mallocarray(pFont->info.nprops, sizeof(FontPropRec));
if (!offsetProps) {
pcfError("pcfWriteFont(): Couldn't allocate offsetProps (%d*%d)",
pFont->info.nprops, (int) sizeof(FontPropRec));
diff --git a/src/bitmap/snfread.c b/src/bitmap/snfread.c
index 452b99d..2e94f9c 100644
--- a/src/bitmap/snfread.c
+++ b/src/bitmap/snfread.c
@@ -53,6 +53,7 @@ from The Open Group.
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <ctype.h>
#include <X11/fonts/fntfilst.h>
@@ -453,13 +454,13 @@ snfReadFontInfo(FontInfoPtr pFontInfo, FontFilePtr file)
return ret;
snfCopyInfo(&fi, pFontInfo);
- pFontInfo->props = malloc(fi.nProps * sizeof(FontPropRec));
+ pFontInfo->props = mallocarray(fi.nProps, sizeof(FontPropRec));
if (!pFontInfo->props) {
snfError("snfReadFontInfo(): Couldn't allocate props (%d*%d)\n",
fi.nProps, (int) sizeof(FontPropRec));
return AllocError;
}
- pFontInfo->isStringProp = malloc(fi.nProps * sizeof(char));
+ pFontInfo->isStringProp = mallocarray(fi.nProps, sizeof(char));
if (!pFontInfo->isStringProp) {
snfError("snfReadFontInfo(): Couldn't allocate isStringProp (%d*%d)\n",
fi.nProps, (int) sizeof(char));
diff --git a/src/fc/fsconvert.c b/src/fc/fsconvert.c
index 312bacb..34230e9 100644
--- a/src/fc/fsconvert.c
+++ b/src/fc/fsconvert.c
@@ -29,6 +29,7 @@
#include <config.h>
#endif
#include "libxfontint.h"
+#include "src/util/replace.h"
#include <X11/X.h>
#include <X11/Xtrans/Xtrans.h>
#include <X11/Xpoll.h>
@@ -107,7 +108,7 @@ _fs_convert_props(fsPropInfo *pi, fsPropOffset *po, pointer pd,
|| nprops > SIZE_MAX/(sizeof(FontPropRec) + sizeof(char)))
return -1;
- dprop = malloc(sizeof(FontPropRec) * nprops + sizeof (char) * nprops);
+ dprop = mallocarray(nprops, sizeof(FontPropRec) + sizeof (char));
if (!dprop)
return -1;
diff --git a/src/fc/fserve.c b/src/fc/fserve.c
index 46f100e..824b355 100644
--- a/src/fc/fserve.c
+++ b/src/fc/fserve.c
@@ -1089,7 +1089,7 @@ fs_read_extent_info(FontPathElementPtr fpe, FSBlockDataPtr blockrec)
pCI = NULL;
}
else
- pCI = malloc(sizeof(CharInfoRec) * numInfos);
+ pCI = mallocarray(numInfos, sizeof(CharInfoRec));
if (!pCI)
{
diff --git a/src/fontfile/fontdir.c b/src/fontfile/fontdir.c
index c07222f..f4edc01 100644
--- a/src/fontfile/fontdir.c
+++ b/src/fontfile/fontdir.c
@@ -49,7 +49,7 @@ FontFileInitTable (FontTablePtr table, int size)
return FALSE;
if (size)
{
- table->entries = malloc(sizeof(FontEntryRec) * size);
+ table->entries = mallocarray(size, sizeof(FontEntryRec));
if (!table->entries)
return FALSE;
}
diff --git a/src/util/fontnames.c b/src/util/fontnames.c
index 4460728..43eafc2 100644
--- a/src/util/fontnames.c
+++ b/src/util/fontnames.c
@@ -65,8 +65,8 @@ xfont2_make_font_names_record(unsigned size)
pFN->size = size;
if (size)
{
- pFN->length = malloc(size * sizeof(int));
- pFN->names = malloc(size * sizeof(char *));
+ pFN->length = mallocarray(size, sizeof(int));
+ pFN->names = mallocarray(size, sizeof(char *));
if (!pFN->length || !pFN->names) {
free(pFN->length);
free(pFN->names);
diff --git a/src/util/fontutil.c b/src/util/fontutil.c
index f7ac507..fd87d77 100644
--- a/src/util/fontutil.c
+++ b/src/util/fontutil.c
@@ -137,7 +137,7 @@ xfont2_query_text_extents(FontPtr pFont,
unsigned char defc[2];
int firstReal;
- charinfo = malloc(count * sizeof(xCharInfo *));
+ charinfo = mallocarray(count, sizeof(xCharInfo *));
if (!charinfo)
return FALSE;
encoding = TwoD16Bit;
@@ -303,7 +303,7 @@ add_range(fsRange *newrange,
/* Grow the list if necessary */
if (*nranges == 0 || *range == (fsRange *)0)
{
- *range = malloc(range_alloc_granularity * SIZEOF(fsRange));
+ *range = mallocarray(range_alloc_granularity, SIZEOF(fsRange));
*nranges = 0;
}
else if (!(*nranges % range_alloc_granularity))
diff --git a/src/util/private.c b/src/util/private.c
index 47c2699..b07a757 100644
--- a/src/util/private.c
+++ b/src/util/private.c
@@ -90,7 +90,7 @@ xfont2_font_set_private(FontPtr pFont, int n, pointer ptr)
return FALSE;
} else {
/* omg realloc */
- new = malloc ((n + 1) * sizeof (pointer));
+ new = reallocarray (NULL, (n + 1), sizeof (pointer));
if (!new)
return FALSE;
if (pFont->devPrivates)
diff --git a/src/util/replace.h b/src/util/replace.h
index 16be544..1a24820 100644
--- a/src/util/replace.h
+++ b/src/util/replace.h
@@ -41,6 +41,10 @@ extern _X_HIDDEN void *
reallocarray(void *optr, size_t nmemb, size_t size);
#endif
+#ifndef mallocarray
+#define mallocarray(n, s) reallocarray(NULL, n, s)
+#endif
+
#include <string.h>
#if defined(HAVE_LIBBSD) && defined(HAVE_STRLCPY)
#include <bsd/string.h> /* for strlcpy, strlcat */