From 784e63c9a24f731bccc86a72b08fd5f20eaf0eb6 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Tue, 29 Nov 2022 14:50:07 -0800 Subject: [PATCH] Fix potential memory leaks Bug: 259995529 Test: make tidy-system_subset Change-Id: I604a308caf498a854b916dc86a8e274148c21ab0 --- fs_mgr/tools/dmuserd.cpp | 19 ++++++++++--------- .../mte_upgrade_test_helper.cpp | 3 ++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/fs_mgr/tools/dmuserd.cpp b/fs_mgr/tools/dmuserd.cpp index 6b68b2871..da7156c58 100644 --- a/fs_mgr/tools/dmuserd.cpp +++ b/fs_mgr/tools/dmuserd.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #define SECTOR_SIZE ((__u64)512) #define BUFFER_BYTES 4096 @@ -133,16 +134,16 @@ int not_splice(int from, int to, __u64 count) { return 0; } -int simple_daemon(char* control_path, char* backing_path) { - int control_fd = open(control_path, O_RDWR); +static int simple_daemon(const std::string& control_path, const std::string& backing_path) { + int control_fd = open(control_path.c_str(), O_RDWR); if (control_fd < 0) { - fprintf(stderr, "Unable to open control device %s\n", control_path); + fprintf(stderr, "Unable to open control device %s\n", control_path.c_str()); return -1; } - int backing_fd = open(backing_path, O_RDWR); + int backing_fd = open(backing_path.c_str(), O_RDWR); if (backing_fd < 0) { - fprintf(stderr, "Unable to open backing device %s\n", backing_path); + fprintf(stderr, "Unable to open backing device %s\n", backing_path.c_str()); return -1; } @@ -286,8 +287,8 @@ void usage(char* prog) { } int main(int argc, char* argv[]) { - char* control_path = NULL; - char* backing_path = NULL; + std::string control_path; + std::string backing_path; char* store; int c; @@ -299,10 +300,10 @@ int main(int argc, char* argv[]) { usage(basename(argv[0])); exit(0); case 'c': - control_path = strdup(optarg); + control_path = optarg; break; case 'b': - backing_path = strdup(optarg); + backing_path = optarg; break; case 'v': verbose = true; diff --git a/init/test_upgrade_mte/mte_upgrade_test_helper.cpp b/init/test_upgrade_mte/mte_upgrade_test_helper.cpp index 10af06b36..3188337ce 100644 --- a/init/test_upgrade_mte/mte_upgrade_test_helper.cpp +++ b/init/test_upgrade_mte/mte_upgrade_test_helper.cpp @@ -22,6 +22,7 @@ #include #include #include +#include int MaybeDowngrade() { int res = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); @@ -58,7 +59,7 @@ int main(int argc, char** argv) { // Disallow automatic upgrade from ASYNC mode. if (prctl(PR_SET_TAGGED_ADDR_CTRL, res & ~PR_MTE_TCF_SYNC, 0, 0, 0) == -1) abort(); } - volatile char* f = (char*)malloc(1); + std::unique_ptr f(new char[1]); f[17] = 'x'; char buf[1]; read(1, buf, 1);