[Spice-devel] [PATCH spice-server 09/10] Avoid some alignment warnings using clang

Frediano Ziglio fziglio at redhat.com
Mon Jan 22 17:55:01 UTC 2018


clang reports may warnings like:

test-display-base.c:252:11: error: cast from 'uint8_t *' (aka 'unsigned char *') to 'uint32_t *' (aka 'unsigned int *') increases required alignment from 1 to 4 [-Werror,-Wcast-align]
    dst = (uint32_t *)bitmap;
          ^~~~~~~~~~~~~~~~~~

Use SPICE_ALIGNED_CAST/SPICE_UNALIGNED_CAST macros in common/mem.h to
mark the cast safe or possibly unsafe.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/lz4-encoder.c                  |  3 ++-
 server/tests/test-display-base.c      |  2 +-
 server/tests/test-display-streaming.c | 10 ++++++----
 server/tests/test-gst.c               |  4 ++--
 4 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/server/lz4-encoder.c b/server/lz4-encoder.c
index a8f0d17c..8657e8d9 100644
--- a/server/lz4-encoder.c
+++ b/server/lz4-encoder.c
@@ -89,7 +89,8 @@ int lz4_encode(Lz4EncoderContext *lz4, int height, int stride, uint8_t *io_ptr,
             LZ4_freeStream(stream);
             return 0;
         }
-        *((uint32_t *)compressed_lines) = GUINT32_TO_BE(enc_size);
+        // compressed_lines is returned by malloc so is surely aligned
+        *SPICE_ALIGNED_CAST(uint32_t *, compressed_lines) = GUINT32_TO_BE(enc_size);
 
         out_size += enc_size += 4;
         already_copied = 0;
diff --git a/server/tests/test-display-base.c b/server/tests/test-display-base.c
index a86a8b67..ea3a23ba 100644
--- a/server/tests/test-display-base.c
+++ b/server/tests/test-display-base.c
@@ -249,7 +249,7 @@ static SimpleSpiceUpdate *test_spice_create_update_solid(uint32_t surface_id, QX
     bh = bbox.bottom - bbox.top;
 
     bitmap = g_malloc(bw * bh * 4);
-    dst = (uint32_t *)bitmap;
+    dst = SPICE_ALIGNED_CAST(uint32_t *, bitmap);
 
     for (i = 0 ; i < bh * bw ; ++i, ++dst) {
         *dst = color;
diff --git a/server/tests/test-display-streaming.c b/server/tests/test-display-streaming.c
index 1cd3f6e5..6ab70db9 100644
--- a/server/tests/test-display-streaming.c
+++ b/server/tests/test-display-streaming.c
@@ -32,6 +32,8 @@
 
 #include <glib.h>
 
+#include <common/mem.h>
+
 #include "test-display-base.h"
 
 static int sized;
@@ -51,7 +53,7 @@ static void create_overlay(Command *command , int width, int height)
 
     cmd->num_clip_rects = 0;
     cmd->bitmap = g_malloc(width * height * 4 );
-    dst = (uint32_t *)cmd->bitmap;
+    dst = SPICE_ALIGNED_CAST(uint32_t *, cmd->bitmap);
     for (int i = 0; i < width * height; i++, dst++) {
         *dst = 0x8B008B;
     }
@@ -123,7 +125,7 @@ static void create_clipped_frame(Test *test, Command *command, int clipping_fact
 
     cmd->bitmap = g_malloc(width*height*4);
     memset(cmd->bitmap, 0xff, width*height*4);
-    dst = (uint32_t *)(cmd->bitmap + cur_line*width*4);
+    dst = SPICE_ALIGNED_CAST(uint32_t *, cmd->bitmap + cur_line*width*4);
     for (; cur_line < end_line; cur_line++) {
         int col;
         for (col = 0; col < width; col++, dst++) {
@@ -134,13 +136,13 @@ static void create_clipped_frame(Test *test, Command *command, int clipping_fact
         int i;
         uint32_t color = 0xffffff & rand();
 
-        dst = (uint32_t *)cmd->bitmap;
+        dst = SPICE_ALIGNED_CAST(uint32_t *, cmd->bitmap);
 
         for (i = 0; i < 50*width; i++, dst++) {
             *dst = color;
         }
 
-        dst = ((uint32_t *)(cmd->bitmap + (height - 50)*4*width));
+        dst = SPICE_ALIGNED_CAST(uint32_t *, cmd->bitmap + (height - 50)*4*width);
 
         for (i = 0; i < 50*width; i++, dst++) {
             *dst = color;
diff --git a/server/tests/test-gst.c b/server/tests/test-gst.c
index 91ef92d6..43ee4004 100644
--- a/server/tests/test-gst.c
+++ b/server/tests/test-gst.c
@@ -905,7 +905,7 @@ get_convert_line(SpiceBitmapFmt format)
 static void
 convert_line16(uint8_t *dest, const uint8_t *src, uint32_t width)
 {
-    uint16_t *dest16 = (uint16_t *) dest;
+    uint16_t *dest16 = SPICE_ALIGNED_CAST(uint16_t *, dest);
     for (; width; --width) {
         *dest16++ = (src[0] >> 3) | ((src[1] & 0xf8) << 2) | ((src[2] & 0xf8) << 7);
         src += 4;
@@ -1055,7 +1055,7 @@ bitmap_extract32(SpiceBitmap *bitmap, uint8_t *buf, int32_t x, int32_t y, int32_
 static uint8_t *
 bitmap_extract16(SpiceBitmap *bitmap, uint8_t *buf, int32_t x, int32_t y, int32_t w)
 {
-    const uint16_t *line = (const uint16_t *)(bitmap_get_line(bitmap, y) + x * 2);
+    const uint16_t *line = SPICE_ALIGNED_CAST(const uint16_t *, bitmap_get_line(bitmap, y) + x * 2);
     uint8_t *dest = buf;
     for (; w; --w) {
         uint16_t pixel = *line++;
-- 
2.14.3



More information about the Spice-devel mailing list