diff mbox

[libdrm] Fix strict aliasing violation in drmHandleEvent

Message ID 20161006025506.GA8538@nyan (mailing list archive)
State New, archived
Headers show

Commit Message

Felix Janda Oct. 6, 2016, 2:55 a.m. UTC
&buffer[i], e and vblank have been pointers of different types but
refering to the same memory location, thus breaking the strict
aliasing rules.

Fix this by working exclusively with pointers to char.

Signed-off-by: Felix Janda <felix.janda@posteo.de>
---
 xf86drmMode.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
diff mbox

Patch

diff --git a/xf86drmMode.c b/xf86drmMode.c
index 228c6e4..cc776c1 100644
--- a/xf86drmMode.c
+++ b/xf86drmMode.c
@@ -885,10 +885,10 @@  int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
 
 int drmHandleEvent(int fd, drmEventContextPtr evctx)
 {
-	char buffer[1024];
-	int len, i;
-	struct drm_event *e;
-	struct drm_event_vblank *vblank;
+	char buffer[1024], *p;
+	int len;
+	struct drm_event e;
+	struct drm_event_vblank vblank;
 
 	/* The DRM read semantics guarantees that we always get only
 	 * complete events. */
@@ -896,39 +896,39 @@  int drmHandleEvent(int fd, drmEventContextPtr evctx)
 	len = read(fd, buffer, sizeof buffer);
 	if (len == 0)
 		return 0;
-	if (len < (int)sizeof *e)
+	if (len < (int)sizeof e)
 		return -1;
 
-	i = 0;
-	while (i < len) {
-		e = (struct drm_event *) &buffer[i];
-		switch (e->type) {
+	p = buffer;
+	while (p < buffer + len) {
+		e = *(struct drm_event *) p;
+		switch (e.type) {
 		case DRM_EVENT_VBLANK:
 			if (evctx->version < 1 ||
 			    evctx->vblank_handler == NULL)
 				break;
-			vblank = (struct drm_event_vblank *) e;
+			vblank = *(struct drm_event_vblank *) p;
 			evctx->vblank_handler(fd,
-					      vblank->sequence,
-					      vblank->tv_sec,
-					      vblank->tv_usec,
-					      U642VOID (vblank->user_data));
+					      vblank.sequence,
+					      vblank.tv_sec,
+					      vblank.tv_usec,
+					      U642VOID (vblank.user_data));
 			break;
 		case DRM_EVENT_FLIP_COMPLETE:
 			if (evctx->version < 2 ||
 			    evctx->page_flip_handler == NULL)
 				break;
-			vblank = (struct drm_event_vblank *) e;
+			vblank = *(struct drm_event_vblank *) p;
 			evctx->page_flip_handler(fd,
-						 vblank->sequence,
-						 vblank->tv_sec,
-						 vblank->tv_usec,
-						 U642VOID (vblank->user_data));
+						 vblank.sequence,
+						 vblank.tv_sec,
+						 vblank.tv_usec,
+						 U642VOID (vblank.user_data));
 			break;
 		default:
 			break;
 		}
-		i += e->length;
+		p += e.length;
 	}
 
 	return 0;