From 17dfb69882df950fbda3b709f8b6ca9310a0799f Mon Sep 17 00:00:00 2001
From: ShubhamDesai <42180509+ShubhamDesai@users.noreply.github.com>
Date: Mon, 9 Dec 2024 18:51:09 -0500
Subject: [PATCH] lib/raster: Fix Resource Leak issue in put_title.c (#4821)

---
 lib/raster/put_title.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/raster/put_title.c b/lib/raster/put_title.c
index 8d0d4f0b43f..346bf12f23d 100644
--- a/lib/raster/put_title.c
+++ b/lib/raster/put_title.c
@@ -35,6 +35,7 @@ int Rast_put_cell_title(const char *name, const char *title)
     if (!out) {
         fclose(in);
         G_warning(_("G_put_title - can't create a temp file"));
+        G_free(tempfile);
         return -1;
     }
 
@@ -52,12 +53,15 @@ int Rast_put_cell_title(const char *name, const char *title)
     if (line < 3) {
         G_warning(_("category information for [%s] in [%s] invalid"), name,
                   mapset);
+        remove(tempfile);
+        G_free(tempfile);
         return -1;
     }
 
     in = fopen(tempfile, "r");
     if (!in) {
         G_warning(_("G_put_title - can't reopen temp file"));
+        G_free(tempfile);
         return -1;
     }
 
@@ -66,6 +70,8 @@ int Rast_put_cell_title(const char *name, const char *title)
         fclose(in);
         G_warning(_("can't write category information for [%s] in [%s]"), name,
                   mapset);
+        remove(tempfile);
+        G_free(tempfile);
         return -1;
     }
 
@@ -75,6 +81,7 @@ int Rast_put_cell_title(const char *name, const char *title)
     fclose(in);
     fclose(out);
     remove(tempfile);
+    G_free(tempfile);
 
     return 1;
 }