Browse Source

[trac536] Handle empty buffers

Michal 'vorner' Vaner 14 years ago
parent
commit
aac391dda5
2 changed files with 10 additions and 2 deletions
  1. 2 2
      src/lib/dns/buffer.h
  2. 8 0
      src/lib/dns/tests/buffer_unittest.cc

+ 2 - 2
src/lib/dns/buffer.h

@@ -296,7 +296,7 @@ public:
         // We use malloc and free instead of C++ new[] and delete[].
         // This way we can use realloc, which may in fact do it without a copy.
         buffer_ = static_cast<uint8_t*>(malloc(allocated_));
-        if (buffer_ == NULL) {
+        if (buffer_ == NULL && len != 0) {
             throw std::bad_alloc();
         }
     }
@@ -308,7 +308,7 @@ public:
         allocated_(other.allocated_)
     {
         buffer_ = static_cast<uint8_t*>(malloc(allocated_));
-        if (buffer_ == NULL) {
+        if (buffer_ == NULL && allocated_ != 0) {
             throw std::bad_alloc();
         }
         memcpy(buffer_, other.buffer_, size_);

+ 8 - 0
src/lib/dns/tests/buffer_unittest.cc

@@ -219,4 +219,12 @@ TEST_F(BufferTest, outputBufferAssign) {
     });
 }
 
+TEST_F(BufferTest, outputBufferZeroSize) {
+    // Some OSes might return NULL on malloc for 0 size, so check it works
+    EXPECT_NO_THROW({
+        OutputBuffer first(0);
+        OutputBuffer copy(first);
+    });
+}
+
 }