[2/3] rust: Fix memory leak in compile_{integer,float}_literal

Message ID 20230402150515.40826-3-rep.dot.nop@gmail.com
State Accepted
Headers
Series [1/3] go: Fix memory leak in Integer_expression |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Bernhard Reutner-Fischer April 2, 2023, 3:05 p.m. UTC
  From: Bernhard Reutner-Fischer <aldot@gcc.gnu.org>

Cc: Arthur Cohen <arthur.cohen@embecosm.com>
Cc: Philip Herron <herron.philip@googlemail.com>

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::compile_integer_literal):
	(CompileExpr::compile_float_literal): Fix memory leak.
---
 gcc/rust/backend/rust-compile-expr.cc | 7 +++++++
 1 file changed, 7 insertions(+)
  

Patch

diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 436fc924a13..82078b81953 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -2119,6 +2119,7 @@  CompileExpr::compile_integer_literal (const HIR::LiteralExpr &expr,
   if (mpz_init_set_str (ival, literal_value.as_string ().c_str (), 10) != 0)
     {
       rust_error_at (expr.get_locus (), "bad number in literal");
+      mpz_clear (ival);
       return error_mark_node;
     }
 
@@ -2133,6 +2134,9 @@  CompileExpr::compile_integer_literal (const HIR::LiteralExpr &expr,
       rust_error_at (expr.get_locus (),
 		     "integer overflows the respective type %<%s%>",
 		     tyty->get_name ().c_str ());
+      mpz_clear (type_min);
+      mpz_clear (type_max);
+      mpz_clear (ival);
       return error_mark_node;
     }
 
@@ -2158,6 +2162,7 @@  CompileExpr::compile_float_literal (const HIR::LiteralExpr &expr,
       != 0)
     {
       rust_error_at (expr.get_locus (), "bad number in literal");
+      mpfr_clear (fval);
       return error_mark_node;
     }
 
@@ -2179,9 +2184,11 @@  CompileExpr::compile_float_literal (const HIR::LiteralExpr &expr,
       rust_error_at (expr.get_locus (),
 		     "decimal overflows the respective type %<%s%>",
 		     tyty->get_name ().c_str ());
+      mpfr_clear (fval);
       return error_mark_node;
     }
 
+  mpfr_clear (fval);
   return real_value;
 }