[fedfs-utils] [PATCH 5/7] libnsdb: Fix memory allocation and access bugs in nsdb_normalize_path()

Chuck Lever chuck.lever at oracle.com
Thu Nov 3 08:28:55 PDT 2011


The malloc(3) call in nsdb_normalize_path() neglected to allocate
space for the NUL on the end of the string.

As a minor optimization, we don't need to perform the strlen(3) at the
end of the function to compute the length of the result.  We already
have that value in j.

Signed-off-by: Chuck Lever <chuck.lever at oracle.com>
---

 src/libpath/path.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/libpath/path.c b/src/libpath/path.c
index 0f7a621..91c648c 100644
--- a/src/libpath/path.c
+++ b/src/libpath/path.c
@@ -83,7 +83,7 @@ nsdb_normalize_path(const char *pathname)
 		return NULL;
 	}
 
-	result = malloc(len);
+	result = malloc(len + 1);
 	if (result == NULL) {
 		xlog(L_ERROR, "%s: Failed to allocate pathname buffer",
 			__func__);
@@ -97,9 +97,8 @@ nsdb_normalize_path(const char *pathname)
 	}
 	result[j] = '\0';
 
-	len = strlen(result);
-	if (len > 1 && result[len - 1] == '/')
-		result[len - 1] = '\0';
+	if (j > 1 && result[j - 1] == '/')
+		result[j - 1] = '\0';
 
 	xlog(D_CALL, "%s: result = '%s'", __func__, result);
 	return result;




More information about the fedfs-utils-devel mailing list