於 西元2014年04月15日 17:03, Erik Larsson 提到:
Hi,

Chih-Wei Huang wrote 2014-04-15 10.33:
I'm trying to fix the compiling warnings of ntfs-3g and
then found a possible leak in ntfs_fuse_parse_path of ntfs-3g.c.
If I read the code correctly, in ntfs_fuse_parse_path(), it's possible
that strdup() succeeds but ntfs_mbstoucs() returns a negative value.
In such a case the callers just treat it as an error and ignores the
allocated path buffer that results in a memory leak.

I suggest the attached patch to fix it, as well as some warnings.
Let me know if it's the correct way to handle it.

It looks like you are reading the code correctly, however the patch also
includes changes outside of ntfs_fuse_parse_path that are not described
by the commit message and appear to be unrelated.
Please take these out and attach a separate patch describing these
changes (I assume they are meant to fix compiler warnings).

Yes. It's documented in the summary and I think it's self-explanatory.
Anyway, I split it into two patches, as attached.
Thank you for the review.

>From 6c3867e20a6719c1c57d525bc04a0a8abd4f967e Mon Sep 17 00:00:00 2001
From: Chih-Wei Huang <[email protected]>
Date: Tue, 15 Apr 2014 16:23:07 +0800
Subject: [PATCH 1/2] Initialize 'path' correctly

It fixes the warnings
src/ntfs-3g.c: In function 'ntfs_fuse_readlink':
src/ntfs-3g.c:987:6: warning: 'path' may be used uninitialized in this function [-Wmaybe-uninitialized]
src/ntfs-3g.c: In function 'ntfs_fuse_create':
src/ntfs-3g.c:1765:6: warning: 'path' may be used uninitialized in this function [-Wmaybe-uninitialized]
---
 src/ntfs-3g.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/ntfs-3g.c b/src/ntfs-3g.c
index baccbfd..2d06342 100644
--- a/src/ntfs-3g.c
+++ b/src/ntfs-3g.c
@@ -896,7 +896,7 @@ exit:
 
 static int ntfs_fuse_readlink(const char *org_path, char *buf, size_t buf_size)
 {
-	char *path;
+	char *path = NULL;
 	ntfschar *stream_name;
 	ntfs_inode *ni = NULL;
 	ntfs_attr *na = NULL;
@@ -1617,7 +1617,7 @@ static int ntfs_fuse_create(const char *org_path, mode_t typemode, dev_t dev,
 	ntfs_inode *dir_ni = NULL, *ni;
 	char *dir_path;
 	le32 securid;
-	char *path;
+	char *path = NULL;
 	gid_t gid;
 	mode_t dsetgid;
 	ntfschar *stream_name;
-- 
1.9.0

>From 34e68e33835c3f4991309c1ae877668cf8cc2d19 Mon Sep 17 00:00:00 2001
From: Chih-Wei Huang <[email protected]>
Date: Tue, 15 Apr 2014 16:23:07 +0800
Subject: [PATCH 2/2] Fix a possible memory leak

In ntfs_fuse_parse_path(), it's possible that strdup() succeeds but
ntfs_mbstoucs() returns a negative value. In such a case the callers
just treat it as an error and ignores the allocated path buffer
that results in a memory leak.
---
 src/ntfs-3g.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/ntfs-3g.c b/src/ntfs-3g.c
index 2d06342..166ef72 100644
--- a/src/ntfs-3g.c
+++ b/src/ntfs-3g.c
@@ -524,8 +524,11 @@ static int ntfs_fuse_parse_path(const char *org_path, char **path,
 		if (stream_name_mbs) {
 			*stream_name = NULL;
 			res = ntfs_mbstoucs(stream_name_mbs, stream_name);
-			if (res < 0)
+			if (res < 0) {
+				free(*path);
+				*path = NULL;
 				return -errno;
+			}
 			return res;
 		}
 	} else
-- 
1.9.0

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
ntfs-3g-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ntfs-3g-devel

Reply via email to