[Spice-devel] [spice-common PATCH v2 04/10] fix warnings about memory alignment

Victor Toso victortoso at redhat.com
Fri Aug 14 09:24:25 PDT 2015


- By either using SPICE_ALIGNED_CAST to false positives and
SPICE_UNALIGNED_CAST to the cast that could lead to problems; or
- By using a better type to the variable;
---
 common/canvas_base.c | 16 +++++++++-------
 common/sw_canvas.c   | 10 ++++++----
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/common/canvas_base.c b/common/canvas_base.c
index 6f48340..ad1d25a 100644
--- a/common/canvas_base.c
+++ b/common/canvas_base.c
@@ -396,7 +396,7 @@ static pixman_image_t *canvas_get_quic(CanvasBase *canvas, SpiceImage *image,
     quic_data->current_chunk = 0;
 
     if (quic_decode_begin(quic_data->quic,
-                          (uint32_t *)image->u.quic.data->chunk[0].data,
+                          SPICE_ALIGNED_CAST(uint32_t, image->u.quic.data->chunk[0].data),
                           image->u.quic.data->chunk[0].len >> 2,
                           &type, &width, &height) == QUIC_ERROR) {
         spice_warning("quic decode begin failed");
@@ -583,7 +583,7 @@ static pixman_image_t *canvas_get_lz4(CanvasBase *canvas, SpiceImage *image)
 
     do {
         // Read next compressed block
-        enc_size = ntohl(*((uint32_t *)data));
+        enc_size = ntohl(*SPICE_ALIGNED_CAST(uint32_t, data));
         data += 4;
         dec_size = LZ4_decompress_safe_continue(stream, (const char *) data,
                                                 (char *) dest, enc_size, available);
@@ -606,9 +606,9 @@ static pixman_image_t *canvas_get_lz4(CanvasBase *canvas, SpiceImage *image)
             dest -= (stride_abs * (height - 1));
         }
         for (row = height - 1; row > 0; --row) {
-            uint32_t *dest_aligned, *dest_misaligned;
-            dest_aligned = (uint32_t *)(dest + stride_abs*row);
-            dest_misaligned = (uint32_t*)(dest + stride_encoded*row);
+            void *dest_aligned, *dest_misaligned;
+            dest_aligned = (dest + stride_abs*row);
+            dest_misaligned = (dest + stride_encoded*row);
             memmove(dest_aligned, dest_misaligned, stride_encoded);
         }
     }
@@ -1919,7 +1919,8 @@ static int quic_usr_more_space(QuicUsrContext *usr, uint32_t **io_ptr, int rows_
     }
     quic_data->current_chunk++;
 
-    *io_ptr = (uint32_t *)quic_data->chunks->chunk[quic_data->current_chunk].data;
+    *io_ptr = SPICE_ALIGNED_CAST(uint32_t,
+            quic_data->chunks->chunk[quic_data->current_chunk].data);
     return quic_data->chunks->chunk[quic_data->current_chunk].len >> 2;
 }
 
@@ -2066,7 +2067,8 @@ static void canvas_mask_pixman(CanvasBase *canvas,
     /* round down X to even 32 pixels (i.e. uint32_t) */
     extents.x1 = extents.x1 & ~(0x1f);
 
-    mask_data = (uint32_t *)((uint8_t *)mask_data + mask_stride * extents.y1 + extents.x1 / 32);
+    mask_data = SPICE_UNALIGNED_CAST(uint32_t,
+            ((uint8_t *)mask_data + mask_stride * extents.y1 + extents.x1 / 32));
     mask_x -= extents.x1;
     mask_y -= extents.y1;
     mask_width = extents.x2 - extents.x1;
diff --git a/common/sw_canvas.c b/common/sw_canvas.c
index 7d67ca5..1892614 100644
--- a/common/sw_canvas.c
+++ b/common/sw_canvas.c
@@ -22,6 +22,7 @@
 #include <config.h>
 #endif
 
+#include <spice/macros.h>
 #include <math.h>
 #include "sw_canvas.h"
 #define CANVAS_USE_PIXMAN
@@ -357,8 +358,8 @@ static void clear_dest_alpha(pixman_image_t *dest,
     }
 
     stride = pixman_image_get_stride(dest);
-    data = (uint32_t *) (
-        (uint8_t *)pixman_image_get_data(dest) + y * stride + 4 * x);
+    data = SPICE_ALIGNED_CAST(uint32_t,
+            ((uint8_t *)pixman_image_get_data(dest) + y * stride + 4 * x));
 
     if ((*data & 0xff000000U) == 0xff000000U) {
         spice_pixman_fill_rect_rop(dest,
@@ -1012,7 +1013,7 @@ static void canvas_put_image(SpiceCanvas *spice_canvas,
     src = pixman_image_create_bits(PIXMAN_x8r8g8b8,
                                    src_width,
                                    src_height,
-                                   (uint32_t*)src_data,
+                                   SPICE_ALIGNED_CAST(uint32_t, src_data),
                                    src_stride);
 
 
@@ -1276,7 +1277,8 @@ SpiceCanvas *canvas_create_for_data(int width, int height, uint32_t format,
     pixman_image_t *image;
 
     image = pixman_image_create_bits(spice_surface_format_to_pixman (format),
-                                     width, height, (uint32_t *)data, stride);
+                                     width, height,
+                                     SPICE_ALIGNED_CAST(uint32_t, data), stride);
 
     return canvas_create_common(image, format
                                 , bits_cache
-- 
2.4.3



More information about the Spice-devel mailing list