From 9708b080608f70250ed72121927004a42f549c4a Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Fri, 22 Nov 2024 16:56:15 -0500 Subject: [PATCH] KPath: Use a default buffer size of B_PATH_NAME_LENGTH without + 1. B_PATH_NAME_LENGTH == PATH_MAX, and PATH_MAX is inclusive of the final NULL terminator, so we don't need a + 1 here. The original KPath default was to not use + 1, but that was changed in 42e3c6f97874f37701385e7027c77e4366d7c450 due to all the consumers that did. But all those consumers are wrong, it appears; they should just be using the default length instead. So now we do that. --- headers/private/kernel/fs/KPath.h | 7 ++++--- src/system/kernel/fs/KPath.cpp | 10 ++++++---- src/system/kernel/fs/vfs.cpp | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/headers/private/kernel/fs/KPath.h b/headers/private/kernel/fs/KPath.h index 372b02fc2d..726ed810f8 100644 --- a/headers/private/kernel/fs/KPath.h +++ b/headers/private/kernel/fs/KPath.h @@ -22,15 +22,16 @@ public: TRAVERSE_LEAF_LINK = 0x02, LAZY_ALLOC = 0x04 }; + public: - KPath(size_t bufferSize = B_PATH_NAME_LENGTH + 1); + KPath(size_t bufferSize = B_PATH_NAME_LENGTH); KPath(const char* path, int32 flags = DEFAULT, - size_t bufferSize = B_PATH_NAME_LENGTH + 1); + size_t bufferSize = B_PATH_NAME_LENGTH); KPath(const KPath& other); ~KPath(); status_t SetTo(const char* path, int32 flags = DEFAULT, - size_t bufferSize = B_PATH_NAME_LENGTH + 1); + size_t bufferSize = B_PATH_NAME_LENGTH); void Adopt(KPath& other); status_t InitCheck() const; diff --git a/src/system/kernel/fs/KPath.cpp b/src/system/kernel/fs/KPath.cpp index cfca5be237..32ded8b69d 100644 --- a/src/system/kernel/fs/KPath.cpp +++ b/src/system/kernel/fs/KPath.cpp @@ -80,7 +80,7 @@ status_t KPath::SetTo(const char* path, int32 flags, size_t bufferSize) { if (bufferSize == 0) - bufferSize = B_PATH_NAME_LENGTH + 1; + bufferSize = B_PATH_NAME_LENGTH; // free the previous buffer, if the buffer size differs if (fBuffer != NULL && fBufferSize != bufferSize) { @@ -249,11 +249,13 @@ KPath::DetachBuffer() { char* buffer = fBuffer; - if (fBufferSize == (B_PATH_NAME_LENGTH + 1)) { +#ifdef _KERNEL_MODE + if (fBufferSize == B_PATH_NAME_LENGTH) { buffer = (char*)malloc(fBufferSize); memcpy(buffer, fBuffer, fBufferSize); _FreeBuffer(); } +#endif if (fBuffer != NULL) { fBuffer = NULL; @@ -427,7 +429,7 @@ KPath::_AllocateBuffer() { if (fBuffer == NULL && fBufferSize != 0) { #ifdef _KERNEL_MODE - if (fBufferSize == (B_PATH_NAME_LENGTH + 1)) + if (fBufferSize == B_PATH_NAME_LENGTH) fBuffer = (char*)object_cache_alloc(sPathNameCache, 0); else #endif @@ -448,7 +450,7 @@ void KPath::_FreeBuffer() { #ifdef _KERNEL_MODE - if (fBufferSize == (B_PATH_NAME_LENGTH + 1)) + if (fBufferSize == B_PATH_NAME_LENGTH) object_cache_free(sPathNameCache, fBuffer, 0); else #endif diff --git a/src/system/kernel/fs/vfs.cpp b/src/system/kernel/fs/vfs.cpp index 85e2098512..2a5dbbbccb 100644 --- a/src/system/kernel/fs/vfs.cpp +++ b/src/system/kernel/fs/vfs.cpp @@ -5284,7 +5284,7 @@ vfs_init(kernel_args* args) panic("vfs_init: error creating mounts hash table\n"); sPathNameCache = create_object_cache("vfs path names", - B_PATH_NAME_LENGTH + 1, 8, NULL, NULL, NULL); + B_PATH_NAME_LENGTH, 8, NULL, NULL, NULL); if (sPathNameCache == NULL) panic("vfs_init: error creating path name object_cache\n");