diff mbox series

Hexagon (disas/hexagon.c) fix memory leak for early exit cases

Message ID 1628773760-870-1-git-send-email-tsimpson@quicinc.com
State New
Headers show
Series Hexagon (disas/hexagon.c) fix memory leak for early exit cases | expand

Commit Message

Taylor Simpson Aug. 12, 2021, 1:09 p.m. UTC
Don't allocate the string until error conditions have been checked

Fixes: a00cfed0e ("Hexagon (disas) disassembler")
Eliminate Coverity CID 1460121 (Resource leak)

Signed-off-by: Taylor Simpson <tsimpson@quicinc.com>
---
 disas/hexagon.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Peter Maydell Aug. 12, 2021, 1:10 p.m. UTC | #1
On Thu, 12 Aug 2021 at 14:09, Taylor Simpson <tsimpson@quicinc.com> wrote:
>
> Don't allocate the string until error conditions have been checked
>
> Fixes: a00cfed0e ("Hexagon (disas) disassembler")
> Eliminate Coverity CID 1460121 (Resource leak)
>
> Signed-off-by: Taylor Simpson <tsimpson@quicinc.com>
> ---
>  disas/hexagon.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/disas/hexagon.c b/disas/hexagon.c
> index 3c24e2a..c1a4ffc 100644
> --- a/disas/hexagon.c
> +++ b/disas/hexagon.c
> @@ -33,7 +33,7 @@ int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
>  {
>      uint32_t words[PACKET_WORDS_MAX];
>      bool found_end = false;
> -    GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
> +    GString *buf;
>      int i, len;
>
>      for (i = 0; i < PACKET_WORDS_MAX && !found_end; i++) {
> @@ -57,6 +57,7 @@ int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
>          return PACKET_WORDS_MAX * sizeof(uint32_t);
>      }
>
> +    buf = g_string_sized_new(PACKET_BUFFER_LEN);
>      len = disassemble_hexagon(words, i, memaddr, buf);
>      (*info->fprintf_func)(info->stream, "%s", buf->str);
>      g_string_free(buf, true);
> --

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM
Philippe Mathieu-Daudé Aug. 12, 2021, 1:16 p.m. UTC | #2
On 8/12/21 3:09 PM, Taylor Simpson wrote:
> Don't allocate the string until error conditions have been checked
> 
> Fixes: a00cfed0e ("Hexagon (disas) disassembler")
> Eliminate Coverity CID 1460121 (Resource leak)
> 
> Signed-off-by: Taylor Simpson <tsimpson@quicinc.com>
> ---
>  disas/hexagon.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
diff mbox series

Patch

diff --git a/disas/hexagon.c b/disas/hexagon.c
index 3c24e2a..c1a4ffc 100644
--- a/disas/hexagon.c
+++ b/disas/hexagon.c
@@ -33,7 +33,7 @@  int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
 {
     uint32_t words[PACKET_WORDS_MAX];
     bool found_end = false;
-    GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
+    GString *buf;
     int i, len;
 
     for (i = 0; i < PACKET_WORDS_MAX && !found_end; i++) {
@@ -57,6 +57,7 @@  int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
         return PACKET_WORDS_MAX * sizeof(uint32_t);
     }
 
+    buf = g_string_sized_new(PACKET_BUFFER_LEN);
     len = disassemble_hexagon(words, i, memaddr, buf);
     (*info->fprintf_func)(info->stream, "%s", buf->str);
     g_string_free(buf, true);