Skip to contentSkip to conversation

target/i386: Fix memory leak in sev_read_file_base64()

PATCH Applied Peter Maydell 3 participants 4 messages Aug 20, 2021 – Sep 20, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386: Fix memory leak in sev_read_file_base64()

In sev_read_file_base64() we call g_file_get_contents(), which allocates memory for the file contents. We then base64-decode the contents (which allocates another buffer for the decoded data), but forgot to free the memory for the original file data.

Use g_autofree to ensure that the file data is freed.

Fixes Coverity CID 1459997

---
Tested with 'make/make check' only...

 target/i386/sev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/sev.c +1 -1
index 83df8c09f6a..1e7833da1ab 100644
@@ -565,7 +565,7 @@ static int
 sev_read_file_base64(const char *filename, guchar **data, gsize *len)
 {
     gsize sz;
-    gchar *base64;
+    g_autofree gchar *base64 = NULL;
     GError *error = NULL;
 
     if (!g_file_get_contents(filename, &base64, &sz, &error)) {
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Peter Maydell

In sev_read_file_base64() we call g_file_get_contents(), which allocates memory for the file contents. We then base64-decode the contents (which allocates another buffer for the decoded data), but forgot to free the memory for the original file data.

Use g_autofree to ensure that the file data is freed.

Quoted 7 lines
Fixes Coverity CID 1459997

---
Tested with 'make/make check' only...

 target/i386/sev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Peter Maydell

In sev_read_file_base64() we call g_file_get_contents(), which allocates memory for the file contents. We then base64-decode the contents (which allocates another buffer for the decoded data), but forgot to free the memory for the original file data.

Use g_autofree to ensure that the file data is freed.

Fixes Coverity CID 1459997

---
Tested with 'make/make check' only...

Quoted 5 lines
Quoted 2 lines
  target/i386/sev.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Queued, thanks.

Paolo

#3 lore raw
In reply to Peter Maydell

Ping?

thanks
-- PMM

Quoted 32 lines · Peter Maydell

In sev_read_file_base64() we call g_file_get_contents(), which allocates memory for the file contents. We then base64-decode the contents (which allocates another buffer for the decoded data), but forgot to free the memory for the original file data.

Use g_autofree to ensure that the file data is freed.

Fixes Coverity CID 1459997

---
Tested with 'make/make check' only...

 target/i386/sev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/sev.c +1 -2
index 83df8c09f6a..1e7833da1ab 100644
@@ -565,7 +565,7 @@ static int
 sev_read_file_base64(const char *filename, guchar **data, gsize *len)
 {
     gsize sz;
-    gchar *base64;
+    g_autofree gchar *base64 = NULL;
     GError *error = NULL;
 
     if (!g_file_get_contents(filename, &base64, &sz, &error)) {
--

2.20.1