dix: assert that size of buffers to swap is a multiple of the swap size

If we're swapping 4-byte integers or 2-byte integers, make sure the size
of the buffer doesn't have any bytes left over, since we won't correctly
handle those bytes.

Reported in #1817:

xwayland-24.1.6/redhat-linux-build/../dix/swaprep.c:99:22:
 warning[-Wanalyzer-allocation-size]:
 allocated buffer size is not a multiple of the pointee's size

xwayland-24.1.6/redhat-linux-build/../dix/swaprep.c:146:22:
 warning[-Wanalyzer-allocation-size]:
 allocated buffer size is not a multiple of the pointee's size

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
(cherry picked from commit cf49354b60)

Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2077>
This commit is contained in:
Alan Coopersmith 2025-10-11 18:26:55 -07:00 committed by Olivier Fourdan
parent 4a562d3776
commit 236e712ffb

View file

@ -48,6 +48,8 @@ SOFTWARE.
#include <dix-config.h>
#endif
#include <assert.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include "misc.h"
@ -95,6 +97,8 @@ CopySwap32Write(ClientPtr pClient, int size, CARD32 *pbuf)
CARD32 *from, *to, *fromLast, *toLast;
CARD32 tmpbuf[1];
assert((bufsize % sizeof(CARD32)) == 0);
/* Allocate as big a buffer as we can... */
while (!(pbufT = malloc(bufsize))) {
bufsize >>= 1;
@ -142,6 +146,8 @@ CopySwap16Write(ClientPtr pClient, int size, short *pbuf)
short *from, *to, *fromLast, *toLast;
short tmpbuf[2];
assert((bufsize % sizeof(short)) == 0);
/* Allocate as big a buffer as we can... */
while (!(pbufT = malloc(bufsize))) {
bufsize >>= 1;