Convert top level extensions to new *allocarray functions

v2: remove now useless parentheses

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 13:42:12 -07:00
parent b9e665c8b2
commit 1c56ac63c0
34 changed files with 111 additions and 122 deletions

View File

@ -54,7 +54,7 @@ ht_create(int keySize,
ht->elements = 0; ht->elements = 0;
ht->bucketBits = INITHASHSIZE; ht->bucketBits = INITHASHSIZE;
numBuckets = 1 << ht->bucketBits; numBuckets = 1 << ht->bucketBits;
ht->buckets = malloc(numBuckets * sizeof(*ht->buckets)); ht->buckets = xallocarray(numBuckets, sizeof(*ht->buckets));
ht->cdata = cdata; ht->cdata = cdata;
if (ht->buckets) { if (ht->buckets) {
@ -92,7 +92,7 @@ double_size(HashTable ht)
int newNumBuckets = 1 << newBucketBits; int newNumBuckets = 1 << newBucketBits;
int c; int c;
newBuckets = malloc(newNumBuckets * sizeof(*ht->buckets)); newBuckets = xallocarray(newNumBuckets, sizeof(*ht->buckets));
if (newBuckets) { if (newBuckets) {
for (c = 0; c < newNumBuckets; ++c) { for (c = 0; c < newNumBuckets; ++c) {
xorg_list_init(&newBuckets[c]); xorg_list_init(&newBuckets[c]);

View File

@ -747,13 +747,13 @@ PanoramiXMaybeAddDepth(DepthPtr pDepth)
j = PanoramiXNumDepths; j = PanoramiXNumDepths;
PanoramiXNumDepths++; PanoramiXNumDepths++;
PanoramiXDepths = realloc(PanoramiXDepths, PanoramiXDepths = reallocarray(PanoramiXDepths,
PanoramiXNumDepths * sizeof(DepthRec)); PanoramiXNumDepths, sizeof(DepthRec));
PanoramiXDepths[j].depth = pDepth->depth; PanoramiXDepths[j].depth = pDepth->depth;
PanoramiXDepths[j].numVids = 0; PanoramiXDepths[j].numVids = 0;
/* XXX suboptimal, should grow these dynamically */ /* XXX suboptimal, should grow these dynamically */
if (pDepth->numVids) if (pDepth->numVids)
PanoramiXDepths[j].vids = malloc(sizeof(VisualID) * pDepth->numVids); PanoramiXDepths[j].vids = xallocarray(pDepth->numVids, sizeof(VisualID));
else else
PanoramiXDepths[j].vids = NULL; PanoramiXDepths[j].vids = NULL;
} }
@ -789,8 +789,8 @@ PanoramiXMaybeAddVisual(VisualPtr pVisual)
/* found a matching visual on all screens, add it to the subset list */ /* found a matching visual on all screens, add it to the subset list */
j = PanoramiXNumVisuals; j = PanoramiXNumVisuals;
PanoramiXNumVisuals++; PanoramiXNumVisuals++;
PanoramiXVisuals = realloc(PanoramiXVisuals, PanoramiXVisuals = reallocarray(PanoramiXVisuals,
PanoramiXNumVisuals * sizeof(VisualRec)); PanoramiXNumVisuals, sizeof(VisualRec));
memcpy(&PanoramiXVisuals[j], pVisual, sizeof(VisualRec)); memcpy(&PanoramiXVisuals[j], pVisual, sizeof(VisualRec));

View File

@ -1341,7 +1341,7 @@ PanoramiXPolyPoint(ClientPtr client)
isRoot = (draw->type == XRT_WINDOW) && draw->u.win.root; isRoot = (draw->type == XRT_WINDOW) && draw->u.win.root;
npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyPointReq)); npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyPointReq));
if (npoint > 0) { if (npoint > 0) {
origPts = malloc(npoint * sizeof(xPoint)); origPts = xallocarray(npoint, sizeof(xPoint));
memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint)); memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1406,7 +1406,7 @@ PanoramiXPolyLine(ClientPtr client)
isRoot = IS_ROOT_DRAWABLE(draw); isRoot = IS_ROOT_DRAWABLE(draw);
npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyLineReq)); npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyLineReq));
if (npoint > 0) { if (npoint > 0) {
origPts = malloc(npoint * sizeof(xPoint)); origPts = xallocarray(npoint, sizeof(xPoint));
memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint)); memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1475,7 +1475,7 @@ PanoramiXPolySegment(ClientPtr client)
return BadLength; return BadLength;
nsegs >>= 3; nsegs >>= 3;
if (nsegs > 0) { if (nsegs > 0) {
origSegs = malloc(nsegs * sizeof(xSegment)); origSegs = xallocarray(nsegs, sizeof(xSegment));
memcpy((char *) origSegs, (char *) &stuff[1], nsegs * sizeof(xSegment)); memcpy((char *) origSegs, (char *) &stuff[1], nsegs * sizeof(xSegment));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1543,7 +1543,7 @@ PanoramiXPolyRectangle(ClientPtr client)
return BadLength; return BadLength;
nrects >>= 3; nrects >>= 3;
if (nrects > 0) { if (nrects > 0) {
origRecs = malloc(nrects * sizeof(xRectangle)); origRecs = xallocarray(nrects, sizeof(xRectangle));
memcpy((char *) origRecs, (char *) &stuff[1], memcpy((char *) origRecs, (char *) &stuff[1],
nrects * sizeof(xRectangle)); nrects * sizeof(xRectangle));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1610,7 +1610,7 @@ PanoramiXPolyArc(ClientPtr client)
return BadLength; return BadLength;
narcs /= sizeof(xArc); narcs /= sizeof(xArc);
if (narcs > 0) { if (narcs > 0) {
origArcs = malloc(narcs * sizeof(xArc)); origArcs = xallocarray(narcs, sizeof(xArc));
memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc)); memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1672,7 +1672,7 @@ PanoramiXFillPoly(ClientPtr client)
count = bytes_to_int32((client->req_len << 2) - sizeof(xFillPolyReq)); count = bytes_to_int32((client->req_len << 2) - sizeof(xFillPolyReq));
if (count > 0) { if (count > 0) {
locPts = malloc(count * sizeof(DDXPointRec)); locPts = xallocarray(count, sizeof(DDXPointRec));
memcpy((char *) locPts, (char *) &stuff[1], memcpy((char *) locPts, (char *) &stuff[1],
count * sizeof(DDXPointRec)); count * sizeof(DDXPointRec));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1741,7 +1741,7 @@ PanoramiXPolyFillRectangle(ClientPtr client)
return BadLength; return BadLength;
things >>= 3; things >>= 3;
if (things > 0) { if (things > 0) {
origRects = malloc(things * sizeof(xRectangle)); origRects = xallocarray(things, sizeof(xRectangle));
memcpy((char *) origRects, (char *) &stuff[1], memcpy((char *) origRects, (char *) &stuff[1],
things * sizeof(xRectangle)); things * sizeof(xRectangle));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1808,7 +1808,7 @@ PanoramiXPolyFillArc(ClientPtr client)
return BadLength; return BadLength;
narcs /= sizeof(xArc); narcs /= sizeof(xArc);
if (narcs > 0) { if (narcs > 0) {
origArcs = malloc(narcs * sizeof(xArc)); origArcs = xallocarray(narcs, sizeof(xArc));
memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc)); memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1988,8 +1988,7 @@ PanoramiXGetImage(ClientPtr client)
if (linesPerBuf > h) if (linesPerBuf > h)
linesPerBuf = h; linesPerBuf = h;
} }
length = linesPerBuf * widthBytesLine; if (!(pBuf = xallocarray(linesPerBuf, widthBytesLine)))
if (!(pBuf = malloc(length)))
return BadAlloc; return BadAlloc;
WriteReplyToClient(client, sizeof(xGetImageReply), &xgi); WriteReplyToClient(client, sizeof(xGetImageReply), &xgi);

View File

@ -853,7 +853,7 @@ ScreenSaverSetAttributes(ClientPtr client)
goto bail; goto bail;
} }
/* over allocate for override redirect */ /* over allocate for override redirect */
pAttr->values = values = malloc((len + 1) * sizeof(unsigned long)); pAttr->values = values = xallocarray(len + 1, sizeof(unsigned long));
if (!values) { if (!values) {
ret = BadAlloc; ret = BadAlloc;
goto bail; goto bail;

View File

@ -996,7 +996,7 @@ ProcShapeGetRectangles(ClientPtr client)
nrects = RegionNumRects(region); nrects = RegionNumRects(region);
box = RegionRects(region); box = RegionRects(region);
rects = malloc(nrects * sizeof(xRectangle)); rects = xallocarray(nrects, sizeof(xRectangle));
if (!rects && nrects) if (!rects && nrects)
return BadAlloc; return BadAlloc;
for (i = 0; i < nrects; i++, box++) { for (i = 0; i < nrects; i++, box++) {

View File

@ -620,7 +620,7 @@ SyncAwaitTriggerFired(SyncTrigger * pTrigger)
pAwaitUnion = (SyncAwaitUnion *) pAwait->pHeader; pAwaitUnion = (SyncAwaitUnion *) pAwait->pHeader;
numwaits = pAwaitUnion->header.num_waitconditions; numwaits = pAwaitUnion->header.num_waitconditions;
ppAwait = malloc(numwaits * sizeof(SyncAwait *)); ppAwait = xallocarray(numwaits, sizeof(SyncAwait *));
if (!ppAwait) if (!ppAwait)
goto bail; goto bail;
@ -1514,7 +1514,7 @@ SyncAwaitPrologue(ClientPtr client, int items)
/* all the memory for the entire await list is allocated /* all the memory for the entire await list is allocated
* here in one chunk * here in one chunk
*/ */
pAwaitUnion = malloc((items + 1) * sizeof(SyncAwaitUnion)); pAwaitUnion = xallocarray(items + 1, sizeof(SyncAwaitUnion));
if (!pAwaitUnion) if (!pAwaitUnion)
return NULL; return NULL;

View File

@ -101,7 +101,7 @@ ProcXCMiscGetXIDList(ClientPtr client)
if (stuff->count > UINT32_MAX / sizeof(XID)) if (stuff->count > UINT32_MAX / sizeof(XID))
return BadAlloc; return BadAlloc;
pids = (XID *) malloc(stuff->count * sizeof(XID)); pids = xallocarray(stuff->count, sizeof(XID));
if (!pids) { if (!pids) {
return BadAlloc; return BadAlloc;
} }

View File

@ -401,7 +401,7 @@ ProcXF86BigfontQueryFont(ClientPtr client)
} }
else { else {
#endif #endif
pCI = malloc(nCharInfos * sizeof(xCharInfo)); pCI = xallocarray(nCharInfos, sizeof(xCharInfo));
if (!pCI) if (!pCI)
return BadAlloc; return BadAlloc;
#ifdef HAS_SHM #ifdef HAS_SHM
@ -463,7 +463,7 @@ ProcXF86BigfontQueryFont(ClientPtr client)
if (hashModulus > nCharInfos + 1) if (hashModulus > nCharInfos + 1)
hashModulus = nCharInfos + 1; hashModulus = nCharInfos + 1;
tmp = malloc((4 * nCharInfos + 1) * sizeof(CARD16)); tmp = xallocarray(4 * nCharInfos + 1, sizeof(CARD16));
if (!tmp) { if (!tmp) {
if (!pDesc) if (!pDesc)
free(pCI); free(pCI);

View File

@ -223,7 +223,7 @@ ProcXResQueryClients(ClientPtr client)
REQUEST_SIZE_MATCH(xXResQueryClientsReq); REQUEST_SIZE_MATCH(xXResQueryClientsReq);
current_clients = malloc(currentMaxClients * sizeof(int)); current_clients = xallocarray(currentMaxClients, sizeof(int));
num_clients = 0; num_clients = 0;
for (i = 0; i < currentMaxClients; i++) { for (i = 0; i < currentMaxClients; i++) {

View File

@ -64,7 +64,7 @@ SELinuxArraySet(SELinuxArrayRec * rec, unsigned key, void *val)
{ {
if (key >= rec->size) { if (key >= rec->size) {
/* Need to increase size of array */ /* Need to increase size of array */
rec->array = realloc(rec->array, (key + 1) * sizeof(val)); rec->array = reallocarray(rec->array, key + 1, sizeof(val));
if (!rec->array) if (!rec->array)
return FALSE; return FALSE;
memset(rec->array + rec->size, 0, (key - rec->size + 1) * sizeof(val)); memset(rec->array + rec->size, 0, (key - rec->size + 1) * sizeof(val));

View File

@ -1102,7 +1102,7 @@ XvFillColorKey(DrawablePtr pDraw, CARD32 key, RegionPtr region)
(void) ChangeGC(NullClient, gc, GCForeground | GCSubwindowMode, pval); (void) ChangeGC(NullClient, gc, GCForeground | GCSubwindowMode, pval);
ValidateGC(pDraw, gc); ValidateGC(pDraw, gc);
rects = malloc(nbox * sizeof(xRectangle)); rects = xallocarray(nbox, sizeof(xRectangle));
if (rects) { if (rects) {
for (i = 0; i < nbox; i++, pbox++) { for (i = 0; i < nbox; i++, pbox++) {
rects[i].x = pbox->x1 - pDraw->x; rects[i].x = pbox->x1 - pDraw->x;

View File

@ -471,9 +471,9 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
oldTrace = to->focus->trace; oldTrace = to->focus->trace;
memcpy(to->focus, from->focus, sizeof(FocusClassRec)); memcpy(to->focus, from->focus, sizeof(FocusClassRec));
to->focus->trace = realloc(oldTrace, to->focus->trace = reallocarray(oldTrace,
to->focus->traceSize * to->focus->traceSize,
sizeof(WindowPtr)); sizeof(WindowPtr));
if (!to->focus->trace && to->focus->traceSize) if (!to->focus->trace && to->focus->traceSize)
FatalError("[Xi] no memory for trace.\n"); FatalError("[Xi] no memory for trace.\n");
memcpy(to->focus->trace, from->focus->trace, memcpy(to->focus->trace, from->focus->trace,

View File

@ -118,7 +118,7 @@ ProcXGetDeviceDontPropagateList(ClientPtr client)
ClassFromMask(NULL, others->dontPropagateMask[i], i, &count, COUNT); ClassFromMask(NULL, others->dontPropagateMask[i], i, &count, COUNT);
if (count) { if (count) {
rep.count = count; rep.count = count;
buf = (XEventClass *) malloc(rep.count * sizeof(XEventClass)); buf = xallocarray(rep.count, sizeof(XEventClass));
rep.length = bytes_to_int32(rep.count * sizeof(XEventClass)); rep.length = bytes_to_int32(rep.count * sizeof(XEventClass));
tbuf = buf; tbuf = buf;

View File

@ -221,7 +221,7 @@ list_atoms(DeviceIntPtr dev, int *natoms, Atom **atoms_return)
if (nprops) { if (nprops) {
Atom *a; Atom *a;
atoms = malloc(nprops * sizeof(Atom)); atoms = xallocarray(nprops, sizeof(Atom));
if (!atoms) if (!atoms)
return BadAlloc; return BadAlloc;
a = atoms; a = atoms;
@ -687,7 +687,6 @@ XIChangeDeviceProperty(DeviceIntPtr dev, Atom property, Atom type,
{ {
XIPropertyPtr prop; XIPropertyPtr prop;
int size_in_bytes; int size_in_bytes;
int total_size;
unsigned long total_len; unsigned long total_len;
XIPropertyValuePtr prop_value; XIPropertyValuePtr prop_value;
XIPropertyValueRec new_value; XIPropertyValueRec new_value;
@ -725,9 +724,8 @@ XIChangeDeviceProperty(DeviceIntPtr dev, Atom property, Atom type,
if (mode == PropModeReplace || len > 0) { if (mode == PropModeReplace || len > 0) {
void *new_data = NULL, *old_data = NULL; void *new_data = NULL, *old_data = NULL;
total_size = total_len * size_in_bytes; new_value.data = xallocarray(total_len, size_in_bytes);
new_value.data = (void *) malloc(total_size); if (!new_value.data && total_len && size_in_bytes) {
if (!new_value.data && total_size) {
if (add) if (add)
XIDestroyDeviceProperty(prop); XIDestroyDeviceProperty(prop);
return BadAlloc; return BadAlloc;

View File

@ -223,8 +223,8 @@ compRegisterAlternateVisuals(CompScreenPtr cs, VisualID * vids, int nVisuals)
{ {
VisualID *p; VisualID *p;
p = realloc(cs->alternateVisuals, p = reallocarray(cs->alternateVisuals,
sizeof(VisualID) * (cs->numAlternateVisuals + nVisuals)); cs->numAlternateVisuals + nVisuals, sizeof(VisualID));
if (p == NULL) if (p == NULL)
return FALSE; return FALSE;
@ -253,8 +253,8 @@ CompositeRegisterImplicitRedirectionException(ScreenPtr pScreen,
CompScreenPtr cs = GetCompScreen(pScreen); CompScreenPtr cs = GetCompScreen(pScreen);
CompImplicitRedirectException *p; CompImplicitRedirectException *p;
p = realloc(cs->implicitRedirectExceptions, p = reallocarray(cs->implicitRedirectExceptions,
sizeof(p[0]) * (cs->numImplicitRedirectExceptions + 1)); cs->numImplicitRedirectExceptions + 1, sizeof(p[0]));
if (p == NULL) if (p == NULL)
return FALSE; return FALSE;

View File

@ -287,11 +287,10 @@ ProcDbeAllocateBackBufferName(ClientPtr client)
} }
/* malloc/realloc a new array and initialize all elements to 0. */ /* malloc/realloc a new array and initialize all elements to 0. */
pDbeWindowPriv->IDs = (XID *) realloc(pIDs, pDbeWindowPriv->IDs =
(pDbeWindowPriv-> reallocarray(pIDs,
maxAvailableIDs + pDbeWindowPriv->maxAvailableIDs + DBE_INCR_MAX_IDS,
DBE_INCR_MAX_IDS) * sizeof(XID));
sizeof(XID));
if (!pDbeWindowPriv->IDs) { if (!pDbeWindowPriv->IDs) {
return BadAlloc; return BadAlloc;
} }
@ -470,7 +469,7 @@ ProcDbeSwapBuffers(ClientPtr client)
dbeSwapInfo = (xDbeSwapInfo *) &stuff[1]; dbeSwapInfo = (xDbeSwapInfo *) &stuff[1];
/* Allocate array to record swap information. */ /* Allocate array to record swap information. */
swapInfo = (DbeSwapInfoPtr) malloc(nStuff * sizeof(DbeSwapInfoRec)); swapInfo = xallocarray(nStuff, sizeof(DbeSwapInfoRec));
if (swapInfo == NULL) { if (swapInfo == NULL) {
return BadAlloc; return BadAlloc;
} }
@ -580,8 +579,7 @@ ProcDbeGetVisualInfo(ClientPtr client)
return BadAlloc; return BadAlloc;
/* Make sure any specified drawables are valid. */ /* Make sure any specified drawables are valid. */
if (stuff->n != 0) { if (stuff->n != 0) {
if (!(pDrawables = (DrawablePtr *) malloc(stuff->n * if (!(pDrawables = xallocarray(stuff->n, sizeof(DrawablePtr)))) {
sizeof(DrawablePtr)))) {
return BadAlloc; return BadAlloc;
} }

View File

@ -87,7 +87,7 @@ miDbeGetVisualInfo(ScreenPtr pScreen, XdbeScreenVisualInfo * pScrVisInfo)
} }
/* Allocate an array of XdbeVisualInfo items. */ /* Allocate an array of XdbeVisualInfo items. */
if (!(visInfo = (XdbeVisualInfo *) malloc(count * sizeof(XdbeVisualInfo)))) { if (!(visInfo = xallocarray(count, sizeof(XdbeVisualInfo)))) {
return FALSE; /* memory alloc failure */ return FALSE; /* memory alloc failure */
} }

View File

@ -140,9 +140,9 @@ PseudoramiXAddScreen(int x, int y, int w, int h)
if (pseudoramiXNumScreens == pseudoramiXScreensAllocated) { if (pseudoramiXNumScreens == pseudoramiXScreensAllocated) {
pseudoramiXScreensAllocated += pseudoramiXScreensAllocated + 1; pseudoramiXScreensAllocated += pseudoramiXScreensAllocated + 1;
pseudoramiXScreens = realloc(pseudoramiXScreens, pseudoramiXScreens = reallocarray(pseudoramiXScreens,
pseudoramiXScreensAllocated * pseudoramiXScreensAllocated,
sizeof(PseudoramiXScreenRec)); sizeof(PseudoramiXScreenRec));
} }
DEBUG_LOG("x: %d, y: %d, w: %d, h: %d\n", x, y, w, h); DEBUG_LOG("x: %d, y: %d, w: %d, h: %d\n", x, y, w, h);

View File

@ -65,8 +65,8 @@ RRCrtcCreate(ScreenPtr pScreen, void *devPrivate)
/* make space for the crtc pointer */ /* make space for the crtc pointer */
if (pScrPriv->numCrtcs) if (pScrPriv->numCrtcs)
crtcs = realloc(pScrPriv->crtcs, crtcs = reallocarray(pScrPriv->crtcs,
(pScrPriv->numCrtcs + 1) * sizeof(RRCrtcPtr)); pScrPriv->numCrtcs + 1, sizeof(RRCrtcPtr));
else else
crtcs = malloc(sizeof(RRCrtcPtr)); crtcs = malloc(sizeof(RRCrtcPtr));
if (!crtcs) if (!crtcs)
@ -176,10 +176,10 @@ RRCrtcNotify(RRCrtcPtr crtc,
if (numOutputs) { if (numOutputs) {
if (crtc->numOutputs) if (crtc->numOutputs)
newoutputs = realloc(crtc->outputs, newoutputs = reallocarray(crtc->outputs,
numOutputs * sizeof(RROutputPtr)); numOutputs, sizeof(RROutputPtr));
else else
newoutputs = malloc(numOutputs * sizeof(RROutputPtr)); newoutputs = xallocarray(numOutputs, sizeof(RROutputPtr));
if (!newoutputs) if (!newoutputs)
return FALSE; return FALSE;
} }
@ -798,7 +798,7 @@ RRCrtcGammaSetSize(RRCrtcPtr crtc, int size)
if (size == crtc->gammaSize) if (size == crtc->gammaSize)
return TRUE; return TRUE;
if (size) { if (size) {
gamma = malloc(size * 3 * sizeof(CARD16)); gamma = xallocarray(size, 3 * sizeof(CARD16));
if (!gamma) if (!gamma)
return FALSE; return FALSE;
} }
@ -1027,7 +1027,7 @@ ProcRRSetCrtcConfig(ClientPtr client)
return BadMatch; return BadMatch;
} }
if (numOutputs) { if (numOutputs) {
outputs = malloc(numOutputs * sizeof(RROutputPtr)); outputs = xallocarray(numOutputs, sizeof(RROutputPtr));
if (!outputs) if (!outputs)
return BadAlloc; return BadAlloc;
} }

View File

@ -55,8 +55,8 @@ RROldModeAdd(RROutputPtr output, RRScreenSizePtr size, int refresh)
} }
if (output->numModes) if (output->numModes)
modes = realloc(output->modes, modes = reallocarray(output->modes,
(output->numModes + 1) * sizeof(RRModePtr)); output->numModes + 1, sizeof(RRModePtr));
else else
modes = malloc(sizeof(RRModePtr)); modes = malloc(sizeof(RRModePtr));
if (!modes) { if (!modes) {
@ -266,8 +266,8 @@ RRRegisterSize(ScreenPtr pScreen,
for (i = 0; i < pScrPriv->nSizes; i++) for (i = 0; i < pScrPriv->nSizes; i++)
if (RRScreenSizeMatches(&tmp, &pScrPriv->pSizes[i])) if (RRScreenSizeMatches(&tmp, &pScrPriv->pSizes[i]))
return &pScrPriv->pSizes[i]; return &pScrPriv->pSizes[i];
pNew = realloc(pScrPriv->pSizes, pNew = reallocarray(pScrPriv->pSizes,
(pScrPriv->nSizes + 1) * sizeof(RRScreenSize)); pScrPriv->nSizes + 1, sizeof(RRScreenSize));
if (!pNew) if (!pNew)
return 0; return 0;
pNew[pScrPriv->nSizes++] = tmp; pNew[pScrPriv->nSizes++] = tmp;
@ -289,7 +289,7 @@ RRRegisterRate(ScreenPtr pScreen, RRScreenSizePtr pSize, int rate)
if (pSize->pRates[i].rate == rate) if (pSize->pRates[i].rate == rate)
return TRUE; return TRUE;
pNew = realloc(pSize->pRates, (pSize->nRates + 1) * sizeof(RRScreenRate)); pNew = reallocarray(pSize->pRates, pSize->nRates + 1, sizeof(RRScreenRate));
if (!pNew) if (!pNew)
return FALSE; return FALSE;
pRate = &pNew[pSize->nRates++]; pRate = &pNew[pSize->nRates++];

View File

@ -79,7 +79,7 @@ RRModeCreate(xRRModeInfo * modeInfo, const char *name, ScreenPtr userScreen)
mode->userScreen = userScreen; mode->userScreen = userScreen;
if (num_modes) if (num_modes)
newModes = realloc(modes, (num_modes + 1) * sizeof(RRModePtr)); newModes = reallocarray(modes, num_modes + 1, sizeof(RRModePtr));
else else
newModes = malloc(sizeof(RRModePtr)); newModes = malloc(sizeof(RRModePtr));
@ -166,7 +166,7 @@ RRModesForScreen(ScreenPtr pScreen, int *num_ret)
RRModePtr *screen_modes; RRModePtr *screen_modes;
int num_screen_modes = 0; int num_screen_modes = 0;
screen_modes = malloc((num_modes ? num_modes : 1) * sizeof(RRModePtr)); screen_modes = xallocarray((num_modes ? num_modes : 1), sizeof(RRModePtr));
if (!screen_modes) if (!screen_modes)
return NULL; return NULL;

View File

@ -494,8 +494,9 @@ RRMonitorAdd(ClientPtr client, ScreenPtr screen, RRMonitorPtr monitor)
* needs to not have any side-effects on failure * needs to not have any side-effects on failure
*/ */
if (pScrPriv->numMonitors) if (pScrPriv->numMonitors)
monitors = realloc(pScrPriv->monitors, monitors = reallocarray(pScrPriv->monitors,
(pScrPriv->numMonitors + 1) * sizeof (RRMonitorPtr)); pScrPriv->numMonitors + 1,
sizeof (RRMonitorPtr));
else else
monitors = malloc(sizeof (RRMonitorPtr)); monitors = malloc(sizeof (RRMonitorPtr));

View File

@ -60,8 +60,8 @@ RROutputCreate(ScreenPtr pScreen,
pScrPriv = rrGetScrPriv(pScreen); pScrPriv = rrGetScrPriv(pScreen);
if (pScrPriv->numOutputs) if (pScrPriv->numOutputs)
outputs = realloc(pScrPriv->outputs, outputs = reallocarray(pScrPriv->outputs,
(pScrPriv->numOutputs + 1) * sizeof(RROutputPtr)); pScrPriv->numOutputs + 1, sizeof(RROutputPtr));
else else
outputs = malloc(sizeof(RROutputPtr)); outputs = malloc(sizeof(RROutputPtr));
if (!outputs) if (!outputs)
@ -124,7 +124,7 @@ RROutputSetClones(RROutputPtr output, RROutputPtr * clones, int numClones)
return TRUE; return TRUE;
} }
if (numClones) { if (numClones) {
newClones = malloc(numClones * sizeof(RROutputPtr)); newClones = xallocarray(numClones, sizeof(RROutputPtr));
if (!newClones) if (!newClones)
return FALSE; return FALSE;
} }
@ -157,7 +157,7 @@ RROutputSetModes(RROutputPtr output,
} }
if (numModes) { if (numModes) {
newModes = malloc(numModes * sizeof(RRModePtr)); newModes = xallocarray(numModes, sizeof(RRModePtr));
if (!newModes) if (!newModes)
return FALSE; return FALSE;
} }
@ -200,8 +200,8 @@ RROutputAddUserMode(RROutputPtr output, RRModePtr mode)
return BadMatch; return BadMatch;
if (output->userModes) if (output->userModes)
newModes = realloc(output->userModes, newModes = reallocarray(output->userModes,
(output->numUserModes + 1) * sizeof(RRModePtr)); output->numUserModes + 1, sizeof(RRModePtr));
else else
newModes = malloc(sizeof(RRModePtr)); newModes = malloc(sizeof(RRModePtr));
if (!newModes) if (!newModes)
@ -256,7 +256,7 @@ RROutputSetCrtcs(RROutputPtr output, RRCrtcPtr * crtcs, int numCrtcs)
return TRUE; return TRUE;
} }
if (numCrtcs) { if (numCrtcs) {
newCrtcs = malloc(numCrtcs * sizeof(RRCrtcPtr)); newCrtcs = xallocarray(numCrtcs, sizeof(RRCrtcPtr));
if (!newCrtcs) if (!newCrtcs)
return FALSE; return FALSE;
} }

View File

@ -140,7 +140,6 @@ RRChangeOutputProperty(RROutputPtr output, Atom property, Atom type,
RRPropertyPtr prop; RRPropertyPtr prop;
rrScrPrivPtr pScrPriv = rrGetScrPriv(output->pScreen); rrScrPrivPtr pScrPriv = rrGetScrPriv(output->pScreen);
int size_in_bytes; int size_in_bytes;
int total_size;
unsigned long total_len; unsigned long total_len;
RRPropertyValuePtr prop_value; RRPropertyValuePtr prop_value;
RRPropertyValueRec new_value; RRPropertyValueRec new_value;
@ -180,9 +179,8 @@ RRChangeOutputProperty(RROutputPtr output, Atom property, Atom type,
if (mode == PropModeReplace || len > 0) { if (mode == PropModeReplace || len > 0) {
void *new_data = NULL, *old_data = NULL; void *new_data = NULL, *old_data = NULL;
total_size = total_len * size_in_bytes; new_value.data = xallocarray(total_len, size_in_bytes);
new_value.data = (void *) malloc(total_size); if (!new_value.data && total_len && size_in_bytes) {
if (!new_value.data && total_size) {
if (add) if (add)
RRDestroyOutputProperty(prop); RRDestroyOutputProperty(prop);
return BadAlloc; return BadAlloc;
@ -350,7 +348,7 @@ RRConfigureOutputProperty(RROutputPtr output, Atom property,
return BadMatch; return BadMatch;
} }
new_values = malloc(num_values * sizeof(INT32)); new_values = xallocarray(num_values, sizeof(INT32));
if (!new_values && num_values) { if (!new_values && num_values) {
if (add) if (add)
RRDestroyOutputProperty(prop); RRDestroyOutputProperty(prop);
@ -400,7 +398,7 @@ ProcRRListOutputProperties(ClientPtr client)
for (prop = output->properties; prop; prop = prop->next) for (prop = output->properties; prop; prop = prop->next)
numProps++; numProps++;
if (numProps) if (numProps)
if (!(pAtoms = (Atom *) malloc(numProps * sizeof(Atom)))) if (!(pAtoms = xallocarray(numProps, sizeof(Atom))))
return BadAlloc; return BadAlloc;
rep = (xRRListOutputPropertiesReply) { rep = (xRRListOutputPropertiesReply) {
@ -447,7 +445,7 @@ ProcRRQueryOutputProperty(ClientPtr client)
return BadName; return BadName;
if (prop->num_valid) { if (prop->num_valid) {
extra = malloc(prop->num_valid * sizeof(INT32)); extra = xallocarray(prop->num_valid, sizeof(INT32));
if (!extra) if (!extra)
return BadAlloc; return BadAlloc;
} }

View File

@ -350,7 +350,7 @@ RRConfigureProviderProperty(RRProviderPtr provider, Atom property,
return BadMatch; return BadMatch;
} }
new_values = malloc(num_values * sizeof(INT32)); new_values = xallocarray(num_values, sizeof(INT32));
if (!new_values && num_values) { if (!new_values && num_values) {
if (add) if (add)
RRDestroyProviderProperty(prop); RRDestroyProviderProperty(prop);
@ -400,7 +400,7 @@ ProcRRListProviderProperties(ClientPtr client)
for (prop = provider->properties; prop; prop = prop->next) for (prop = provider->properties; prop; prop = prop->next)
numProps++; numProps++;
if (numProps) if (numProps)
if (!(pAtoms = (Atom *) malloc(numProps * sizeof(Atom)))) if (!(pAtoms = xallocarray(numProps, sizeof(Atom))))
return BadAlloc; return BadAlloc;
rep = (xRRListProviderPropertiesReply) { rep = (xRRListProviderPropertiesReply) {
@ -445,7 +445,7 @@ ProcRRQueryProviderProperty(ClientPtr client)
return BadName; return BadName;
if (prop->num_valid) { if (prop->num_valid) {
extra = malloc(prop->num_valid * sizeof(INT32)); extra = xallocarray(prop->num_valid, sizeof(INT32));
if (!extra) if (!extra)
return BadAlloc; return BadAlloc;
} }

View File

@ -70,7 +70,7 @@ RRTransformSetFilter(RRTransformPtr dst,
xFixed *new_params; xFixed *new_params;
if (nparams) { if (nparams) {
new_params = malloc(nparams * sizeof(xFixed)); new_params = xallocarray(nparams, sizeof(xFixed));
if (!new_params) if (!new_params)
return FALSE; return FALSE;
memcpy(new_params, params, nparams * sizeof(xFixed)); memcpy(new_params, params, nparams * sizeof(xFixed));

View File

@ -1074,19 +1074,19 @@ RecordAddClientToRCAP(RecordClientsAndProtocolPtr pRCAP, XID clientspec)
{ {
if (pRCAP->numClients == pRCAP->sizeClients) { if (pRCAP->numClients == pRCAP->sizeClients) {
if (pRCAP->clientIDsSeparatelyAllocated) { if (pRCAP->clientIDsSeparatelyAllocated) {
XID *pNewIDs = (XID *) realloc(pRCAP->pClientIDs, XID *pNewIDs =
(pRCAP->sizeClients + reallocarray(pRCAP->pClientIDs,
CLIENT_ARRAY_GROWTH_INCREMENT) * pRCAP->sizeClients + CLIENT_ARRAY_GROWTH_INCREMENT,
sizeof(XID)); sizeof(XID));
if (!pNewIDs) if (!pNewIDs)
return; return;
pRCAP->pClientIDs = pNewIDs; pRCAP->pClientIDs = pNewIDs;
pRCAP->sizeClients += CLIENT_ARRAY_GROWTH_INCREMENT; pRCAP->sizeClients += CLIENT_ARRAY_GROWTH_INCREMENT;
} }
else { else {
XID *pNewIDs = (XID *) malloc((pRCAP->sizeClients + XID *pNewIDs =
CLIENT_ARRAY_GROWTH_INCREMENT) * xallocarray(pRCAP->sizeClients + CLIENT_ARRAY_GROWTH_INCREMENT,
sizeof(XID)); sizeof(XID));
if (!pNewIDs) if (!pNewIDs)
return; return;
memcpy(pNewIDs, pRCAP->pClientIDs, pRCAP->numClients * sizeof(XID)); memcpy(pNewIDs, pRCAP->pClientIDs, pRCAP->numClients * sizeof(XID));
@ -1217,7 +1217,7 @@ RecordCanonicalizeClientSpecifiers(XID *pClientspecs, int *pNumClientspecs,
for (i = 0; i < numClients; i++) { for (i = 0; i < numClients; i++) {
if (pClientspecs[i] == XRecordAllClients || pClientspecs[i] == XRecordCurrentClients) { /* expand All/Current */ if (pClientspecs[i] == XRecordAllClients || pClientspecs[i] == XRecordCurrentClients) { /* expand All/Current */
int j, nc; int j, nc;
XID *pCanon = (XID *) malloc(sizeof(XID) * (currentMaxClients + 1)); XID *pCanon = xallocarray(currentMaxClients + 1, sizeof(XID));
if (!pCanon) if (!pCanon)
return NULL; return NULL;
@ -1421,8 +1421,7 @@ static int
RecordAllocIntervals(SetInfoPtr psi, int nIntervals) RecordAllocIntervals(SetInfoPtr psi, int nIntervals)
{ {
assert(!psi->intervals); assert(!psi->intervals);
psi->intervals = (RecordSetInterval *) psi->intervals = xallocarray(nIntervals, sizeof(RecordSetInterval));
malloc(nIntervals * sizeof(RecordSetInterval));
if (!psi->intervals) if (!psi->intervals)
return BadAlloc; return BadAlloc;
memset(psi->intervals, 0, nIntervals * sizeof(RecordSetInterval)); memset(psi->intervals, 0, nIntervals * sizeof(RecordSetInterval));
@ -1584,7 +1583,7 @@ RecordRegisterClients(RecordContextPtr pContext, ClientPtr client,
* range for extension replies. * range for extension replies.
*/ */
maxSets = PREDEFSETS + 2 * stuff->nRanges; maxSets = PREDEFSETS + 2 * stuff->nRanges;
si = (SetInfoPtr) malloc(sizeof(SetInfoRec) * maxSets); si = xallocarray(maxSets, sizeof(SetInfoRec));
if (!si) { if (!si) {
err = BadAlloc; err = BadAlloc;
goto bailout; goto bailout;
@ -1853,8 +1852,8 @@ ProcRecordCreateContext(ClientPtr client)
/* make sure there is room in ppAllContexts to store the new context */ /* make sure there is room in ppAllContexts to store the new context */
ppNewAllContexts = (RecordContextPtr *) ppNewAllContexts =
realloc(ppAllContexts, sizeof(RecordContextPtr) * (numContexts + 1)); reallocarray(ppAllContexts, numContexts + 1, sizeof(RecordContextPtr));
if (!ppNewAllContexts) if (!ppNewAllContexts)
goto bailout; goto bailout;
ppAllContexts = ppNewAllContexts; ppAllContexts = ppNewAllContexts;
@ -1971,8 +1970,7 @@ RecordAllocRanges(GetContextRangeInfoPtr pri, int nRanges)
#define SZINCR 8 #define SZINCR 8
newsize = max(pri->size + SZINCR, nRanges); newsize = max(pri->size + SZINCR, nRanges);
pNewRange = (xRecordRange *) realloc(pri->pRanges, pNewRange = reallocarray(pri->pRanges, newsize, sizeof(xRecordRange));
newsize * sizeof(xRecordRange));
if (!pNewRange) if (!pNewRange)
return BadAlloc; return BadAlloc;
@ -2150,9 +2148,7 @@ ProcRecordGetContext(ClientPtr client)
/* allocate and initialize space for record range info */ /* allocate and initialize space for record range info */
pRangeInfo = pRangeInfo = xallocarray(nRCAPs, sizeof(GetContextRangeInfoRec));
(GetContextRangeInfoPtr) malloc(nRCAPs *
sizeof(GetContextRangeInfoRec));
if (!pRangeInfo && nRCAPs > 0) if (!pRangeInfo && nRCAPs > 0)
return BadAlloc; return BadAlloc;
for (i = 0; i < nRCAPs; i++) { for (i = 0; i < nRCAPs; i++) {
@ -2733,7 +2729,8 @@ RecordAClientStateChange(CallbackListPtr *pcbl, void *nulldata,
/* RecordDisableContext modifies contents of ppAllContexts. */ /* RecordDisableContext modifies contents of ppAllContexts. */
numContextsCopy = numContexts; numContextsCopy = numContexts;
ppAllContextsCopy = malloc(numContextsCopy * sizeof(RecordContextPtr)); ppAllContextsCopy = xallocarray(numContextsCopy,
sizeof(RecordContextPtr));
assert(ppAllContextsCopy); assert(ppAllContextsCopy);
memcpy(ppAllContextsCopy, ppAllContexts, memcpy(ppAllContextsCopy, ppAllContexts,
numContextsCopy * sizeof(RecordContextPtr)); numContextsCopy * sizeof(RecordContextPtr));

View File

@ -303,9 +303,7 @@ IntervalListCreateSet(RecordSetInterval * pIntervals, int nIntervals,
CARD16 first; CARD16 first;
if (nIntervals > 0) { if (nIntervals > 0) {
stackIntervals = stackIntervals = xallocarray(nIntervals, sizeof(RecordSetInterval));
(RecordSetInterval *) malloc(sizeof(RecordSetInterval) *
nIntervals);
if (!stackIntervals) if (!stackIntervals)
return NULL; return NULL;

View File

@ -67,7 +67,7 @@ PictureGetFilterId(const char *filter, int len, Bool makeit)
memcpy(name, filter, len); memcpy(name, filter, len);
name[len] = '\0'; name[len] = '\0';
if (filterNames) if (filterNames)
names = realloc(filterNames, (nfilterNames + 1) * sizeof(char *)); names = reallocarray(filterNames, nfilterNames + 1, sizeof(char *));
else else
names = malloc(sizeof(char *)); names = malloc(sizeof(char *));
if (!names) { if (!names) {
@ -145,7 +145,7 @@ PictureAddFilter(ScreenPtr pScreen,
return -1; return -1;
if (ps->filters) if (ps->filters)
filters = filters =
realloc(ps->filters, (ps->nfilters + 1) * sizeof(PictFilterRec)); reallocarray(ps->filters, ps->nfilters + 1, sizeof(PictFilterRec));
else else
filters = malloc(sizeof(PictFilterRec)); filters = malloc(sizeof(PictFilterRec));
if (!filters) if (!filters)
@ -177,9 +177,9 @@ PictureSetFilterAlias(ScreenPtr pScreen, const char *filter, const char *alias)
PictFilterAliasPtr aliases; PictFilterAliasPtr aliases;
if (ps->filterAliases) if (ps->filterAliases)
aliases = realloc(ps->filterAliases, aliases = reallocarray(ps->filterAliases,
(ps->nfilterAliases + 1) * ps->nfilterAliases + 1,
sizeof(PictFilterAliasRec)); sizeof(PictFilterAliasRec));
else else
aliases = malloc(sizeof(PictFilterAliasRec)); aliases = malloc(sizeof(PictFilterAliasRec));
if (!aliases) if (!aliases)
@ -336,7 +336,7 @@ SetPicturePictFilter(PicturePtr pPicture, PictFilterPtr pFilter,
return BadMatch; return BadMatch;
if (nparams != pPicture->filter_nparams) { if (nparams != pPicture->filter_nparams) {
xFixed *new_params = malloc(nparams * sizeof(xFixed)); xFixed *new_params = xallocarray(nparams, sizeof(xFixed));
if (!new_params && nparams) if (!new_params && nparams)
return BadAlloc; return BadAlloc;

View File

@ -254,7 +254,7 @@ miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat)
return FALSE; return FALSE;
pFormat->index.nvalues = num; pFormat->index.nvalues = num;
pFormat->index.pValues = malloc(num * sizeof(xIndexValue)); pFormat->index.pValues = xallocarray(num, sizeof(xIndexValue));
if (!pFormat->index.pValues) { if (!pFormat->index.pValues) {
free(pIndexed); free(pIndexed);
return FALSE; return FALSE;

View File

@ -510,7 +510,7 @@ miTriStrip(CARD8 op,
int ntri; int ntri;
ntri = npoints - 2; ntri = npoints - 2;
tris = malloc(ntri * sizeof(xTriangle)); tris = xallocarray(ntri, sizeof(xTriangle));
if (!tris) if (!tris)
return; return;
@ -535,7 +535,7 @@ miTriFan(CARD8 op,
int ntri; int ntri;
ntri = npoints - 2; ntri = npoints - 2;
tris = malloc(ntri * sizeof(xTriangle)); tris = xallocarray(ntri, sizeof(xTriangle));
if (!tris) if (!tris)
return; return;

View File

@ -837,7 +837,7 @@ initGradient(SourcePictPtr pGradient, int stopCount,
dpos = stopPoints[i]; dpos = stopPoints[i];
} }
pGradient->gradient.stops = malloc(stopCount * sizeof(PictGradientStop)); pGradient->gradient.stops = xallocarray(stopCount, sizeof(PictGradientStop));
if (!pGradient->gradient.stops) { if (!pGradient->gradient.stops) {
*error = BadAlloc; *error = BadAlloc;
return; return;

View File

@ -1318,14 +1318,14 @@ ProcRenderCompositeGlyphs(ClientPtr client)
if (nglyph <= NLOCALGLYPH) if (nglyph <= NLOCALGLYPH)
glyphsBase = glyphsLocal; glyphsBase = glyphsLocal;
else { else {
glyphsBase = (GlyphPtr *) malloc(nglyph * sizeof(GlyphPtr)); glyphsBase = xallocarray(nglyph, sizeof(GlyphPtr));
if (!glyphsBase) if (!glyphsBase)
return BadAlloc; return BadAlloc;
} }
if (nlist <= NLOCALDELTA) if (nlist <= NLOCALDELTA)
listsBase = listsLocal; listsBase = listsLocal;
else { else {
listsBase = (GlyphListPtr) malloc(nlist * sizeof(GlyphListRec)); listsBase = xallocarray(nlist, sizeof(GlyphListRec));
if (!listsBase) { if (!listsBase) {
rc = BadAlloc; rc = BadAlloc;
goto bail; goto bail;
@ -1793,7 +1793,7 @@ ProcRenderCreateAnimCursor(ClientPtr client)
ncursor = ncursor =
(client->req_len - (client->req_len -
(bytes_to_int32(sizeof(xRenderCreateAnimCursorReq)))) >> 1; (bytes_to_int32(sizeof(xRenderCreateAnimCursorReq)))) >> 1;
cursors = malloc(ncursor * (sizeof(CursorPtr) + sizeof(CARD32))); cursors = xallocarray(ncursor, sizeof(CursorPtr) + sizeof(CARD32));
if (!cursors) if (!cursors)
return BadAlloc; return BadAlloc;
deltas = (CARD32 *) (cursors + ncursor); deltas = (CARD32 *) (cursors + ncursor);

View File

@ -777,7 +777,7 @@ ProcXFixesExpandRegion(ClientPtr client)
nBoxes = RegionNumRects(pSource); nBoxes = RegionNumRects(pSource);
pSrc = RegionRects(pSource); pSrc = RegionRects(pSource);
if (nBoxes) { if (nBoxes) {
pTmp = malloc(nBoxes * sizeof(BoxRec)); pTmp = xallocarray(nBoxes, sizeof(BoxRec));
if (!pTmp) if (!pTmp)
return BadAlloc; return BadAlloc;
for (i = 0; i < nBoxes; i++) { for (i = 0; i < nBoxes; i++) {