Skip to content

Potential fix for code scanning alert no. 2: Use of string after lifetime ends #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2025

Conversation

dongsupark
Copy link
Member

Potential fix for https://github.com/flatcar/update_engine/security/code-scanning/2

To fix the issue, we need to ensure that the lifetime of the std::string object returned by Get(index) is extended so that the pointer returned by c_str() remains valid. The best way to achieve this is to store the std::string object in a local variable within the GetCStr method and return the pointer to its character array. This ensures that the std::string object outlives the call to c_str().

Changes will be made to the GetCStr method in the UrlTerms class. Specifically:

  1. Store the result of Get(index) in a local variable.
  2. Call c_str() on the local variable and return the pointer.

No additional imports or dependencies are required for this fix.


Suggested fixes powered by Copilot Autofix. Review carefully before merging.

@dongsupark
Copy link
Member Author

Now the CI starts to fail when downloading https://github.com/kinvolk/seismograph/archive/flatcar-master.zip, which has nothing to do with this PR. That happens because of the recent branch renaming.

@dongsupark dongsupark force-pushed the fix-string-after-lifetime branch from 0cbb3cb to 6513e95 Compare July 1, 2025 12:35
dongsupark and others added 2 commits July 10, 2025 14:38
…time ends

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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 <knowak@microsoft.com>
@dongsupark dongsupark force-pushed the fix-string-after-lifetime branch from 6513e95 to 547c09a Compare July 10, 2025 12:38
@dongsupark dongsupark merged commit 4ecb5e3 into main Jul 10, 2025
4 checks passed
@dongsupark dongsupark deleted the fix-string-after-lifetime branch July 10, 2025 13:02
dongsupark added a commit to flatcar/scripts that referenced this pull request Jul 10, 2025
dongsupark added a commit to flatcar/scripts that referenced this pull request Jul 16, 2025
Bump coreos-base/update_engine to 2025-07-10.

Pulls in flatcar/update_engine#44,
flatcar/update_engine#45,
flatcar/update_engine#46.

Signed-off-by: Dongsu Park <dongsu@dpark.io>
dongsupark added a commit to flatcar/scripts that referenced this pull request Jul 16, 2025
Bump coreos-base/update_engine to 2025-07-10.

Pulls in flatcar/update_engine#44,
flatcar/update_engine#45,
flatcar/update_engine#46.

Signed-off-by: Dongsu Park <dongsu@dpark.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants