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

Victor Toso victortoso at redhat.com
Fri Aug 14 09:24:31 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;
---
 server/lz4_encoder.c   | 2 +-
 server/red_parse_qxl.c | 4 ++--
 server/red_worker.c    | 4 ++--
 server/reds.c          | 8 ++++----
 server/reds_stream.c   | 2 +-
 server/snd_worker.c    | 6 +++---
 6 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/server/lz4_encoder.c b/server/lz4_encoder.c
index b3e38b2..1fbdd93 100644
--- a/server/lz4_encoder.c
+++ b/server/lz4_encoder.c
@@ -86,7 +86,7 @@ int lz4_encode(Lz4EncoderContext *lz4, int height, int stride, uint8_t *io_ptr,
             LZ4_freeStream(stream);
             return 0;
         }
-        *((uint32_t *)compressed_lines) = htonl(enc_size);
+        *SPICE_ALIGNED_CAST(uint32_t, compressed_lines) = htonl(enc_size);
 
         out_size += enc_size += 4;
         already_copied = 0;
diff --git a/server/red_parse_qxl.c b/server/red_parse_qxl.c
index 5b1befa..74ff99a 100644
--- a/server/red_parse_qxl.c
+++ b/server/red_parse_qxl.c
@@ -839,9 +839,9 @@ static SpiceString *red_get_string(RedMemSlotInfo *slots, int group_id,
         spice_assert((QXLRasterGlyph*)(&start->data[glyph_size]) <= end);
         memcpy(glyph->data, start->data, glyph_size);
         start = (QXLRasterGlyph*)(&start->data[glyph_size]);
-        glyph = (SpiceRasterGlyph*)
+        glyph = SPICE_ALIGNED_CAST(SpiceRasterGlyph,
             (((uint8_t *)glyph) +
-             SPICE_ALIGN(sizeof(SpiceRasterGlyph) + glyph_size, 4));
+             SPICE_ALIGN(sizeof(SpiceRasterGlyph) + glyph_size, 4)));
     }
 
     if (free_data) {
diff --git a/server/red_worker.c b/server/red_worker.c
index dd731f5..e020ea0 100644
--- a/server/red_worker.c
+++ b/server/red_worker.c
@@ -3967,7 +3967,7 @@ static int rgb32_data_has_alpha(int width, int height, size_t stride,
 
     has_alpha = FALSE;
     while (height-- > 0) {
-        line = (uint32_t *)data;
+        line = SPICE_ALIGNED_CAST(uint32_t, data);
         end = line + width;
         data += stride;
         while (line != end) {
@@ -5998,7 +5998,7 @@ static BitmapGradualType _get_bitmap_graduality_level(RedWorker *worker, SpiceBi
         x = bitmap->x;
         switch (bitmap->format) {
         case SPICE_BITMAP_FMT_16BIT:
-            compute_lines_gradual_score_rgb16((rgb16_pixel_t *)chunk[i].data, x, num_lines,
+            compute_lines_gradual_score_rgb16(SPICE_ALIGNED_CAST(rgb16_pixel_t, chunk[i].data), x, num_lines,
                                               &chunk_score, &chunk_num_samples);
             break;
         case SPICE_BITMAP_FMT_24BIT:
diff --git a/server/reds.c b/server/reds.c
index c07cb6c..5e889f7 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -1338,7 +1338,7 @@ static const uint32_t *red_link_info_get_caps(const RedLinkInfo *link)
 {
     const uint8_t *caps_start = (const uint8_t *)link->link_mess;
 
-    return (const uint32_t *)(caps_start + link->link_mess->caps_offset);
+    return SPICE_ALIGNED_CAST(const uint32_t, (caps_start + link->link_mess->caps_offset));
 }
 
 static bool red_link_info_test_capability(const RedLinkInfo *link, uint32_t cap)
@@ -1628,7 +1628,7 @@ static void reds_handle_main_link(RedLinkInfo *link)
     link->stream = NULL;
     link->link_mess = NULL;
     reds_link_free(link);
-    caps = (uint32_t *)((uint8_t *)link_mess + link_mess->caps_offset);
+    caps = SPICE_ALIGNED_CAST(uint32_t, ((uint8_t *)link_mess + link_mess->caps_offset));
     client = red_client_new(mig_target);
     ring_add(&reds->clients, &client->link);
     reds->num_clients++;
@@ -1709,7 +1709,7 @@ static void reds_channel_do_link(RedChannel *channel, RedClient *client,
     spice_assert(link_msg);
     spice_assert(stream);
 
-    caps = (uint32_t *)((uint8_t *)link_msg + link_msg->caps_offset);
+    caps = SPICE_ALIGNED_CAST(uint32_t, ((uint8_t *)link_msg + link_msg->caps_offset));
     channel->client_cbs.connect(channel, client, stream,
                                 red_client_during_migrate_at_target(client),
                                 link_msg->num_common_caps,
@@ -2108,7 +2108,7 @@ static void reds_handle_read_link_done(void *opaque)
     link_mess->num_common_caps = GUINT32_FROM_LE(link_mess->num_common_caps);
 
     num_caps = link_mess->num_common_caps + link_mess->num_channel_caps;
-    caps = (uint32_t *)((uint8_t *)link_mess + link_mess->caps_offset);
+    caps = SPICE_ALIGNED_CAST(uint32_t, ((uint8_t *)link_mess + link_mess->caps_offset));
 
     if (num_caps && (num_caps * sizeof(uint32_t) + link_mess->caps_offset >
                      link->link_header.size ||
diff --git a/server/reds_stream.c b/server/reds_stream.c
index 1a595b2..1caa40d 100644
--- a/server/reds_stream.c
+++ b/server/reds_stream.c
@@ -326,7 +326,7 @@ RedsStream *reds_stream_new(int socket)
     RedsStream *stream;
 
     stream = spice_malloc0(sizeof(RedsStream) + sizeof(RedsStreamPrivate));
-    stream->priv = (RedsStreamPrivate *)(((char *)stream) + sizeof(RedsStream));
+    stream->priv = SPICE_ALIGNED_CAST(RedsStreamPrivate, ((char *)stream + sizeof(RedsStream)));
     stream->priv->info = spice_new0(SpiceChannelEventInfo, 1);
     reds_stream_set_socket(stream, socket);
 
diff --git a/server/snd_worker.c b/server/snd_worker.c
index b039939..198cb43 100644
--- a/server/snd_worker.c
+++ b/server/snd_worker.c
@@ -303,7 +303,7 @@ static int snd_record_handle_write(RecordChannel *record_channel, size_t size, v
 {
     SpiceMsgcRecordPacket *packet;
     uint32_t write_pos;
-    uint32_t* data;
+    uint8_t* data;
     uint32_t len;
     uint32_t now;
 
@@ -314,7 +314,7 @@ static int snd_record_handle_write(RecordChannel *record_channel, size_t size, v
     packet = (SpiceMsgcRecordPacket *)message;
 
     if (record_channel->mode == SPICE_AUDIO_DATA_MODE_RAW) {
-        data = (uint32_t *)packet->data;
+        data = packet->data;
         size = packet->data_size >> 2;
         size = MIN(size, RECORD_SAMPLES_SIZE);
      } else {
@@ -323,7 +323,7 @@ static int snd_record_handle_write(RecordChannel *record_channel, size_t size, v
         if (snd_codec_decode(record_channel->codec, packet->data, packet->data_size,
                     record_channel->decode_buf, &decode_size) != SND_CODEC_OK)
             return FALSE;
-        data = (uint32_t *) record_channel->decode_buf;
+        data = record_channel->decode_buf;
         size = decode_size >> 2;
     }
 
-- 
2.4.3



More information about the Spice-devel mailing list