qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] softmmu/memory: use memcpy for multi-byte accesses
@ 2023-11-14 20:55 Patrick Venture
  2023-11-14 21:18 ` Richard Henderson
  2023-11-15 10:34 ` Peter Maydell
  0 siblings, 2 replies; 11+ messages in thread
From: Patrick Venture @ 2023-11-14 20:55 UTC (permalink / raw)
  To: pbonzini, peterx, david, philmd, peter.maydell
  Cc: qemu-devel, Patrick Venture, Chris Rauer, Peter Foley

Avoids unaligned pointer issues.

Reviewed-by: Chris Rauer <crauer@google.com>
Reviewed-by: Peter Foley <pefoley@google.com>
Signed-off-by: Patrick Venture <venture@google.com>
---
 system/memory.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/system/memory.c b/system/memory.c
index 304fa843ea..02c97d5187 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -1343,16 +1343,16 @@ static uint64_t memory_region_ram_device_read(void *opaque,
 
     switch (size) {
     case 1:
-        data = *(uint8_t *)(mr->ram_block->host + addr);
+        memcpy(&data, mr->ram_block->host + addr, sizeof(uint8_t));
         break;
     case 2:
-        data = *(uint16_t *)(mr->ram_block->host + addr);
+        memcpy(&data, mr->ram_block->host + addr, sizeof(uint16_t));
         break;
     case 4:
-        data = *(uint32_t *)(mr->ram_block->host + addr);
+        memcpy(&data, mr->ram_block->host + addr, sizeof(uint32_t));
         break;
     case 8:
-        data = *(uint64_t *)(mr->ram_block->host + addr);
+        memcpy(&data, mr->ram_block->host + addr, sizeof(uint64_t));
         break;
     }
 
@@ -1370,16 +1370,16 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
 
     switch (size) {
     case 1:
-        *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
+        memcpy(mr->ram_block->host + addr, &data, sizeof(uint8_t));
         break;
     case 2:
-        *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
+        memcpy(mr->ram_block->host + addr, &data, sizeof(uint16_t));
         break;
     case 4:
-        *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
+        memcpy(mr->ram_block->host + addr, &data, sizeof(uint32_t));
         break;
     case 8:
-        *(uint64_t *)(mr->ram_block->host + addr) = data;
+        memcpy(mr->ram_block->host + addr, &data, sizeof(uint64_t));
         break;
     }
 }
-- 
2.43.0.rc0.421.g78406f8d94-goog



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-11-16 11:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-14 20:55 [PATCH] softmmu/memory: use memcpy for multi-byte accesses Patrick Venture
2023-11-14 21:18 ` Richard Henderson
2023-11-14 21:39   ` Patrick Venture
2023-11-15 10:30   ` Peter Maydell
2023-11-15 16:57     ` Patrick Venture
2023-11-15 10:34 ` Peter Maydell
2023-11-15 16:58   ` Patrick Venture
2023-11-15 17:02     ` Richard Henderson
2023-11-15 17:26       ` Patrick Venture
2023-11-15 17:26         ` Patrick Venture
2023-11-16 11:30         ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).