Skip to contentSkip to conversation

block/curl: Fix Resource Leak in curl_header_cb

PATCH In review Trieu Huynh 2 participants 3 messages Mar 12 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/curl: Fix Resource Leak in curl_header_cb

From: "trieu2.huynh" <[email protected]>

The function curl_header_cb uses g_autofree with g_strstrip(g_strndup(...)). However, g_strstrip may return a pointer that is an offset from the original allocated memory, causing g_autofree to attempt to free an invalid pointer or leak the original.

Separate the allocation and the stripping to ensure the original pointer is correctly tracked and freed.

Resolves: CID 1645633

---
 block/curl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/curl.c +2 -1
index 66aecfb20e..5b66c80704 100644
@@ -208,7 +208,8 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
 {
     BDRVCURLState *s = opaque;
     size_t realsize = size * nmemb;
-    g_autofree char *header = g_strstrip(g_strndup(ptr, realsize));
+    g_autofree char *header = g_strndup(ptr, realsize);
+    g_strstrip(header);
     char *val = strchr(header, ':');
 
     if (!val) {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 7 lines · Trieu Huynh

From: "trieu2.huynh" <[email protected]>

The function curl_header_cb uses g_autofree with g_strstrip(g_strndup(...)). However, g_strstrip may return a pointer that is an offset from the original allocated memory, causing g_autofree to attempt to free an invalid pointer or leak the original.

I don't believe this is correct. g_strstrip() will always return the string argument it is passed. (The glib documentation for g_strstrip() doesn't say so explicitly, but it is a macro for g_strchomp(g_strchug(string)), and both those functions say that they return the input argmuent.)

Quoted 15 lines

Separate the allocation and the stripping to ensure the original pointer is correctly tracked and freed.

Resolves: CID 1645633

---
 block/curl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/curl.c +0 -0
index 66aecfb20e..5b66c80704 100644

block/curl.c static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) · line 212

Quoted 6 lines
block/curl.c +2 -1
@@ -208,7 +208,8 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
 {
     BDRVCURLState *s = opaque;
     size_t realsize = size * nmemb;
-    g_autofree char *header = g_strstrip(g_strndup(ptr, realsize));
+    g_autofree char *header = g_strndup(ptr, realsize);
+    g_strstrip(header);

Being able to rewrite the code like this confirms that we don't actually have a leak -- we are still relying here on g_strstrip(X) == X, just in a different way.

block/curl.c static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) · line 215

Quoted 3 lines
block/curl.c +0 -0
     char *val = strchr(header, ':');
 
     if (!val) {

This looks like a Coverity false positive to me, so I've marked it that way in the Coverity Scan UI.

thanks
-- PMM

#2 lore raw

ack.

BRs,

Vào Thứ 5, 12 thg 3, 2026 vào lúc 16:34 Peter Maydell < [email protected]> đã viết:

Quoted 53 lines · Trieu Huynh
Quoted 4 lines

From: "trieu2.huynh" <[email protected]>

The function curl_header_cb uses g_autofree with

g_strstrip(g_strndup(...)).

Quoted 3 lines

However, g_strstrip may return a pointer that is an offset from the original allocated memory, causing g_autofree to attempt to free an invalid pointer or leak the original.

I don't believe this is correct. g_strstrip() will always return the string argument it is passed. (The glib documentation for g_strstrip() doesn't say so explicitly, but it is a macro for g_strchomp(g_strchug(string)), and both those functions say that they return the input argmuent.)

block/curl.c static size_t curl_header_cb(void *ptr, size_t size,

Quoted 15 lines

Separate the allocation and the stripping to ensure the original pointer is correctly tracked and freed.

Resolves: CID 1645633

---
 block/curl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/curl.c +0 -0
index 66aecfb20e..5b66c80704 100644
@@ -208,7 +208,8 @@ static size_t curl_header_cb(void *ptr, size_t size,

size_t nmemb, void *opaque)

block/curl.c static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) · line 212

Quoted 6 lines
block/curl.c +2 -1
 {
     BDRVCURLState *s = opaque;
     size_t realsize = size * nmemb;
-    g_autofree char *header = g_strstrip(g_strndup(ptr, realsize));
+    g_autofree char *header = g_strndup(ptr, realsize);
+    g_strstrip(header);

Being able to rewrite the code like this confirms that we don't actually have a leak -- we are still relying here on g_strstrip(X) == X, just in a different way.

block/curl.c static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) · line 215

Quoted 3 lines
block/curl.c +0 -0
     char *val = strchr(header, ':');
 
     if (!val) {

This looks like a Coverity false positive to me, so I've marked it that way in the Coverity Scan UI.

thanks
-- PMM