Skip to content

Commit

Permalink
Standardize behavior of CrtAllocator::Malloc()
Browse files Browse the repository at this point in the history
  • Loading branch information
miloyip committed Apr 17, 2015
1 parent 4cd14b7 commit 0e8bbe5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/rapidjson/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ concept Allocator {
class CrtAllocator {
public:
static const bool kNeedFree = true;
void* Malloc(size_t size) { return std::malloc(size); }
void* Malloc(size_t size) {
if (size) // behavior of malloc(0) is implementation defined.
return std::malloc(size);
else
return NULL; // standardize to returning NULL.
}
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return std::realloc(originalPtr, newSize); }
static void Free(void *ptr) { std::free(ptr); }
};
Expand Down
2 changes: 2 additions & 0 deletions test/unittest/allocatorstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ using namespace rapidjson;

template <typename Allocator>
void TestAllocator(Allocator& a) {
EXPECT_TRUE(a.Malloc(0) == 0);

uint8_t* p = (uint8_t*)a.Malloc(100);
EXPECT_TRUE(p != 0);
for (size_t i = 0; i < 100; i++)
Expand Down

0 comments on commit 0e8bbe5

Please sign in to comment.