diff mbox series

[02/17] target/avr: Fix buffer read in avr_print_insn

Message ID 20250323173730.3213964-3-richard.henderson@linaro.org
State Superseded
Headers show
Series target/avr: Increase page size | expand

Commit Message

Richard Henderson March 23, 2025, 5:37 p.m. UTC
Do not unconditionally attempt to read 4 bytes, as there
may only be 2 bytes remaining in the translator cache.

Cc: qemu-stable@nongnu.org
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 target/avr/disas.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

Comments

Pierrick Bouvier March 25, 2025, 12:52 a.m. UTC | #1
On 3/23/25 10:37, Richard Henderson wrote:
> Do not unconditionally attempt to read 4 bytes, as there
> may only be 2 bytes remaining in the translator cache.
> 
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   target/avr/disas.c | 21 ++++++++++++++-------
>   1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/target/avr/disas.c b/target/avr/disas.c
> index b7689e8d7c..d341030174 100644
> --- a/target/avr/disas.c
> +++ b/target/avr/disas.c
> @@ -68,28 +68,35 @@ static bool decode_insn(DisasContext *ctx, uint16_t insn);
>   
>   int avr_print_insn(bfd_vma addr, disassemble_info *info)
>   {
> -    DisasContext ctx;
> +    DisasContext ctx = { info };
>       DisasContext *pctx = &ctx;
>       bfd_byte buffer[4];
>       uint16_t insn;
>       int status;
>   
> -    ctx.info = info;
> -
> -    status = info->read_memory_func(addr, buffer, 4, info);
> +    status = info->read_memory_func(addr, buffer, 2, info);
>       if (status != 0) {
>           info->memory_error_func(status, addr, info);
>           return -1;
>       }
>       insn = bfd_getl16(buffer);
> -    ctx.next_word = bfd_getl16(buffer + 2);
> -    ctx.next_word_used = false;
> +
> +    status = info->read_memory_func(addr + 2, buffer + 2, 2, info);
> +    if (status == 0) {
> +        ctx.next_word = bfd_getl16(buffer + 2);
> +    }
>   
>       if (!decode_insn(&ctx, insn)) {
>           output(".db", "0x%02x, 0x%02x", buffer[0], buffer[1]);
>       }
>   
> -    return ctx.next_word_used ? 4 : 2;
> +    if (!ctx.next_word_used) {
> +        return 2;
> +    } else if (status == 0) {
> +        return 4;
> +    }
> +    info->memory_error_func(status, addr + 2, info);
> +    return -1;
>   }
>   
>   

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
diff mbox series

Patch

diff --git a/target/avr/disas.c b/target/avr/disas.c
index b7689e8d7c..d341030174 100644
--- a/target/avr/disas.c
+++ b/target/avr/disas.c
@@ -68,28 +68,35 @@  static bool decode_insn(DisasContext *ctx, uint16_t insn);
 
 int avr_print_insn(bfd_vma addr, disassemble_info *info)
 {
-    DisasContext ctx;
+    DisasContext ctx = { info };
     DisasContext *pctx = &ctx;
     bfd_byte buffer[4];
     uint16_t insn;
     int status;
 
-    ctx.info = info;
-
-    status = info->read_memory_func(addr, buffer, 4, info);
+    status = info->read_memory_func(addr, buffer, 2, info);
     if (status != 0) {
         info->memory_error_func(status, addr, info);
         return -1;
     }
     insn = bfd_getl16(buffer);
-    ctx.next_word = bfd_getl16(buffer + 2);
-    ctx.next_word_used = false;
+
+    status = info->read_memory_func(addr + 2, buffer + 2, 2, info);
+    if (status == 0) {
+        ctx.next_word = bfd_getl16(buffer + 2);
+    }
 
     if (!decode_insn(&ctx, insn)) {
         output(".db", "0x%02x, 0x%02x", buffer[0], buffer[1]);
     }
 
-    return ctx.next_word_used ? 4 : 2;
+    if (!ctx.next_word_used) {
+        return 2;
+    } else if (status == 0) {
+        return 4;
+    }
+    info->memory_error_func(status, addr + 2, info);
+    return -1;
 }