From patchwork Thu Nov 3 15:28:55 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [5/7] libnsdb: Fix memory allocation and access bugs in nsdb_normalize_path() Date: Thu, 03 Nov 2011 05:28:55 -0000 From: Chuck Lever X-Patchwork-Id: 123459 Message-Id: <20111103152854.2445.48144.stgit@degas.1015granger.net> To: fedfs-utils-devel@oss.oracle.com 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 --- 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;