From 57652e6429cf6eac8e8bedb265cd6dee6ec56021 Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Wed, 28 May 2025 17:20:15 +0200 Subject: [PATCH 1/2] Potential fix for code scanning alert no. 2: Use of string after lifetime ends Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- src/update_engine/test_http_server.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/update_engine/test_http_server.cc b/src/update_engine/test_http_server.cc index a573811..95bad13 100644 --- a/src/update_engine/test_http_server.cc +++ b/src/update_engine/test_http_server.cc @@ -466,7 +466,8 @@ class UrlTerms { return terms[index]; } inline const char *GetCStr(const off_t index) const { - return Get(index).c_str(); + const string& term = Get(index); + return term.c_str(); } inline int GetInt(const off_t index) const { return atoi(GetCStr(index)); From 547c09aef615232eb7319765d6cb93b721c1840d Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Tue, 1 Jul 2025 12:38:48 +0200 Subject: [PATCH 2/2] test_http_server: Fix dangling pointer issue Get(index) returns a copy of a string. That copy is kept alive by the const reference until the end of the GetCStr function. When the GetCStr function returns, the copy is destroyed (and so is its char buffer), so returned pointer to the char buffer becomes dangling. Suggested-by: Krzesimir Nowak --- src/update_engine/test_http_server.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/update_engine/test_http_server.cc b/src/update_engine/test_http_server.cc index 95bad13..68c6dae 100644 --- a/src/update_engine/test_http_server.cc +++ b/src/update_engine/test_http_server.cc @@ -466,8 +466,7 @@ class UrlTerms { return terms[index]; } inline const char *GetCStr(const off_t index) const { - const string& term = Get(index); - return term.c_str(); + return terms[index].c_str(); } inline int GetInt(const off_t index) const { return atoi(GetCStr(index));