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
This commit is contained in:
Jin Qian 2016-11-03 13:36:35 -07:00
parent a61c48d5fd
commit 905948ed74
3 changed files with 17 additions and 12 deletions

View file

@ -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;

View file

@ -14,12 +14,17 @@
* limitations under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#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 {

View file

@ -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);