From 10816eb91b413db99f1c3d38acf36f433c3eb49d Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Tue, 11 Oct 2016 16:12:13 +0200 Subject: [PATCH] Do not clobber call-by-reference variables on failure Only touch *ndevices once we are sure to not return NULL. --- src/XListDev.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/XListDev.c b/src/XListDev.c index f850cd0..b3870e8 100644 --- a/src/XListDev.c +++ b/src/XListDev.c @@ -184,7 +184,7 @@ XListInputDevices( XAnyClassPtr Any; char *end = NULL; unsigned char *nptr, *Nptr; - int i; + int i, ndev; unsigned long rlen; XExtDisplayInfo *info = XInput_find_display(dpy); @@ -202,8 +202,8 @@ XListInputDevices( return (XDeviceInfo *) NULL; } - if ((*ndevices = rep.ndevices)) { /* at least 1 input device */ - size = *ndevices * sizeof(XDeviceInfo); + if ((ndev = rep.ndevices)) { /* at least 1 input device */ + size = ndev * sizeof(XDeviceInfo); if (rep.length < (INT_MAX >> 2)) { rlen = rep.length << 2; /* multiply length by 4 */ slist = list = Xmalloc(rlen); @@ -216,10 +216,10 @@ XListInputDevices( } _XRead(dpy, (char *)list, rlen); - any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo))); + any = (xAnyClassPtr) ((char *)list + (ndev * sizeof(xDeviceInfo))); sav_any = any; end = (char *)list + rlen; - for (i = 0; i < *ndevices; i++, list++) { + for (i = 0; i < ndev; i++, list++) { s = SizeClassInfo(&any, end - (char *)any, (int)list->num_classes); if (!s) goto out; @@ -227,7 +227,7 @@ XListInputDevices( } Nptr = ((unsigned char *)list) + rlen; - for (i = 0, nptr = (unsigned char *)any; i < *ndevices; i++) { + for (i = 0, nptr = (unsigned char *)any; i < ndev; i++) { if (nptr >= Nptr) goto out; size += *nptr + 1; @@ -242,11 +242,12 @@ XListInputDevices( return (XDeviceInfo *) NULL; } sclist = clist; + *ndevices = ndev; Any = (XAnyClassPtr) ((char *)clist + - (*ndevices * sizeof(XDeviceInfo))); + (ndev * sizeof(XDeviceInfo))); list = slist; any = sav_any; - for (i = 0; i < *ndevices; i++, list++, clist++) { + for (i = 0; i < ndev; i++, list++, clist++) { clist->type = list->type; clist->id = list->id; clist->use = list->use; @@ -259,7 +260,7 @@ XListInputDevices( clist = sclist; nptr = (unsigned char *)any; Nptr = (unsigned char *)Any; - for (i = 0; i < *ndevices; i++, clist++) { + for (i = 0; i < ndev; i++, clist++) { clist->name = (char *)Nptr; memcpy(Nptr, nptr + 1, *nptr); Nptr += (*nptr); -- 2.10.0