[PATCH v2 14/17] Convert hw/xfree86 to new *allocarray functions

Alan Coopersmith alan.coopersmith at oracle.com
Thu Apr 16 18:49:22 PDT 2015


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>
---
 hw/xfree86/common/xf86AutoConfig.c      |    2 +-
 hw/xfree86/common/xf86Bus.c             |   21 ++++++++++----------
 hw/xfree86/common/xf86Config.c          |   20 +++++++++----------
 hw/xfree86/common/xf86Configure.c       |    2 +-
 hw/xfree86/common/xf86DGA.c             |    2 +-
 hw/xfree86/common/xf86Helper.c          |   32 ++++++++++++++++---------------
 hw/xfree86/common/xf86Xinput.c          |    5 +++--
 hw/xfree86/common/xf86cmap.c            |    6 +++---
 hw/xfree86/common/xf86fbman.c           |   11 ++++++-----
 hw/xfree86/common/xf86pciBus.c          |   10 +++++-----
 hw/xfree86/common/xf86platformBus.c     |    6 +++---
 hw/xfree86/common/xf86sbusBus.c         |   10 +++++-----
 hw/xfree86/common/xf86vmode.c           |    8 ++++----
 hw/xfree86/common/xf86xv.c              |   10 +++++-----
 hw/xfree86/common/xf86xvmc.c            |    2 +-
 hw/xfree86/ddc/ddc.c                    |    2 +-
 hw/xfree86/dri/xf86dri.c                |    2 +-
 hw/xfree86/dri2/dri2.c                  |    4 ++--
 hw/xfree86/drivers/modesetting/driver.c |    2 +-
 hw/xfree86/i2c/xf86i2c.c                |    2 +-
 hw/xfree86/loader/loadmod.c             |   10 +++++-----
 hw/xfree86/modes/xf86Crtc.c             |   21 ++++++++++----------
 hw/xfree86/modes/xf86DiDGA.c            |    2 +-
 hw/xfree86/modes/xf86RandR12.c          |   20 +++++++++----------
 hw/xfree86/os-support/bus/Sbus.c        |    2 +-
 hw/xfree86/vbe/vbe.c                    |    4 ++--
 26 files changed, 111 insertions(+), 107 deletions(-)

diff --git a/hw/xfree86/common/xf86AutoConfig.c b/hw/xfree86/common/xf86AutoConfig.c
index 1450afb..6b8d0eb 100644
--- a/hw/xfree86/common/xf86AutoConfig.c
+++ b/hw/xfree86/common/xf86AutoConfig.c
@@ -105,7 +105,7 @@ AppendToList(const char *s, const char ***list, int *lines)
     str = xnfstrdup(s);
     for (p = strtok(str, "\n"); p; p = strtok(NULL, "\n")) {
         (*lines)++;
-        *list = xnfrealloc(*list, (*lines + 1) * sizeof(**list));
+        *list = xnfreallocarray(*list, *lines + 1, sizeof(**list));
         newstr = xnfalloc(strlen(p) + 2);
         strcpy(newstr, p);
         strcat(newstr, "\n");
diff --git a/hw/xfree86/common/xf86Bus.c b/hw/xfree86/common/xf86Bus.c
index 02f7bf2..bd36fc5 100644
--- a/hw/xfree86/common/xf86Bus.c
+++ b/hw/xfree86/common/xf86Bus.c
@@ -256,8 +256,8 @@ int
 xf86AllocateEntity(void)
 {
     xf86NumEntities++;
-    xf86Entities = xnfrealloc(xf86Entities,
-                              sizeof(EntityPtr) * xf86NumEntities);
+    xf86Entities = xnfreallocarray(xf86Entities,
+                                   xf86NumEntities, sizeof(EntityPtr));
     xf86Entities[xf86NumEntities - 1] = xnfcalloc(1, sizeof(EntityRec));
     xf86Entities[xf86NumEntities - 1]->entityPrivates =
         xnfcalloc(xf86EntityPrivateCount, sizeof(DevUnion));
@@ -326,12 +326,13 @@ xf86AddEntityToScreen(ScrnInfoPtr pScrn, int entityIndex)
     }
 
     pScrn->numEntities++;
-    pScrn->entityList = xnfrealloc(pScrn->entityList,
-                                   pScrn->numEntities * sizeof(int));
+    pScrn->entityList = xnfreallocarray(pScrn->entityList,
+                                        pScrn->numEntities, sizeof(int));
     pScrn->entityList[pScrn->numEntities - 1] = entityIndex;
     xf86Entities[entityIndex]->inUse = TRUE;
-    pScrn->entityInstanceList = xnfrealloc(pScrn->entityInstanceList,
-                                           pScrn->numEntities * sizeof(int));
+    pScrn->entityInstanceList = xnfreallocarray(pScrn->entityInstanceList,
+                                                pScrn->numEntities,
+                                                sizeof(int));
     pScrn->entityInstanceList[pScrn->numEntities - 1] = 0;
 }
 
@@ -427,8 +428,8 @@ xf86AddDevToEntity(int entityIndex, GDevPtr dev)
 
     pEnt = xf86Entities[entityIndex];
     pEnt->numInstances++;
-    pEnt->devices = xnfrealloc(pEnt->devices,
-                               pEnt->numInstances * sizeof(GDevPtr));
+    pEnt->devices = xnfreallocarray(pEnt->devices,
+                                    pEnt->numInstances, sizeof(GDevPtr));
     pEnt->devices[pEnt->numInstances - 1] = dev;
     dev->claimed = TRUE;
 }
@@ -670,8 +671,8 @@ xf86AllocateEntityPrivateIndex(void)
     idx = xf86EntityPrivateCount++;
     for (i = 0; i < xf86NumEntities; i++) {
         pEnt = xf86Entities[i];
-        nprivs = xnfrealloc(pEnt->entityPrivates,
-                            xf86EntityPrivateCount * sizeof(DevUnion));
+        nprivs = xnfreallocarray(pEnt->entityPrivates,
+                                 xf86EntityPrivateCount, sizeof(DevUnion));
         /* Zero the new private */
         memset(&nprivs[idx], 0, sizeof(DevUnion));
         pEnt->entityPrivates = nprivs;
diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c
index 098d2dd..7edac0b 100644
--- a/hw/xfree86/common/xf86Config.c
+++ b/hw/xfree86/common/xf86Config.c
@@ -363,8 +363,8 @@ xf86ModulelistFromConfig(void ***optlist)
     /*
      * allocate the memory and walk the list again to fill in the pointers
      */
-    modulearray = xnfalloc((count + 1) * sizeof(char *));
-    optarray = xnfalloc((count + 1) * sizeof(void *));
+    modulearray = xnfallocarray(count + 1, sizeof(char *));
+    optarray = xnfallocarray(count + 1, sizeof(void *));
     count = 0;
     if (xf86configptr->conf_modules) {
         modp = xf86configptr->conf_modules->mod_load_lst;
@@ -429,7 +429,7 @@ xf86DriverlistFromConfig(void)
     /*
      * allocate the memory and walk the list again to fill in the pointers
      */
-    modulearray = xnfalloc((count + 1) * sizeof(char *));
+    modulearray = xnfallocarray(count + 1, sizeof(char *));
     count = 0;
     slp = xf86ConfigLayout.screens;
     while (slp->screen) {
@@ -493,7 +493,7 @@ xf86InputDriverlistFromConfig(void)
     /*
      * allocate the memory and walk the list again to fill in the pointers
      */
-    modulearray = xnfalloc((count + 1) * sizeof(char *));
+    modulearray = xnfallocarray(count + 1, sizeof(char *));
     count = 0;
     idp = xf86ConfigLayout.inputs;
     while (idp && *idp) {
@@ -1086,7 +1086,7 @@ addDevice(InputInfoPtr * list, InputInfoPtr pInfo)
     for (devs = list; devs && *devs; devs++)
         count++;
 
-    list = xnfrealloc(list, (count + 1) * sizeof(InputInfoPtr));
+    list = xnfreallocarray(list, count + 1, sizeof(InputInfoPtr));
     list[count] = NULL;
 
     list[count - 1] = pInfo;
@@ -1626,7 +1626,7 @@ configLayout(serverLayoutPtr servlayoutp, XF86ConfLayoutPtr conf_layout,
     }
     DebugF("Found %d inactive devices in the layout section %s\n",
            count, conf_layout->lay_identifier);
-    gdp = xnfalloc((count + 1) * sizeof(GDevRec));
+    gdp = xnfallocarray(count + 1, sizeof(GDevRec));
     gdp[count].identifier = NULL;
     idp = conf_layout->lay_inactive_lst;
     count = 0;
@@ -1746,7 +1746,7 @@ configXvAdaptor(confXvAdaptorPtr adaptor, XF86ConfVideoAdaptorPtr conf_adaptor)
         count++;
         conf_port = (XF86ConfVideoPortPtr) conf_port->list.next;
     }
-    adaptor->ports = xnfalloc((count) * sizeof(confXvPortRec));
+    adaptor->ports = xnfallocarray(count, sizeof(confXvPortRec));
     adaptor->numports = count;
     count = 0;
     conf_port = conf_adaptor->va_port_lst;
@@ -1827,7 +1827,7 @@ configScreen(confScreenPtr screenp, XF86ConfScreenPtr conf_screen, int scrnum,
         count++;
         dispptr = (XF86ConfDisplayPtr) dispptr->list.next;
     }
-    screenp->displays = xnfalloc((count) * sizeof(DispRec));
+    screenp->displays = xnfallocarray(count, sizeof(DispRec));
     screenp->numdisplays = count;
 
     /* Fill in the default Virtual size, if any */
@@ -1857,7 +1857,7 @@ configScreen(confScreenPtr screenp, XF86ConfScreenPtr conf_screen, int scrnum,
         count++;
         conf_adaptor = (XF86ConfAdaptorLinkPtr) conf_adaptor->list.next;
     }
-    screenp->xvadaptors = xnfalloc((count) * sizeof(confXvAdaptorRec));
+    screenp->xvadaptors = xnfallocarray(count, sizeof(confXvAdaptorRec));
     screenp->numxvadaptors = 0;
     conf_adaptor = conf_screen->scrn_adaptor_lst;
     while (conf_adaptor) {
@@ -2096,7 +2096,7 @@ configDisplay(DispPtr displayp, XF86ConfDisplayPtr conf_display)
         count++;
         modep = (XF86ModePtr) modep->list.next;
     }
-    displayp->modes = xnfalloc((count + 1) * sizeof(char *));
+    displayp->modes = xnfallocarray(count + 1, sizeof(char *));
     modep = conf_display->disp_mode_lst;
     count = 0;
     while (modep) {
diff --git a/hw/xfree86/common/xf86Configure.c b/hw/xfree86/common/xf86Configure.c
index 7ab378f..1271010 100644
--- a/hw/xfree86/common/xf86Configure.c
+++ b/hw/xfree86/common/xf86Configure.c
@@ -109,7 +109,7 @@ xf86AddBusDeviceToConfigure(const char *driver, BusType bus, void *busData,
     /* Allocate new structure occurrence */
     i = nDevToConfig++;
     DevToConfig =
-        xnfrealloc(DevToConfig, nDevToConfig * sizeof(DevToConfigRec));
+        xnfreallocarray(DevToConfig, nDevToConfig, sizeof(DevToConfigRec));
     memset(DevToConfig + i, 0, sizeof(DevToConfigRec));
 
     DevToConfig[i].GDev.chipID =
diff --git a/hw/xfree86/common/xf86DGA.c b/hw/xfree86/common/xf86DGA.c
index b9e1e3f..9533e1c 100644
--- a/hw/xfree86/common/xf86DGA.c
+++ b/hw/xfree86/common/xf86DGA.c
@@ -1349,7 +1349,7 @@ ProcXDGAQueryModes(ClientPtr client)
         return Success;
     }
 
-    if (!(mode = (XDGAModePtr) malloc(num * sizeof(XDGAModeRec))))
+    if (!(mode = xallocarray(num, sizeof(XDGAModeRec))))
         return BadAlloc;
 
     for (i = 0; i < num; i++)
diff --git a/hw/xfree86/common/xf86Helper.c b/hw/xfree86/common/xf86Helper.c
index e2b32a0..6a35250 100644
--- a/hw/xfree86/common/xf86Helper.c
+++ b/hw/xfree86/common/xf86Helper.c
@@ -77,8 +77,8 @@ xf86AddDriver(DriverPtr driver, void *module, int flags)
         xf86NumDrivers = 0;
 
     xf86NumDrivers++;
-    xf86DriverList = xnfrealloc(xf86DriverList,
-                                xf86NumDrivers * sizeof(DriverPtr));
+    xf86DriverList = xnfreallocarray(xf86DriverList,
+                                     xf86NumDrivers, sizeof(DriverPtr));
     xf86DriverList[xf86NumDrivers - 1] = xnfalloc(sizeof(DriverRec));
     if (flags & HaveDriverFuncs)
         *xf86DriverList[xf86NumDrivers - 1] = *driver;
@@ -117,9 +117,9 @@ xf86AddInputDriver(InputDriverPtr driver, void *module, int flags)
         xf86NumInputDrivers = 0;
 
     xf86NumInputDrivers++;
-    xf86InputDriverList = xnfrealloc(xf86InputDriverList,
-                                     xf86NumInputDrivers *
-                                     sizeof(InputDriverPtr));
+    xf86InputDriverList = xnfreallocarray(xf86InputDriverList,
+                                          xf86NumInputDrivers,
+                                          sizeof(InputDriverPtr));
     xf86InputDriverList[xf86NumInputDrivers - 1] =
         xnfalloc(sizeof(InputDriverRec));
     *xf86InputDriverList[xf86NumInputDrivers - 1] = *driver;
@@ -173,7 +173,8 @@ xf86AllocateScreen(DriverPtr drv, int flags)
         if (xf86GPUScreens == NULL)
             xf86NumGPUScreens = 0;
         i = xf86NumGPUScreens++;
-        xf86GPUScreens = xnfrealloc(xf86GPUScreens, xf86NumGPUScreens * sizeof(ScrnInfoPtr));
+        xf86GPUScreens = xnfreallocarray(xf86GPUScreens, xf86NumGPUScreens,
+                                         sizeof(ScrnInfoPtr));
         xf86GPUScreens[i] = xnfcalloc(sizeof(ScrnInfoRec), 1);
         pScrn = xf86GPUScreens[i];
         pScrn->scrnIndex = i + GPU_SCREEN_OFFSET;      /* Changes when a screen is removed */
@@ -183,7 +184,8 @@ xf86AllocateScreen(DriverPtr drv, int flags)
             xf86NumScreens = 0;
 
         i = xf86NumScreens++;
-        xf86Screens = xnfrealloc(xf86Screens, xf86NumScreens * sizeof(ScrnInfoPtr));
+        xf86Screens = xnfreallocarray(xf86Screens, xf86NumScreens,
+                                      sizeof(ScrnInfoPtr));
         xf86Screens[i] = xnfcalloc(sizeof(ScrnInfoRec), 1);
         pScrn = xf86Screens[i];
 
@@ -293,16 +295,16 @@ xf86AllocateScrnInfoPrivateIndex(void)
     idx = xf86ScrnInfoPrivateCount++;
     for (i = 0; i < xf86NumScreens; i++) {
         pScr = xf86Screens[i];
-        nprivs = xnfrealloc(pScr->privates,
-                            xf86ScrnInfoPrivateCount * sizeof(DevUnion));
+        nprivs = xnfreallocarray(pScr->privates,
+                                 xf86ScrnInfoPrivateCount, sizeof(DevUnion));
         /* Zero the new private */
         memset(&nprivs[idx], 0, sizeof(DevUnion));
         pScr->privates = nprivs;
     }
     for (i = 0; i < xf86NumGPUScreens; i++) {
         pScr = xf86GPUScreens[i];
-        nprivs = xnfrealloc(pScr->privates,
-                            xf86ScrnInfoPrivateCount * sizeof(DevUnion));
+        nprivs = xnfreallocarray(pScr->privates,
+                                 xf86ScrnInfoPrivateCount, sizeof(DevUnion));
         /* Zero the new private */
         memset(&nprivs[idx], 0, sizeof(DevUnion));
         pScr->privates = nprivs;
@@ -636,8 +638,8 @@ xf86SetDepthBpp(ScrnInfoPtr scrp, int depth, int dummy, int fbbpp,
     if (i == scrp->confScreen->numdisplays) {
         scrp->confScreen->numdisplays++;
         scrp->confScreen->displays =
-            xnfrealloc(scrp->confScreen->displays,
-                       scrp->confScreen->numdisplays * sizeof(DispRec));
+            xnfreallocarray(scrp->confScreen->displays,
+                            scrp->confScreen->numdisplays, sizeof(DispRec));
         xf86DrvMsg(scrp->scrnIndex, X_INFO,
                    "Creating default Display subsection in Screen section\n"
                    "\t\"%s\" for depth/fbbpp %d/%d\n",
@@ -1408,7 +1410,7 @@ xf86MatchDevice(const char *drivername, GDevPtr ** sectlist)
             /*
              * we have a matching driver that wasn't claimed, yet
              */
-            pgdp = xnfrealloc(pgdp, (i + 2) * sizeof(GDevPtr));
+            pgdp = xnfreallocarray(pgdp, i + 2, sizeof(GDevPtr));
             pgdp[i++] = screensecptr->device;
         }
     }
@@ -1420,7 +1422,7 @@ xf86MatchDevice(const char *drivername, GDevPtr ** sectlist)
         if (gdp->driver && !gdp->claimed &&
             !xf86NameCmp(gdp->driver, drivername)) {
             /* we have a matching driver that wasn't claimed yet */
-            pgdp = xnfrealloc(pgdp, (i + 2) * sizeof(GDevPtr));
+            pgdp = xnfreallocarray(pgdp, i + 2, sizeof(GDevPtr));
             pgdp[i++] = gdp;
         }
         j++;
diff --git a/hw/xfree86/common/xf86Xinput.c b/hw/xfree86/common/xf86Xinput.c
index 9fa3dc4..55bf2bb 100644
--- a/hw/xfree86/common/xf86Xinput.c
+++ b/hw/xfree86/common/xf86Xinput.c
@@ -867,8 +867,9 @@ xf86NewInputDevice(InputInfoPtr pInfo, DeviceIntPtr *pdev, BOOL enable)
         if (fd != -1) {
             if (paused) {
                 /* Put on new_input_devices list for delayed probe */
-                new_input_devices = xnfrealloc(new_input_devices,
-                            sizeof(pInfo) * (new_input_devices_count + 1));
+                new_input_devices = xnfreallocarray(new_input_devices,
+                                                    new_input_devices_count + 1,
+                                                    sizeof(pInfo));
                 new_input_devices[new_input_devices_count] = pInfo;
                 new_input_devices_count++;
                 systemd_logind_release_fd(pInfo->major, pInfo->minor, fd);
diff --git a/hw/xfree86/common/xf86cmap.c b/hw/xfree86/common/xf86cmap.c
index ab51f96..704e353 100644
--- a/hw/xfree86/common/xf86cmap.c
+++ b/hw/xfree86/common/xf86cmap.c
@@ -166,10 +166,10 @@ xf86HandleColormaps(ScreenPtr pScreen,
 
     elements = 1 << sigRGBbits;
 
-    if (!(gamma = malloc(elements * sizeof(LOCO))))
+    if (!(gamma = xallocarray(elements, sizeof(LOCO))))
         return FALSE;
 
-    if (!(indices = malloc(maxColors * sizeof(int)))) {
+    if (!(indices = xallocarray(maxColors, sizeof(int)))) {
         free(gamma);
         return FALSE;
     }
@@ -270,7 +270,7 @@ CMapAllocateColormapPrivate(ColormapPtr pmap)
     else
         numColors = 1 << pmap->pVisual->nplanes;
 
-    if (!(colors = malloc(numColors * sizeof(LOCO))))
+    if (!(colors = xallocarray(numColors, sizeof(LOCO))))
         return FALSE;
 
     if (!(pColPriv = malloc(sizeof(CMapColormapRec)))) {
diff --git a/hw/xfree86/common/xf86fbman.c b/hw/xfree86/common/xf86fbman.c
index 1b2cb57..91ddedc 100644
--- a/hw/xfree86/common/xf86fbman.c
+++ b/hw/xfree86/common/xf86fbman.c
@@ -317,16 +317,17 @@ localRegisterFreeBoxCallback(ScreenPtr pScreen,
 
     offman = (FBManagerPtr) dixLookupPrivate(&pScreen->devPrivates,
                                              xf86FBScreenKey);
-    newCallbacks = realloc(offman->FreeBoxesUpdateCallback,
-                           sizeof(FreeBoxCallbackProcPtr) *
-                           (offman->NumCallbacks + 1));
+    newCallbacks = reallocarray(offman->FreeBoxesUpdateCallback,
+                                offman->NumCallbacks + 1,
+                                sizeof(FreeBoxCallbackProcPtr));
     if (!newCallbacks)
         return FALSE;
     else
         offman->FreeBoxesUpdateCallback = newCallbacks;
 
-    newPrivates = realloc(offman->devPrivates,
-                          sizeof(DevUnion) * (offman->NumCallbacks + 1));
+    newPrivates = reallocarray(offman->devPrivates,
+                               offman->NumCallbacks + 1,
+                               sizeof(DevUnion));
     if (!newPrivates)
         return FALSE;
     else
diff --git a/hw/xfree86/common/xf86pciBus.c b/hw/xfree86/common/xf86pciBus.c
index e86ecb9..8158c2b 100644
--- a/hw/xfree86/common/xf86pciBus.c
+++ b/hw/xfree86/common/xf86pciBus.c
@@ -103,9 +103,9 @@ xf86PciProbe(void)
     while ((info = pci_device_next(iter)) != NULL) {
         if (PCIINFOCLASSES(info->device_class)) {
             num++;
-            xf86PciVideoInfo = xnfrealloc(xf86PciVideoInfo,
-                                          (sizeof(struct pci_device *)
-                                           * (num + 1)));
+            xf86PciVideoInfo = xnfreallocarray(xf86PciVideoInfo,
+                                               num + 1,
+                                               sizeof(struct pci_device *));
             xf86PciVideoInfo[num] = NULL;
             xf86PciVideoInfo[num - 1] = info;
 
@@ -679,7 +679,7 @@ xf86MatchPciInstances(const char *driverName, int vendorID,
         }
 
         pci_iterator_destroy(iter);
-        instances = xnfalloc(max_entries * sizeof(struct Inst));
+        instances = xnfallocarray(max_entries, sizeof(struct Inst));
     }
 
     iter = pci_slot_match_iterator_create(NULL);
@@ -976,7 +976,7 @@ xf86MatchPciInstances(const char *driverName, int vendorID,
 
         /* Allocate an entry in the lists to be returned */
         numFound++;
-        retEntities = xnfrealloc(retEntities, numFound * sizeof(int));
+        retEntities = xnfreallocarray(retEntities, numFound, sizeof(int));
         retEntities[numFound - 1] = xf86ClaimPciSlot(pPci, drvp,
                                                      instances[i].chip,
                                                      instances[i].dev,
diff --git a/hw/xfree86/common/xf86platformBus.c b/hw/xfree86/common/xf86platformBus.c
index c1aaba4..f1e9423 100644
--- a/hw/xfree86/common/xf86platformBus.c
+++ b/hw/xfree86/common/xf86platformBus.c
@@ -59,9 +59,9 @@ struct xf86_platform_device *xf86_platform_devices;
 int
 xf86_add_platform_device(struct OdevAttributes *attribs, Bool unowned)
 {
-    xf86_platform_devices = xnfrealloc(xf86_platform_devices,
-                                   (sizeof(struct xf86_platform_device)
-                                    * (xf86_num_platform_devices + 1)));
+    xf86_platform_devices = xnfreallocarray(xf86_platform_devices,
+                                            xf86_num_platform_devices + 1,
+                                            sizeof(struct xf86_platform_device));
 
     xf86_platform_devices[xf86_num_platform_devices].attribs = attribs;
     xf86_platform_devices[xf86_num_platform_devices].pdev = NULL;
diff --git a/hw/xfree86/common/xf86sbusBus.c b/hw/xfree86/common/xf86sbusBus.c
index 07eb71e..119211d 100644
--- a/hw/xfree86/common/xf86sbusBus.c
+++ b/hw/xfree86/common/xf86sbusBus.c
@@ -68,7 +68,7 @@ CheckSbusDevice(const char *device, int fbNum)
     if (!sbusDeviceTable[i].devId)
         return;
     xf86SbusInfo =
-        xnfrealloc(xf86SbusInfo, sizeof(psdp) * (++xf86nSbusInfo + 1));
+        xnfreallocarray(xf86SbusInfo, ++xf86nSbusInfo + 1, sizeof(psdp));
     xf86SbusInfo[xf86nSbusInfo] = NULL;
     xf86SbusInfo[xf86nSbusInfo - 1] = psdp = xnfcalloc(sizeof(sbusDevice), 1);
     psdp->devId = sbusDeviceTable[i].devId;
@@ -406,8 +406,8 @@ xf86MatchSbusInstances(const char *driverName, int sbusDevId,
         if (psdp->fd == -2)
             continue;
         ++allocatedInstances;
-        instances = xnfrealloc(instances,
-                               allocatedInstances * sizeof(struct Inst));
+        instances = xnfreallocarray(instances,
+                                    allocatedInstances, sizeof(struct Inst));
         instances[allocatedInstances - 1].sbus = psdp;
         instances[allocatedInstances - 1].dev = NULL;
         instances[allocatedInstances - 1].claimed = FALSE;
@@ -532,7 +532,7 @@ xf86MatchSbusInstances(const char *driverName, int sbusDevId,
 
         /* Allocate an entry in the lists to be returned */
         numFound++;
-        retEntities = xnfrealloc(retEntities, numFound * sizeof(int));
+        retEntities = xnfreallocarray(retEntities, numFound, sizeof(int));
         retEntities[numFound - 1]
             = xf86ClaimSbusSlot(psdp, drvp, instances[i].dev,
                                 instances[i].dev->active ? TRUE : FALSE);
@@ -648,7 +648,7 @@ xf86SbusCmapLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
         return;
     fbcmap.count = 0;
     fbcmap.index = indices[0];
-    fbcmap.red = data = malloc(numColors * 3);
+    fbcmap.red = data = xallocarray(numColors, 3);
     if (!data)
         return;
     fbcmap.green = data + numColors;
diff --git a/hw/xfree86/common/xf86vmode.c b/hw/xfree86/common/xf86vmode.c
index 0ce58e3..818e7dc 100644
--- a/hw/xfree86/common/xf86vmode.c
+++ b/hw/xfree86/common/xf86vmode.c
@@ -1240,11 +1240,11 @@ ProcXF86VidModeGetMonitor(ClientPtr client)
                        pad_to_int32(rep.modelLength));
     rep.nhsync = nHsync;
     rep.nvsync = nVrefresh;
-    hsyncdata = malloc(nHsync * sizeof(CARD32));
+    hsyncdata = xallocarray(nHsync, sizeof(CARD32));
     if (!hsyncdata) {
         return BadAlloc;
     }
-    vsyncdata = malloc(nVrefresh * sizeof(CARD32));
+    vsyncdata = xallocarray(nVrefresh, sizeof(CARD32));
 
     if (!vsyncdata) {
         free(hsyncdata);
@@ -1512,9 +1512,9 @@ ProcXF86VidModeGetGammaRamp(ClientPtr client)
     length = (stuff->size + 1) & ~1;
 
     if (stuff->size) {
-        ramplen = length * 3 * sizeof(CARD16);
-        if (!(ramp = malloc(ramplen)))
+        if (!(ramp = xallocarray(length, 3 * sizeof(CARD16))))
             return BadAlloc;
+        ramplen = length * 3 * sizeof(CARD16);
 
         if (!VidModeGetGammaRamp(stuff->screen, stuff->size,
                                  ramp, ramp + length, ramp + (length * 2))) {
diff --git a/hw/xfree86/common/xf86xv.c b/hw/xfree86/common/xf86xv.c
index b974cd2..d613d71 100644
--- a/hw/xfree86/common/xf86xv.c
+++ b/hw/xfree86/common/xf86xv.c
@@ -131,8 +131,8 @@ xf86XVRegisterGenericAdaptorDriver(xf86XVInitGenericAdaptorPtr InitFunc)
 {
     xf86XVInitGenericAdaptorPtr *newdrivers;
 
-    newdrivers = realloc(GenDrivers, sizeof(xf86XVInitGenericAdaptorPtr) *
-                         (1 + NumGenDrivers));
+    newdrivers = reallocarray(GenDrivers, 1 + NumGenDrivers,
+                              sizeof(xf86XVInitGenericAdaptorPtr));
     if (!newdrivers)
         return 0;
     GenDrivers = newdrivers;
@@ -159,7 +159,7 @@ xf86XVListGenericAdaptors(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr ** adaptors)
         n = (*GenDrivers[i]) (pScrn, &DrivAdap);
         if (0 == n)
             continue;
-        new = realloc(*adaptors, sizeof(XF86VideoAdaptorPtr) * (num + n));
+        new = reallocarray(*adaptors, num + n, sizeof(XF86VideoAdaptorPtr));
         if (NULL == new)
             continue;
         *adaptors = new;
@@ -436,8 +436,8 @@ xf86XVInitAdaptors(ScreenPtr pScreen, XF86VideoAdaptorPtr * infoPtr, int number)
                         void *moreSpace;
 
                         totFormat *= 2;
-                        moreSpace = realloc(pFormat,
-                                            totFormat * sizeof(XvFormatRec));
+                        moreSpace = reallocarray(pFormat, totFormat,
+                                                 sizeof(XvFormatRec));
                         if (!moreSpace)
                             break;
                         pFormat = moreSpace;
diff --git a/hw/xfree86/common/xf86xvmc.c b/hw/xfree86/common/xf86xvmc.c
index 3169c05..a0a94c7 100644
--- a/hw/xfree86/common/xf86xvmc.c
+++ b/hw/xfree86/common/xf86xvmc.c
@@ -155,7 +155,7 @@ xf86XvMCScreenInit(ScreenPtr pScreen,
     if (noXvExtension)
         return FALSE;
 
-    if (!(pAdapt = malloc(sizeof(XvMCAdaptorRec) * num_adaptors)))
+    if (!(pAdapt = xallocarray(num_adaptors, sizeof(XvMCAdaptorRec))))
         return FALSE;
 
     if (!dixRegisterPrivateKey(&XF86XvMCScreenKeyRec, PRIVATE_SCREEN, 0)) {
diff --git a/hw/xfree86/ddc/ddc.c b/hw/xfree86/ddc/ddc.c
index 29185ad..ee533db 100644
--- a/hw/xfree86/ddc/ddc.c
+++ b/hw/xfree86/ddc/ddc.c
@@ -437,7 +437,7 @@ xf86DoEEDID(ScrnInfoPtr pScrn, I2CBusPtr pBus, Bool complete)
         int i, n = EDID_block[0x7e];
 
         if (complete && n) {
-            EDID_block = realloc(EDID_block, EDID1_LEN * (1 + n));
+            EDID_block = reallocarray(EDID_block, 1 + n, EDID1_LEN);
 
             for (i = 0; i < n; i++)
                 DDC2Read(dev, i + 1, EDID_block + (EDID1_LEN * (1 + i)));
diff --git a/hw/xfree86/dri/xf86dri.c b/hw/xfree86/dri/xf86dri.c
index 086e833..68f8b7e 100644
--- a/hw/xfree86/dri/xf86dri.c
+++ b/hw/xfree86/dri/xf86dri.c
@@ -422,7 +422,7 @@ ProcXF86DRIGetDrawableInfo(register ClientPtr client)
 
     if (rep.numClipRects) {
         /* Clip cliprects to screen dimensions (redirected windows) */
-        pClippedRects = malloc(rep.numClipRects * sizeof(drm_clip_rect_t));
+        pClippedRects = xallocarray(rep.numClipRects, sizeof(drm_clip_rect_t));
 
         if (pClippedRects) {
             ScreenPtr pScreen = screenInfo.screens[stuff->screen];
diff --git a/hw/xfree86/dri2/dri2.c b/hw/xfree86/dri2/dri2.c
index 0c038b3..60ea6dd 100644
--- a/hw/xfree86/dri2/dri2.c
+++ b/hw/xfree86/dri2/dri2.c
@@ -1577,7 +1577,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info)
     if (info->version == 3 || info->numDrivers == 0) {
         /* Driver too old: use the old-style driverName field */
         ds->numDrivers = info->driverName ? 1 : 2;
-        ds->driverNames = malloc(ds->numDrivers * sizeof(*ds->driverNames));
+        ds->driverNames = xallocarray(ds->numDrivers, sizeof(*ds->driverNames));
         if (!ds->driverNames)
             goto err_out;
 
@@ -1591,7 +1591,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info)
     }
     else {
         ds->numDrivers = info->numDrivers;
-        ds->driverNames = malloc(info->numDrivers * sizeof(*ds->driverNames));
+        ds->driverNames = xallocarray(info->numDrivers, sizeof(*ds->driverNames));
         if (!ds->driverNames)
             goto err_out;
         memcpy(ds->driverNames, info->driverNames,
diff --git a/hw/xfree86/drivers/modesetting/driver.c b/hw/xfree86/drivers/modesetting/driver.c
index e2f3846..e90e4b8 100644
--- a/hw/xfree86/drivers/modesetting/driver.c
+++ b/hw/xfree86/drivers/modesetting/driver.c
@@ -451,7 +451,7 @@ dispatch_dirty_region(ScrnInfoPtr scrn,
     int ret = 0;
 
     if (num_cliprects) {
-        drmModeClip *clip = malloc(num_cliprects * sizeof(drmModeClip));
+        drmModeClip *clip = xallocarray(num_cliprects, sizeof(drmModeClip));
         BoxPtr rect = REGION_RECTS(dirty);
         int i;
 
diff --git a/hw/xfree86/i2c/xf86i2c.c b/hw/xfree86/i2c/xf86i2c.c
index cf2cd09..2a8b8df 100644
--- a/hw/xfree86/i2c/xf86i2c.c
+++ b/hw/xfree86/i2c/xf86i2c.c
@@ -872,7 +872,7 @@ xf86I2CGetScreenBuses(int scrnIndex, I2CBusPtr ** pppI2CBus)
         if (!pppI2CBus)
             continue;
 
-        *pppI2CBus = xnfrealloc(*pppI2CBus, n * sizeof(I2CBusPtr));
+        *pppI2CBus = xnfreallocarray(*pppI2CBus, n, sizeof(I2CBusPtr));
         (*pppI2CBus)[n - 1] = pI2CBus;
     }
 
diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
index fdf5bd8..73dc1b8 100644
--- a/hw/xfree86/loader/loadmod.c
+++ b/hw/xfree86/loader/loadmod.c
@@ -142,7 +142,7 @@ InitPathList(const char *path)
             if (addslash)
                 len++;
             save = list;
-            list = realloc(list, (n + 2) * sizeof(char *));
+            list = reallocarray(list, n + 2, sizeof(char *));
             if (!list) {
                 if (save) {
                     save[n] = NULL;
@@ -244,7 +244,7 @@ InitPatterns(const char **patternlist)
         for (i = 0, s = patternlist; *s; i++, s++)
             if (*s == DEFAULT_LIST)
                 i += sizeof(stdPatterns) / sizeof(stdPatterns[0]) - 1 - 1;
-        patterns = malloc((i + 1) * sizeof(PatternRec));
+        patterns = xallocarray(i + 1, sizeof(PatternRec));
         if (!patterns) {
             return NULL;
         }
@@ -323,7 +323,7 @@ InitSubdirs(const char **subdirlist)
                 }
             }
         }
-        subdirs = malloc((i * 2 + 1) * sizeof(char *));
+        subdirs = xallocarray(i * 2 + 1, sizeof(char *));
         if (!subdirs) {
             free(tmp_subdirlist);
             return NULL;
@@ -530,8 +530,8 @@ LoaderListDirs(const char **subdirlist, const char **patternlist)
                             match[1].rm_so != -1) {
                             len = match[1].rm_eo - match[1].rm_so;
                             save = listing;
-                            listing = realloc(listing,
-                                              (n + 2) * sizeof(char *));
+                            listing = reallocarray(listing, n + 2,
+                                                   sizeof(char *));
                             if (!listing) {
                                 if (save) {
                                     save[n] = NULL;
diff --git a/hw/xfree86/modes/xf86Crtc.c b/hw/xfree86/modes/xf86Crtc.c
index a194724..127e543 100644
--- a/hw/xfree86/modes/xf86Crtc.c
+++ b/hw/xfree86/modes/xf86Crtc.c
@@ -118,7 +118,7 @@ xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs)
 
     /* Preallocate gamma at a sensible size. */
     crtc->gamma_size = 256;
-    crtc->gamma_red = malloc(3 * crtc->gamma_size * sizeof(CARD16));
+    crtc->gamma_red = xallocarray(crtc->gamma_size, 3 * sizeof(CARD16));
     if (!crtc->gamma_red) {
         free(crtc);
         return NULL;
@@ -127,10 +127,10 @@ xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs)
     crtc->gamma_blue = crtc->gamma_green + crtc->gamma_size;
 
     if (xf86_config->crtc)
-        crtcs = realloc(xf86_config->crtc,
-                        (xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
+        crtcs = reallocarray(xf86_config->crtc,
+                             xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr));
     else
-        crtcs = malloc((xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
+        crtcs = xallocarray(xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr));
     if (!crtcs) {
         free(crtc->gamma_red);
         free(crtc);
@@ -620,11 +620,12 @@ xf86OutputCreate(ScrnInfoPtr scrn,
     }
 
     if (xf86_config->output)
-        outputs = realloc(xf86_config->output,
-                          (xf86_config->num_output +
-                           1) * sizeof(xf86OutputPtr));
+        outputs = reallocarray(xf86_config->output,
+                               xf86_config->num_output + 1,
+                               sizeof(xf86OutputPtr));
     else
-        outputs = malloc((xf86_config->num_output + 1) * sizeof(xf86OutputPtr));
+        outputs = xallocarray(xf86_config->num_output + 1,
+                              sizeof(xf86OutputPtr));
     if (!outputs) {
         free(output);
         return NULL;
@@ -942,7 +943,7 @@ xf86PickCrtcs(ScrnInfoPtr scrn,
     if (modes[n] == NULL)
         return best_score;
 
-    crtcs = malloc(config->num_output * sizeof(xf86CrtcPtr));
+    crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr));
     if (!crtcs)
         return best_score;
 
@@ -2334,7 +2335,7 @@ xf86CrtcSetInitialGamma(xf86CrtcPtr crtc, float gamma_red, float gamma_green,
     int i, size = 256;
     CARD16 *red, *green, *blue;
 
-    red = malloc(3 * size * sizeof(CARD16));
+    red = xallocarray(size, 3 * sizeof(CARD16));
     green = red + size;
     blue = green + size;
 
diff --git a/hw/xfree86/modes/xf86DiDGA.c b/hw/xfree86/modes/xf86DiDGA.c
index 3f1a330..6457274 100644
--- a/hw/xfree86/modes/xf86DiDGA.c
+++ b/hw/xfree86/modes/xf86DiDGA.c
@@ -60,7 +60,7 @@ xf86_dga_get_modes(ScreenPtr pScreen)
     if (!num)
         return FALSE;
 
-    modes = malloc(num * sizeof(DGAModeRec));
+    modes = xallocarray(num, sizeof(DGAModeRec));
     if (!modes)
         return FALSE;
 
diff --git a/hw/xfree86/modes/xf86RandR12.c b/hw/xfree86/modes/xf86RandR12.c
index b1c306a..fb73128 100644
--- a/hw/xfree86/modes/xf86RandR12.c
+++ b/hw/xfree86/modes/xf86RandR12.c
@@ -1058,7 +1058,7 @@ xf86RandR12CrtcNotify(RRCrtcPtr randr_crtc)
     DisplayModePtr mode = &crtc->mode;
     Bool ret;
 
-    randr_outputs = malloc(config->num_output * sizeof(RROutputPtr));
+    randr_outputs = xallocarray(config->num_output, sizeof(RROutputPtr));
     if (!randr_outputs)
         return FALSE;
     x = crtc->x;
@@ -1150,7 +1150,7 @@ xf86RandR12CrtcSet(ScreenPtr pScreen,
     if (!crtc->scrn->vtSema)
         return FALSE;
 
-    save_crtcs = malloc(config->num_output * sizeof(xf86CrtcPtr));
+    save_crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr));
     if ((randr_mode != NULL) != crtc->enabled)
         changed = TRUE;
     else if (randr_mode && !xf86RandRModeMatches(randr_mode, &crtc->mode))
@@ -1255,9 +1255,8 @@ xf86RandR12CrtcSetGamma(ScreenPtr pScreen, RRCrtcPtr randr_crtc)
     if (randr_crtc->gammaSize != crtc->gamma_size) {
         CARD16 *tmp_ptr;
 
-        tmp_ptr =
-            realloc(crtc->gamma_red,
-                    3 * randr_crtc->gammaSize * sizeof(CARD16));
+        tmp_ptr = reallocarray(crtc->gamma_red,
+                               randr_crtc->gammaSize, 3 * sizeof(CARD16));
         if (!tmp_ptr)
             return FALSE;
         crtc->gamma_red = tmp_ptr;
@@ -1298,9 +1297,8 @@ xf86RandR12CrtcGetGamma(ScreenPtr pScreen, RRCrtcPtr randr_crtc)
     if (randr_crtc->gammaSize != crtc->gamma_size) {
         CARD16 *tmp_ptr;
 
-        tmp_ptr =
-            realloc(randr_crtc->gammaRed,
-                    3 * crtc->gamma_size * sizeof(CARD16));
+        tmp_ptr = reallocarray(randr_crtc->gammaRed,
+                               crtc->gamma_size, 3 * sizeof(CARD16));
         if (!tmp_ptr)
             return FALSE;
         randr_crtc->gammaRed = tmp_ptr;
@@ -1394,7 +1392,7 @@ xf86RROutputSetModes(RROutputPtr randr_output, DisplayModePtr modes)
         nmode++;
 
     if (nmode) {
-        rrmodes = malloc(nmode * sizeof(RRModePtr));
+        rrmodes = xallocarray(nmode, sizeof(RRModePtr));
 
         if (!rrmodes)
             return FALSE;
@@ -1449,8 +1447,8 @@ xf86RandR12SetInfo12(ScreenPtr pScreen)
     int o, c, l;
     int nclone;
 
-    clones = malloc(config->num_output * sizeof(RROutputPtr));
-    crtcs = malloc(config->num_crtc * sizeof(RRCrtcPtr));
+    clones = xallocarray(config->num_output, sizeof(RROutputPtr));
+    crtcs = xallocarray(config->num_crtc, sizeof(RRCrtcPtr));
     for (o = 0; o < config->num_output; o++) {
         xf86OutputPtr output = config->output[o];
 
diff --git a/hw/xfree86/os-support/bus/Sbus.c b/hw/xfree86/os-support/bus/Sbus.c
index 16ce5b5..86b4d68 100644
--- a/hw/xfree86/os-support/bus/Sbus.c
+++ b/hw/xfree86/os-support/bus/Sbus.c
@@ -440,7 +440,7 @@ sparcPromAssignNodes(void)
     for (i = 0, j = 0; i < 32; i++)
         if (devicePtrs[i] && devicePtrs[i]->fbNum == -1)
             j++;
-    xf86SbusInfo = xnfrealloc(xf86SbusInfo, sizeof(psdp) * (n + j + 1));
+    xf86SbusInfo = xnfreallocarray(xf86SbusInfo, n + j + 1, sizeof(psdp));
     for (i = 0, psdpp = xf86SbusInfo; i < 32; i++)
         if (devicePtrs[i]) {
             if (devicePtrs[i]->fbNum == -1) {
diff --git a/hw/xfree86/vbe/vbe.c b/hw/xfree86/vbe/vbe.c
index 5ea0197..ef12cb8 100644
--- a/hw/xfree86/vbe/vbe.c
+++ b/hw/xfree86/vbe/vbe.c
@@ -397,7 +397,7 @@ VBEGetVBEInfo(vbeInfoPtr pVbe)
     i = 0;
     while (modes[i] != 0xffff)
         i++;
-    block->VideoModePtr = malloc(sizeof(CARD16) * (i + 1));
+    block->VideoModePtr = xallocarray(i + 1, sizeof(CARD16));
     memcpy(block->VideoModePtr, modes, sizeof(CARD16) * i);
     block->VideoModePtr[i] = 0xffff;
 
@@ -825,7 +825,7 @@ VBESetGetPaletteData(vbeInfoPtr pVbe, Bool set, int first, int num,
     if (set)
         return data;
 
-    data = malloc(num * sizeof(CARD32));
+    data = xallocarray(num, sizeof(CARD32));
     memcpy(data, pVbe->memory, num * sizeof(CARD32));
 
     return data;
-- 
1.7.9.2



More information about the xorg-devel mailing list