[mutter/wip/smcv/cogl-left-shift] cogl tests: Force defined behaviour for 24-bit left-shifts



commit 4af716a878275c664923fb90335cce1be863eaed
Author: Simon McVittie <smcv debian org>
Date:   Fri Sep 6 11:42:47 2019 +0100

    cogl tests: Force defined behaviour for 24-bit left-shifts
    
    When r is 128 or more, running tests compiled with the undefined behaviour
    sanitizer (ubsan) reports:
    
    test-utils.c:312:45: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'
    
    which indeed it cannot. Force the type to be unsigned 32-bit so that we
    get defined behaviour.
    
    Similarly, in test-atlas-migration, the left-shifted guint8 is promoted
    to int, which again does not have enough non-sign bits available to
    left-shift a value >= 128 by 24 bits. Again, force the shift to be done
    in unsigned 32-bit space.
    
    This was originally cogl!22, but applies equally to mutter's fork of cogl.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1271
    
    Signed-off-by: Simon McVittie <smcv debian org>

 cogl/test-fixtures/test-utils.c           | 12 +++++++++++-
 cogl/tests/conform/test-atlas-migration.c |  6 +++---
 2 files changed, 14 insertions(+), 4 deletions(-)
---
diff --git a/cogl/test-fixtures/test-utils.c b/cogl/test-fixtures/test-utils.c
index 6834f9fac7..47586867e4 100644
--- a/cogl/test-fixtures/test-utils.c
+++ b/cogl/test-fixtures/test-utils.c
@@ -250,7 +250,17 @@ void
 test_utils_check_pixel_rgb (CoglFramebuffer *test_fb,
                             int x, int y, int r, int g, int b)
 {
-  test_utils_check_pixel (test_fb, x, y, (r << 24) | (g << 16) | (b << 8));
+  g_return_if_fail (r >= 0);
+  g_return_if_fail (g >= 0);
+  g_return_if_fail (b >= 0);
+  g_return_if_fail (r <= 0xFF);
+  g_return_if_fail (g <= 0xFF);
+  g_return_if_fail (b <= 0xFF);
+
+  test_utils_check_pixel (test_fb, x, y,
+                          (((guint32) r) << 24) |
+                          (((guint32) g) << 16) |
+                          (((guint32) b) << 8));
 }
 
 void
diff --git a/cogl/tests/conform/test-atlas-migration.c b/cogl/tests/conform/test-atlas-migration.c
index b5d04c3d32..877fb37d6d 100644
--- a/cogl/tests/conform/test-atlas-migration.c
+++ b/cogl/tests/conform/test-atlas-migration.c
@@ -99,9 +99,9 @@ verify_texture (CoglTexture *texture, int size)
             };
 
           test_utils_compare_pixel (p,
-                                    (real_color.red << 24) |
-                                    (real_color.green << 16) |
-                                    (real_color.blue << 8) |
+                                    (((guint32) real_color.red) << 24) |
+                                    (((guint32) real_color.green) << 16) |
+                                    (((guint32) real_color.blue) << 8) |
                                     opacity);
           g_assert_cmpint (p[3], ==, opacity);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]