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>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2078>
This commit is contained in:
Alan Coopersmith 2025-10-11 18:26:55 -07:00 committed by Marge Bot
parent 15496a5e3d
commit cf49354b60

View file

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