Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Fixed a bug in output stream (big floats induced buffer overflow)
Browse files Browse the repository at this point in the history
Change-Id: Ib08e0b6f31f2f38e6972b5b815fcf54eb7db5e35
  • Loading branch information
Violin Yanev authored and Martin Veith committed Sep 11, 2013
1 parent 8276f29 commit 8a78cac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules/capu/include/capu/util/StringOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ namespace capu
StringOutputStream&
StringOutputStream::operator<<(const float_t value)
{
char_t buffer[16];
// Maximum float value has 47 digits. +1 for termination 0
char_t buffer[48];
switch(mFloatingPointType)
{
case NORMAL:
StringUtils::Sprintf(buffer, sizeof(buffer), "%f", value);
StringUtils::Sprintf(buffer, sizeof(buffer), "%.6f", value);
break;
case FIXED:
StringUtils::Sprintf(buffer, 16, "%.4f", value);
StringUtils::Sprintf(buffer, 48, "%.4f", value);
break;
}

Expand Down
8 changes: 8 additions & 0 deletions modules/capu/test/util/StringOutputStreamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "StringOutputStreamTest.h"
#include "capu/os/NumericLimits.h"

namespace capu
{
Expand Down Expand Up @@ -42,6 +43,13 @@ namespace capu
EXPECT_EQ(9U, outputStream.length());
}

TEST_F(StringOutputStreamTest, WriteFloatMaximumNegative)
{
outputStream << -capu::generic::NumericLimits::Max<capu::float_t>();
outputStream.flush();
EXPECT_EQ(47U, outputStream.length());
}

TEST_F(StringOutputStreamTest, WriteInt32)
{
outputStream << -1059192060;
Expand Down

0 comments on commit 8a78cac

Please sign in to comment.