Convert hw/dmx to new *allocarray functions

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
This commit is contained in:
Alan Coopersmith 2015-03-21 17:59:13 -07:00
parent dc5acaa28a
commit 0018784cdd
16 changed files with 51 additions and 59 deletions

View File

@ -204,8 +204,8 @@ dmxConfigAddDisplay(const char *name,
{ {
DMXScreenInfo *dmxScreen; DMXScreenInfo *dmxScreen;
if (!(dmxScreens = realloc(dmxScreens, if (!(dmxScreens = reallocarray(dmxScreens, dmxNumScreens + 1,
(dmxNumScreens + 1) * sizeof(*dmxScreens)))) sizeof(*dmxScreens))))
dmxLog(dmxFatal, dmxLog(dmxFatal,
"dmxConfigAddDisplay: realloc failed for screen %d (%s)\n", "dmxConfigAddDisplay: realloc failed for screen %d (%s)\n",
dmxNumScreens, name); dmxNumScreens, name);
@ -234,8 +234,8 @@ dmxConfigAddInput(const char *name, int core)
{ {
DMXInputInfo *dmxInput; DMXInputInfo *dmxInput;
if (!(dmxInputs = realloc(dmxInputs, if (!(dmxInputs = reallocarray(dmxInputs, dmxNumInputs + 1,
(dmxNumInputs + 1) * sizeof(*dmxInputs)))) sizeof(*dmxInputs))))
dmxLog(dmxFatal, dmxLog(dmxFatal,
"dmxConfigAddInput: realloc failed for input %d (%s)\n", "dmxConfigAddInput: realloc failed for input %d (%s)\n",
dmxNumInputs, name); dmxNumInputs, name);
@ -341,7 +341,7 @@ dmxConfigCopyFromOption(DMXConfigOptionPtr o)
for (pt = o->option; pt; pt = pt->next) { for (pt = o->option; pt; pt = pt->next) {
if (pt->string) { if (pt->string) {
++argc; ++argc;
argv = realloc(argv, (argc + 1) * sizeof(*argv)); argv = reallocarray(argv, argc + 1, sizeof(*argv));
argv[argc] = (char *) pt->string; argv[argc] = (char *) pt->string;
} }
} }

View File

@ -427,7 +427,7 @@ ProcDMXChangeScreensAttributes(ClientPtr client)
if (!_DMXXineramaActive()) if (!_DMXXineramaActive())
goto noxinerama; goto noxinerama;
if (!(attribs = malloc(stuff->screenCount * sizeof(*attribs)))) if (!(attribs = xallocarray(stuff->screenCount, sizeof(*attribs))))
return BadAlloc; return BadAlloc;
for (i = 0; i < stuff->screenCount; i++) { for (i = 0; i < stuff->screenCount; i++) {
@ -624,18 +624,18 @@ ProcDMXGetWindowAttributes(ClientPtr client)
REQUEST_SIZE_MATCH(xDMXGetWindowAttributesReq); REQUEST_SIZE_MATCH(xDMXGetWindowAttributesReq);
if (!(screens = malloc(count * sizeof(*screens)))) if (!(screens = xallocarray(count, sizeof(*screens))))
return BadAlloc; return BadAlloc;
if (!(windows = malloc(count * sizeof(*windows)))) { if (!(windows = xallocarray(count, sizeof(*windows)))) {
free(screens); free(screens);
return BadAlloc; return BadAlloc;
} }
if (!(pos = malloc(count * sizeof(*pos)))) { if (!(pos = xallocarray(count, sizeof(*pos)))) {
free(windows); free(windows);
free(screens); free(screens);
return BadAlloc; return BadAlloc;
} }
if (!(vis = malloc(count * sizeof(*vis)))) { if (!(vis = xallocarray(count, sizeof(*vis)))) {
free(pos); free(pos);
free(windows); free(windows);
free(screens); free(screens);

View File

@ -177,7 +177,7 @@ dmxStoreColors(ColormapPtr pColormap, int ndef, xColorItem * pdef)
dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap); dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
if (dmxScreen->beDisplay && (pColormap->pVisual->class & DynamicClass)) { if (dmxScreen->beDisplay && (pColormap->pVisual->class & DynamicClass)) {
XColor *color = malloc(sizeof(*color) * ndef); XColor *color = xallocarray(ndef, sizeof(*color));
int i; int i;
if (color) { if (color) {

View File

@ -203,7 +203,7 @@ miPointerScreenFuncRec dmxPointerCursorFuncs = {
static int * static int *
dmxSLCreate(void) dmxSLCreate(void)
{ {
int *list = malloc(dmxNumScreens * sizeof(*list)); int *list = xallocarray(dmxNumScreens, sizeof(*list));
int i; int i;
for (i = 0; i < dmxNumScreens; i++) for (i = 0; i < dmxNumScreens; i++)

View File

@ -1188,8 +1188,8 @@ dmxBERestoreRenderGlyph(void *value, XID id, void *n)
/* Now allocate the memory we need */ /* Now allocate the memory we need */
images = calloc(len_images, sizeof(char)); images = calloc(len_images, sizeof(char));
gids = malloc(glyphSet->hash.tableEntries * sizeof(Glyph)); gids = xallocarray(glyphSet->hash.tableEntries, sizeof(Glyph));
glyphs = malloc(glyphSet->hash.tableEntries * sizeof(XGlyphInfo)); glyphs = xallocarray(glyphSet->hash.tableEntries, sizeof(XGlyphInfo));
pos = images; pos = images;
ctr = 0; ctr = 0;

View File

@ -72,7 +72,7 @@ dmxGetFontPath(int *npaths)
newfp = malloc(*npaths + len); newfp = malloc(*npaths + len);
c = (unsigned char *) newfp; c = (unsigned char *) newfp;
fp = malloc(*npaths * sizeof(*fp)); fp = xallocarray(*npaths, sizeof(*fp));
memmove(newfp, paths + 1, *npaths + len - 1); memmove(newfp, paths + 1, *npaths + len - 1);
l = *paths; l = *paths;
@ -306,7 +306,7 @@ dmxBELoadFont(ScreenPtr pScreen, FontPtr pFont)
if (!dmxFontPath) if (!dmxFontPath)
dmxLog(dmxWarning, "No default font path is set.\n"); dmxLog(dmxWarning, "No default font path is set.\n");
goodfps = malloc(npaths * sizeof(*goodfps)); goodfps = xallocarray(npaths, sizeof(*goodfps));
dmxLog(dmxError, dmxLog(dmxError,
"The DMX server failed to set the following font paths on " "The DMX server failed to set the following font paths on "
@ -354,7 +354,7 @@ dmxBELoadFont(ScreenPtr pScreen, FontPtr pFont)
return FALSE; return FALSE;
} }
newfp = malloc(len * sizeof(*newfp)); newfp = xallocarray(len, sizeof(*newfp));
for (i = 0; i < npaths; i++) { for (i = 0; i < npaths; i++) {
if (goodfps[i]) { if (goodfps[i]) {
int n = strlen(fp[i]); int n = strlen(fp[i]);

View File

@ -397,7 +397,7 @@ dmxChangeClip(GCPtr pGC, int type, void *pvalue, int nrects)
} else { } else {
if (dmxScreen->beDisplay) { if (dmxScreen->beDisplay) {
nRects = RegionNumRects((RegionPtr) pGC->clientClip); nRects = RegionNumRects((RegionPtr) pGC->clientClip);
pRects = malloc(nRects * sizeof(*pRects)); pRects = xallocarray(nRects, sizeof(*pRects));
pBox = RegionRects((RegionPtr) pGC->clientClip); pBox = RegionRects((RegionPtr) pGC->clientClip);
for (i = 0; i < nRects; i++) { for (i = 0; i < nRects; i++) {

View File

@ -438,7 +438,7 @@ dmxGetColormaps(DMXScreenInfo * dmxScreen)
int i; int i;
dmxScreen->beNumDefColormaps = dmxScreen->beNumVisuals; dmxScreen->beNumDefColormaps = dmxScreen->beNumVisuals;
dmxScreen->beDefColormaps = malloc(dmxScreen->beNumDefColormaps * dmxScreen->beDefColormaps = xallocarray(dmxScreen->beNumDefColormaps,
sizeof(*dmxScreen->beDefColormaps)); sizeof(*dmxScreen->beDefColormaps));
for (i = 0; i < dmxScreen->beNumDefColormaps; i++) for (i = 0; i < dmxScreen->beNumDefColormaps; i++)
@ -793,7 +793,7 @@ InitOutput(ScreenInfo * pScreenInfo, int argc, char *argv[])
nconfigs = dmxScreen->numGlxVisuals; nconfigs = dmxScreen->numGlxVisuals;
} }
configprivs = malloc(nconfigs * sizeof(dmxGlxVisualPrivate *)); configprivs = xallocarray(nconfigs, sizeof(dmxGlxVisualPrivate *));
if (configs != NULL && configprivs != NULL) { if (configs != NULL && configprivs != NULL) {
int j; int j;

View File

@ -390,7 +390,7 @@ dmxProcRenderAddGlyphs(ClientPtr client)
sizeof(xRenderAddGlyphsReq) - sizeof(xRenderAddGlyphsReq) -
(sizeof(CARD32) + sizeof(xGlyphInfo)) * nglyphs); (sizeof(CARD32) + sizeof(xGlyphInfo)) * nglyphs);
gidsCopy = malloc(sizeof(*gidsCopy) * nglyphs); gidsCopy = xallocarray(nglyphs, sizeof(*gidsCopy));
for (i = 0; i < nglyphs; i++) for (i = 0; i < nglyphs; i++)
gidsCopy[i] = gids[i]; gidsCopy[i] = gids[i];
@ -434,7 +434,7 @@ dmxProcRenderFreeGlyphs(ClientPtr client)
nglyphs = ((client->req_len << 2) - sizeof(xRenderFreeGlyphsReq)) >> 2; nglyphs = ((client->req_len << 2) - sizeof(xRenderFreeGlyphsReq)) >> 2;
if (nglyphs) { if (nglyphs) {
gids = malloc(sizeof(*gids) * nglyphs); gids = xallocarray(nglyphs, sizeof(*gids));
for (i = 0; i < nglyphs; i++) for (i = 0; i < nglyphs; i++)
gids[i] = ((CARD32 *) (stuff + 1))[i]; gids[i] = ((CARD32 *) (stuff + 1))[i];
@ -569,11 +569,11 @@ dmxProcRenderCompositeGlyphs(ClientPtr client)
/* The following only works for Render version > 0.2 */ /* The following only works for Render version > 0.2 */
/* All of the XGlyphElt* structure sizes are identical */ /* All of the XGlyphElt* structure sizes are identical */
elts = malloc(nelt * sizeof(XGlyphElt8)); elts = xallocarray(nelt, sizeof(XGlyphElt8));
if (!elts) if (!elts)
return BadAlloc; return BadAlloc;
glyphs = malloc(nglyph * size); glyphs = xallocarray(nglyph, size);
if (!glyphs) { if (!glyphs) {
free(elts); free(elts);
return BadAlloc; return BadAlloc;
@ -925,7 +925,7 @@ dmxChangePictureClip(PicturePtr pPicture, int clipType, void *value, int n)
int nRects; int nRects;
nRects = nBox; nRects = nBox;
pRects = pRect = malloc(nRects * sizeof(*pRect)); pRects = pRect = xallocarray(nRects, sizeof(*pRect));
while (nBox--) { while (nBox--) {
pRect->x = pBox->x1; pRect->x = pBox->x1;

View File

@ -171,7 +171,7 @@ dmxPropertyCheckOtherServers(DMXScreenInfo * dmxScreen, Atom atom)
dmxLogOutputWarning(dmxScreen, dmxLogOutputWarning(dmxScreen,
"%s also running on %s\n", "%s also running on %s\n",
tp.value, dmxScreen->name); tp.value, dmxScreen->name);
list = realloc(list, ++count * sizeof(*list)); list = reallocarray(list, ++count, sizeof(*list));
list[count - 1] = malloc(tp.nitems + 2); list[count - 1] = malloc(tp.nitems + 2);
strncpy(list[count - 1], (char *) tp.value, tp.nitems + 1); strncpy(list[count - 1], (char *) tp.value, tp.nitems + 1);
} }

View File

@ -969,7 +969,7 @@ dmxDoSetShape(WindowPtr pWindow)
if (wBoundingShape(pWindow)) { if (wBoundingShape(pWindow)) {
pBox = RegionRects(wBoundingShape(pWindow)); pBox = RegionRects(wBoundingShape(pWindow));
nRect = nBox = RegionNumRects(wBoundingShape(pWindow)); nRect = nBox = RegionNumRects(wBoundingShape(pWindow));
pRectFirst = pRect = malloc(nRect * sizeof(*pRect)); pRectFirst = pRect = xallocarray(nRect, sizeof(*pRect));
while (nBox--) { while (nBox--) {
pRect->x = pBox->x1; pRect->x = pBox->x1;
pRect->y = pBox->y1; pRect->y = pBox->y1;
@ -992,7 +992,7 @@ dmxDoSetShape(WindowPtr pWindow)
if (wClipShape(pWindow)) { if (wClipShape(pWindow)) {
pBox = RegionRects(wClipShape(pWindow)); pBox = RegionRects(wClipShape(pWindow));
nRect = nBox = RegionNumRects(wClipShape(pWindow)); nRect = nBox = RegionNumRects(wClipShape(pWindow));
pRectFirst = pRect = malloc(nRect * sizeof(*pRect)); pRectFirst = pRect = xallocarray(nRect, sizeof(*pRect));
while (nBox--) { while (nBox--) {
pRect->x = pBox->x1; pRect->x = pBox->x1;
pRect->y = pBox->y1; pRect->y = pBox->y1;

View File

@ -284,11 +284,11 @@ CreateContext(__GLXclientState * cl,
* allocate memory for back-end servers info * allocate memory for back-end servers info
*/ */
num_be_screens = to_screen - from_screen + 1; num_be_screens = to_screen - from_screen + 1;
glxc->real_ids = (XID *) malloc(sizeof(XID) * num_be_screens); glxc->real_ids = xallocarray(num_be_screens, sizeof(XID));
if (!glxc->real_ids) { if (!glxc->real_ids) {
return BadAlloc; return BadAlloc;
} }
glxc->real_vids = (XID *) malloc(sizeof(XID) * num_be_screens); glxc->real_vids = xallocarray(num_be_screens, sizeof(XID));
if (!glxc->real_vids) { if (!glxc->real_vids) {
return BadAlloc; return BadAlloc;
} }
@ -685,22 +685,16 @@ AddCurrentContext(__GLXclientState * cl, __GLXcontext * glxc, DrawablePtr pDraw)
if (!num) { if (!num) {
table = (__GLXcontext **) malloc(sizeof(__GLXcontext *)); table = (__GLXcontext **) malloc(sizeof(__GLXcontext *));
cl->currentDrawables = (DrawablePtr *) malloc(sizeof(DrawablePtr)); cl->currentDrawables = (DrawablePtr *) malloc(sizeof(DrawablePtr));
cl->be_currentCTag = cl->be_currentCTag = xallocarray(screenInfo.numScreens,
(GLXContextTag *) malloc(screenInfo.numScreens * sizeof(GLXContextTag));
sizeof(GLXContextTag));
} }
else { else {
table = (__GLXcontext **) realloc(table, table = reallocarray(table, num + 1, sizeof(__GLXcontext *));
(num + 1) * sizeof(__GLXcontext *)); cl->currentDrawables = reallocarray(cl->currentDrawables, num + 1,
cl->currentDrawables = (DrawablePtr *) realloc(cl->currentDrawables, sizeof(DrawablePtr));
(num + cl->be_currentCTag = reallocarray(cl->be_currentCTag,
1) * (num + 1) * screenInfo.numScreens,
sizeof(DrawablePtr)); sizeof(GLXContextTag));
cl->be_currentCTag =
(GLXContextTag *) realloc(cl->be_currentCTag,
(num +
1) * screenInfo.numScreens *
sizeof(GLXContextTag));
} }
table[num] = glxc; table[num] = glxc;
cl->currentDrawables[num] = pDraw; cl->currentDrawables[num] = pDraw;
@ -1896,7 +1890,7 @@ CreateGLXPixmap(__GLXclientState * cl,
if (!pGlxPixmap) { if (!pGlxPixmap) {
return BadAlloc; return BadAlloc;
} }
pGlxPixmap->be_xids = (XID *) malloc(sizeof(XID) * screenInfo.numScreens); pGlxPixmap->be_xids = xallocarray(screenInfo.numScreens, sizeof(XID));
if (!pGlxPixmap->be_xids) { if (!pGlxPixmap->be_xids) {
free(pGlxPixmap); free(pGlxPixmap);
return BadAlloc; return BadAlloc;
@ -3356,7 +3350,7 @@ __glXCreatePbuffer(__GLXclientState * cl, GLbyte * pc)
return BadAlloc; return BadAlloc;
} }
pGlxPbuffer->be_xids = (XID *) malloc(sizeof(XID) * screenInfo.numScreens); pGlxPbuffer->be_xids = xallocarray(screenInfo.numScreens, sizeof(XID));
if (!pGlxPbuffer->be_xids) { if (!pGlxPbuffer->be_xids) {
free(pGlxPbuffer); free(pGlxPbuffer);
return BadAlloc; return BadAlloc;
@ -3617,13 +3611,13 @@ __glXGetDrawableAttributes(__GLXclientState * cl, GLbyte * pc)
} }
if (reply.numAttribs) { if (reply.numAttribs) {
attribs_size = 2 * reply.numAttribs * __GLX_SIZE_CARD32; attribs = xallocarray(reply.numAttribs, 2 * __GLX_SIZE_CARD32);
attribs = (CARD32 *) malloc(attribs_size);
if (attribs == NULL) { if (attribs == NULL) {
UnlockDisplay(dpy); UnlockDisplay(dpy);
SyncHandle(); SyncHandle();
return BadAlloc; return BadAlloc;
} }
attribs_size = 2 * reply.numAttribs * __GLX_SIZE_CARD32;
_XRead(dpy, (char *) attribs, attribs_size); _XRead(dpy, (char *) attribs, attribs_size);
} }

View File

@ -129,7 +129,7 @@ CalcServerVersionAndExtensions(void)
/* /*
* read extensions strings of all back-end servers * read extensions strings of all back-end servers
*/ */
be_extensions = (char **) malloc(__glXNumActiveScreens * sizeof(char *)); be_extensions = xallocarray(__glXNumActiveScreens, sizeof(char *));
if (!be_extensions) if (!be_extensions)
return; return;
@ -237,10 +237,9 @@ __glXScreenInit(GLint numscreens)
// find the set of FBConfigs that are present on all back-end // find the set of FBConfigs that are present on all back-end
// servers - only those configs will be supported // servers - only those configs will be supported
*/ */
__glXFBConfigs = (__GLXFBConfig **) malloc(dmxScreen0->numFBConfigs * __glXFBConfigs =
(numscreens + xallocarray(dmxScreen0->numFBConfigs * (numscreens + 1),
1) * sizeof(__GLXFBConfig *));
sizeof(__GLXFBConfig *));
__glXNumFBConfigs = 0; __glXNumFBConfigs = 0;
for (c = 0; c < dmxScreen0->numFBConfigs; c++) { for (c = 0; c < dmxScreen0->numFBConfigs; c++) {

View File

@ -86,7 +86,7 @@ void
dmxArgAdd(dmxArg a, const char *string) dmxArgAdd(dmxArg a, const char *string)
{ {
if (a->argm <= a->argc + 2) if (a->argm <= a->argc + 2)
a->argv = realloc(a->argv, sizeof(*a->argv) * (a->argm *= 2)); a->argv = reallocarray(a->argv, (a->argm *= 2), sizeof(*a->argv));
a->argv[a->argc++] = strdup(string); a->argv[a->argc++] = strdup(string);
a->argv[a->argc] = NULL; a->argv[a->argc] = NULL;
} }

View File

@ -814,8 +814,8 @@ dmxInputCopyLocal(DMXInputInfo * dmxInput, DMXLocalInputInfoPtr s)
dmxLocal->deviceId = -1; dmxLocal->deviceId = -1;
++dmxInput->numDevs; ++dmxInput->numDevs;
dmxInput->devs = realloc(dmxInput->devs, dmxInput->devs = reallocarray(dmxInput->devs,
dmxInput->numDevs * sizeof(*dmxInput->devs)); dmxInput->numDevs, sizeof(*dmxInput->devs));
dmxInput->devs[dmxInput->numDevs - 1] = dmxLocal; dmxInput->devs[dmxInput->numDevs - 1] = dmxLocal;
return dmxLocal; return dmxLocal;

View File

@ -113,9 +113,8 @@ dmxPointerPutMotionEvent(DeviceIntPtr pDevice,
int i; int i;
if (!dmxLocal->history) { if (!dmxLocal->history) {
dmxLocal->history = malloc(sizeof(*dmxLocal->history) dmxLocal->history = xallocarray(numAxes + 1,
* (numAxes + 1) sizeof(*dmxLocal->history) * DMX_MOTION_SIZE);
* DMX_MOTION_SIZE);
dmxLocal->head = 0; dmxLocal->head = 0;
dmxLocal->tail = 0; dmxLocal->tail = 0;
dmxLocal->valuators = calloc(sizeof(*dmxLocal->valuators), numAxes); dmxLocal->valuators = calloc(sizeof(*dmxLocal->valuators), numAxes);