patches and low-level development discussion
 help / color / mirror / code / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH] tools/vm-set-persist.c: fix "memory leak"
Date: Sun, 10 May 2026 18:55:41 +0200	[thread overview]
Message-ID: <20260510165541.32975-1-hi@alyssa.is> (raw)

Newer versions of clang-tidy complain about the "memory leak" from
strdup.  This is not a problem because this is not a long lived
program nor a large allocation, but I think it's probably better to
appease clang-tidy than to override it.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
---
 tools/vm-set-persist.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/tools/vm-set-persist.c b/tools/vm-set-persist.c
index ac759504..8f714b38 100644
--- a/tools/vm-set-persist.c
+++ b/tools/vm-set-persist.c
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is>
+// SPDX-FileCopyrightText: 2025-2026 Alyssa Ross <hi@alyssa.is>
 // SPDX-License-Identifier: EUPL-1.2+
 
 #include <err.h>
@@ -142,11 +142,12 @@ static void do_rename(int mnt, const char dir_name[static 1],
 
 int main(int argc, char *argv[])
 {
-	int mnt;
+	int mnt, r;
 	mode_t mode;
 	uint64_t mnt_id;
 	char *disk_path, *dir_name, *old_name, *new_name,
-	     mnt_root[MNT_ROOT_MAX_LEN], source[SOURCE_MAX_LEN];
+	     mnt_root[MNT_ROOT_MAX_LEN], mnt_root_copy[MNT_ROOT_MAX_LEN],
+	     source[SOURCE_MAX_LEN];
 
 	if (argc != 3) {
 		fprintf(stderr, "Usage: vm-set-persist ID INSTANCE\n");
@@ -168,10 +169,13 @@ int main(int argc, char *argv[])
 	do_statx(disk_path, &mode, &mnt_id);
 	do_statmount(mnt_id, mnt_root, source);
 
-	if (!(dir_name = strdup(mnt_root)))
-		err(EXIT_FAILURE, "strdup");
-	dir_name = dirname(dir_name);
-	old_name = basename(mnt_root);
+	r = snprintf(mnt_root_copy, sizeof mnt_root_copy, "%s", mnt_root);
+	if (r == -1)
+		err(EXIT_FAILURE, "snprintf");
+	if ((size_t)r >= sizeof mnt_root_copy)
+		errx(EXIT_FAILURE, "mnt_root longer than mnt_root_copy");
+	dir_name = dirname(mnt_root);
+	old_name = basename(mnt_root_copy);
 
 	mnt = do_mount(source);
 

base-commit: 508851e5d884bc3f2906947dcd43b055dc0465f4
-- 
2.53.0


             reply	other threads:[~2026-05-10 16:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-10 16:55 Alyssa Ross [this message]
2026-05-18  7:10 ` [PATCH] tools/vm-set-persist.c: fix "memory leak" Alyssa Ross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260510165541.32975-1-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=devel@spectrum-os.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).