From 905948ed74f60cb14940e96819f6f95aeda98f86 Mon Sep 17 00:00:00 2001 From: Jin Qian Date: Thu, 3 Nov 2016 13:36:35 -0700 Subject: [PATCH] fs_mgr: fix clang static analyzer warning Pointer from strdup is lost hence triggers mem leak warning from clang, since ptr returned from basename may not point to start of duplicated string any more. Switch to use gnu version of basename which doesn't modify input string so that strdup is no longer necessary. Bug: 27126348 Change-Id: Iadb5cf343437a1e27e588ee067b084897633ea07 Merged-In: I937a68c01c223230932c932bffdd35da6503c3c4 --- fs_mgr/fs_mgr.c | 4 ++-- fs_mgr/fs_mgr_main.c | 23 ++++++++++++++--------- fs_mgr/include/fs_mgr.h | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/fs_mgr/fs_mgr.c b/fs_mgr/fs_mgr.c index d57025571..b1511fed8 100644 --- a/fs_mgr/fs_mgr.c +++ b/fs_mgr/fs_mgr.c @@ -241,7 +241,7 @@ static int __mount(const char *source, const char *target, const struct fstab_re return ret; } -static int fs_match(char *in1, char *in2) +static int fs_match(const char *in1, const char *in2) { char *n1; char *n2; @@ -651,7 +651,7 @@ int fs_mgr_mount_all(struct fstab *fstab, int mount_mode) * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one * in turn, and stop on 1st success, or no more match. */ -int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, +int fs_mgr_do_mount(struct fstab *fstab, const char *n_name, char *n_blk_device, char *tmp_mount_point) { int i = 0; diff --git a/fs_mgr/fs_mgr_main.c b/fs_mgr/fs_mgr_main.c index 33a749653..4bfe20288 100644 --- a/fs_mgr/fs_mgr_main.c +++ b/fs_mgr/fs_mgr_main.c @@ -14,12 +14,17 @@ * limitations under the License. */ +#define _GNU_SOURCE + #include #include #include -#include #include "fs_mgr_priv.h" +#ifdef _LIBGEN_H +#warning "libgen.h must not be included" +#endif + char *me = ""; static void usage(void) @@ -32,10 +37,10 @@ static void usage(void) * and exit the program, do not return to the caller. * Return the number of argv[] entries consumed. */ -static void parse_options(int argc, char *argv[], int *a_flag, int *u_flag, int *n_flag, - char **n_name, char **n_blk_dev) +static void parse_options(int argc, char * const argv[], int *a_flag, int *u_flag, int *n_flag, + const char **n_name, const char **n_blk_dev) { - me = basename(strdup(argv[0])); + me = basename(argv[0]); if (argc <= 1) { usage(); @@ -75,14 +80,14 @@ static void parse_options(int argc, char *argv[], int *a_flag, int *u_flag, int return; } -int main(int argc, char *argv[]) +int main(int argc, char * const argv[]) { int a_flag=0; int u_flag=0; int n_flag=0; - char *n_name=NULL; - char *n_blk_dev=NULL; - char *fstab_file=NULL; + const char *n_name=NULL; + const char *n_blk_dev=NULL; + const char *fstab_file=NULL; struct fstab *fstab=NULL; klog_set_level(6); @@ -97,7 +102,7 @@ int main(int argc, char *argv[]) if (a_flag) { return fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT); } else if (n_flag) { - return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0); + return fs_mgr_do_mount(fstab, n_name, (char *)n_blk_dev, 0); } else if (u_flag) { return fs_mgr_unmount_all(fstab); } else { diff --git a/fs_mgr/include/fs_mgr.h b/fs_mgr/include/fs_mgr.h index c0116ef80..b120f7c35 100644 --- a/fs_mgr/include/fs_mgr.h +++ b/fs_mgr/include/fs_mgr.h @@ -95,7 +95,7 @@ int fs_mgr_mount_all(struct fstab *fstab, int mount_mode); #define FS_MGR_DOMNT_FAILED (-1) #define FS_MGR_DOMNT_BUSY (-2) -int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, +int fs_mgr_do_mount(struct fstab *fstab, const char *n_name, char *n_blk_device, char *tmp_mount_point); int fs_mgr_do_tmpfs_mount(char *n_name); int fs_mgr_unmount_all(struct fstab *fstab);