9a0bab5
From 0f318c5042c3bec3b139acadcc75ad3c3de8187c Mon Sep 17 00:00:00 2001
9a0bab5
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
9a0bab5
Date: Tue, 27 Aug 2019 15:13:50 +0900
9a0bab5
Subject: [PATCH] glhanoi: fix malloc size shortage
9a0bab5
9a0bab5
5.43 glhanoi segfaults like:
9a0bab5
    #0 0x7f87473de5ef  (/lib64/libasan.so.5+0x9b5ef)
9a0bab5
    #1 0x7f8741e2cd1e  (/usr/lib64/dri/swrast_dri.so+0x138d1e)
9a0bab5
    #2 0x7f8742655025  (/usr/lib64/dri/swrast_dri.so+0x961025)
9a0bab5
    #3 0x7f87421f4b8f  (/usr/lib64/dri/swrast_dri.so+0x500b8f)
9a0bab5
    #4 0x7f87421f5a96  (/usr/lib64/dri/swrast_dri.so+0x501a96)
9a0bab5
    #5 0x7f87421b8ca2  (/usr/lib64/dri/swrast_dri.so+0x4c4ca2)
9a0bab5
    #6 0x7f87421ba7e1  (/usr/lib64/dri/swrast_dri.so+0x4c67e1)
9a0bab5
    #7 0x41c371 in makeTextures ../../../hacks/glx/glhanoi.c:1560
9a0bab5
    #8 0x422032 in init_glhanoi ../../../hacks/glx/glhanoi.c:1927
9a0bab5
    #9 0x438a34 in xlockmore_do_init ../../hacks/xlockmore.c:576
9a0bab5
    #10 0x438de7 in xlockmore_check_init ../../hacks/xlockmore.c:603
9a0bab5
    #11 0x439b69 in xlockmore_event ../../hacks/xlockmore.c:679
9a0bab5
    #12 0x427983 in screenhack_table_handle_events ../../hacks/screenhack.c:469
9a0bab5
    #13 0x427cfd in usleep_and_process_events ../../hacks/screenhack.c:526
9a0bab5
    #14 0x428031 in run_screenhack_table ../../hacks/screenhack.c:586
9a0bab5
    #15 0x42ae48 in main ../../hacks/screenhack.c:987
9a0bab5
    #16 0x7f874632af32 in __libc_start_main (/lib64/libc.so.6+0x23f32)
9a0bab5
    #17 0x406e9d in _start
9a0bab5
9a0bab5
Actually this is because of malloced buffer size shortage in makeTexture().
9a0bab5
As we use GL_RGBA format, we must assign 4 bytes per element.
9a0bab5
Also, casting from (GLubyte *) to (GLuint *) (i.e. possibly pointer with larger
9a0bab5
aligment) should be avoided. To avoid this, declare textureData as
9a0bab5
(GLuint *) first, then cast it to  (GLubyte *) when return.
9a0bab5
(Note buffer returned by malloc is guarateed to be casted to any basic type.)
9a0bab5
---
9a0bab5
 hacks/glx/glhanoi.c | 9 +++++----
9a0bab5
 1 file changed, 5 insertions(+), 4 deletions(-)
9a0bab5
9a0bab5
diff --git a/hacks/glx/glhanoi.c b/hacks/glx/glhanoi.c
9a0bab5
index 46cdc68..68126cf 100644
9a0bab5
--- a/hacks/glx/glhanoi.c
9a0bab5
+++ b/hacks/glx/glhanoi.c
9a0bab5
@@ -1424,13 +1424,14 @@ static GLubyte *makeTexture(glhcfg *glhanoi, int x_size, int y_size, int z_size,
9a0bab5
 									   tex_col_t *), tex_col_t * colours)
9a0bab5
 {
9a0bab5
 	int i, j, k;
9a0bab5
-	GLubyte *textureData;
9a0bab5
+	GLuint *textureData;
9a0bab5
 	GLuint *texturePtr;
9a0bab5
 	double x, y, z;
9a0bab5
 	double xi, yi, zi;
9a0bab5
 
9a0bab5
+	/* As we use GL_RGBA format, we must assign 4 bytes per element */
9a0bab5
 	if((textureData =
9a0bab5
-		calloc(x_size * y_size * z_size, sizeof(GLubyte))) == NULL) {
9a0bab5
+		calloc(x_size * y_size * z_size, sizeof(GLuint))) == NULL) {
9a0bab5
 		return NULL;
9a0bab5
 	}
9a0bab5
 
9a0bab5
@@ -1439,7 +1440,7 @@ static GLubyte *makeTexture(glhcfg *glhanoi, int x_size, int y_size, int z_size,
9a0bab5
 	zi = 1.0 / z_size;
9a0bab5
 
9a0bab5
 	z = 0.0;
9a0bab5
-	texturePtr = (void *)textureData;
9a0bab5
+	texturePtr = textureData;
9a0bab5
 	for(k = 0; k < z_size; k++, z += zi) {
9a0bab5
 		y = 0.0;
9a0bab5
 		for(j = 0; j < y_size; j++, y += yi) {
9a0bab5
@@ -1450,7 +1451,7 @@ static GLubyte *makeTexture(glhcfg *glhanoi, int x_size, int y_size, int z_size,
9a0bab5
 			}
9a0bab5
 		}
9a0bab5
 	}
9a0bab5
-	return textureData;
9a0bab5
+	return (GLubyte *)textureData;
9a0bab5
 }
9a0bab5
 
9a0bab5
 static void freeTexCols(tex_col_t*p)
9a0bab5
-- 
9a0bab5
2.21.0
9a0bab5