From b4598d95e6d1ff1772d2788630700b7dad364b5e Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 28 Jan 2006 22:34:36 +0000 Subject: [PATCH] Fixed nasty bug in BAppFileInfo::_ReadData(). When an attribute or resource to be read did not exist and the method was told to allocate a buffer, it would try to allocate the buffer with an uninitialized size value. This basically concerned SetSupportedTypes() and methods using that one (IsSupportedType(), Supports()). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@16131 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/build/libbe/storage/AppFileInfo.cpp | 10 ++++++++-- src/kits/storage/AppFileInfo.cpp | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/build/libbe/storage/AppFileInfo.cpp b/src/build/libbe/storage/AppFileInfo.cpp index c5823333af..e7e9767348 100644 --- a/src/build/libbe/storage/AppFileInfo.cpp +++ b/src/build/libbe/storage/AppFileInfo.cpp @@ -1063,7 +1063,7 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, // check type and size, allocate a buffer, if required if (error == B_OK && info.type != type) error = B_BAD_VALUE; - if (allocatedBuffer) { + if (error == B_OK && allocatedBuffer) { buffer = malloc(info.size); if (!buffer) error = B_NO_MEMORY; @@ -1084,6 +1084,12 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, } foundData = (error == B_OK); + + // free the allocated buffer on error + if (!foundData && allocatedBuffer && buffer) { + free(buffer); + buffer = NULL; + } } if (!foundData && IsUsingResources()) { @@ -1099,7 +1105,7 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, // check id and size, allocate a buffer, if required if (error == B_OK && id >= 0 && idFound != id) error = B_ENTRY_NOT_FOUND; - if (allocatedBuffer) { + if (error == B_OK && allocatedBuffer) { buffer = malloc(sizeFound); if (!buffer) error = B_NO_MEMORY; diff --git a/src/kits/storage/AppFileInfo.cpp b/src/kits/storage/AppFileInfo.cpp index 62ae1c7b44..09c71cb814 100644 --- a/src/kits/storage/AppFileInfo.cpp +++ b/src/kits/storage/AppFileInfo.cpp @@ -1063,7 +1063,7 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, // check type and size, allocate a buffer, if required if (error == B_OK && info.type != type) error = B_BAD_VALUE; - if (allocatedBuffer) { + if (error == B_OK && allocatedBuffer) { buffer = malloc(info.size); if (!buffer) error = B_NO_MEMORY; @@ -1084,6 +1084,12 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, } foundData = (error == B_OK); + + // free the allocated buffer on error + if (!foundData && allocatedBuffer && buffer) { + free(buffer); + buffer = NULL; + } } if (!foundData && IsUsingResources()) { @@ -1099,7 +1105,7 @@ BAppFileInfo::_ReadData(const char *name, int32 id, type_code type, // check id and size, allocate a buffer, if required if (error == B_OK && id >= 0 && idFound != id) error = B_ENTRY_NOT_FOUND; - if (allocatedBuffer) { + if (error == B_OK && allocatedBuffer) { buffer = malloc(sizeFound); if (!buffer) error = B_NO_MEMORY;