From b239441ff9235de2827cf4f0b37e2cfefaa35be4 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Thu, 19 Jul 2007 12:48:22 +0100 Subject: [PATCH] Buffer::Iterator::Write (buffer, size) should have a uint32_t parameter, not uint16_t, else integer overflow may occur. Fixes bug #54. --- src/common/buffer.cc | 35 ++++++++++++++++++++++++++++++----- src/common/buffer.h | 4 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 07a370afa..c17c51c23 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -440,6 +440,7 @@ Buffer::PeekData (void) const #ifdef RUN_SELF_TESTS #include "ns3/test.h" +#include "ns3/random-variable.h" #include namespace ns3 { @@ -501,14 +502,14 @@ BufferTest::EnsureWrittenBytes (Buffer b, uint32_t n, uint8_t array[]) uint8_t bytes[] = {__VA_ARGS__}; \ if (!EnsureWrittenBytes (buffer, n , bytes)) \ { \ - ok = false; \ + result = false; \ } \ } bool BufferTest::RunTests (void) { - bool ok = true; + bool result = true; Buffer buffer; Buffer::Iterator i; buffer.AddAtStart (6); @@ -555,7 +556,7 @@ BufferTest::RunTests (void) i.Prev (2); if (i.ReadNtohU16 () != 0xff00) { - ok = false; + result = false; } i.Prev (2); i.WriteU16 (saved); @@ -645,7 +646,7 @@ BufferTest::RunTests (void) buffer.RemoveAtEnd (8); if (buffer.GetSize () != 0) { - ok = false; + result = false; } buffer = Buffer (6); @@ -669,7 +670,31 @@ BufferTest::RunTests (void) i.Prev (100); i.WriteU8 (1, 100); - return ok; + // Bug #54 + { + const uint32_t actualSize = 72602; + const uint32_t chunkSize = 67624; + UniformVariable bytesRng (0, 256); + + Buffer inputBuffer; + Buffer outputBuffer; + + inputBuffer.AddAtEnd (actualSize); + { + Buffer::Iterator iter = inputBuffer.Begin (); + for (uint32_t i = 0; i < actualSize; i++) + iter.WriteU8 (static_cast (bytesRng.GetValue ())); + } + + outputBuffer.AddAtEnd (chunkSize); + Buffer::Iterator iter = outputBuffer.End (); + iter.Prev (chunkSize); + iter.Write (inputBuffer.PeekData (), chunkSize); + + NS_TEST_ASSERT (memcmp (inputBuffer.PeekData (), outputBuffer.PeekData (), chunkSize) == 0); + } + + return result; } diff --git a/src/common/buffer.h b/src/common/buffer.h index 02f1f69f2..244d8b78b 100644 --- a/src/common/buffer.h +++ b/src/common/buffer.h @@ -158,7 +158,7 @@ public: * Write the data in buffer and avance the iterator position * by size bytes. */ - inline void Write (uint8_t const*buffer, uint16_t size); + inline void Write (uint8_t const*buffer, uint32_t size); /** * \param start the start of the data to copy * \param end the end of the data to copy @@ -621,7 +621,7 @@ Buffer::Iterator::WriteHtonU64 (uint64_t data) m_current += 8; } void -Buffer::Iterator::Write (uint8_t const*buffer, uint16_t size) +Buffer::Iterator::Write (uint8_t const*buffer, uint32_t size) { uint8_t *current = m_data + GetIndex (size); memcpy (current, buffer, size);