Skip to content

Commit e6c9311

Browse files
committed
Merge pull request #28 from pjohnmeyer/fixCheckEqualCrashForLargeNumbers
Changed sprintf to snprintf in MemoryOutStream::FormatToStream. Fixes #18.
2 parents a695db5 + 4c366ee commit e6c9311

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/MemoryOutStream.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const
3232
{
3333
using namespace std;
3434

35-
char txt[32];
36-
sprintf(txt, format, value);
35+
const size_t BUFFER_SIZE=32;
36+
char txt[BUFFER_SIZE];
37+
snprintf(txt, BUFFER_SIZE, format, value);
3738
stream << txt;
3839
}
3940

@@ -127,7 +128,7 @@ MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n)
127128

128129
MemoryOutStream& MemoryOutStream::operator <<(float const f)
129130
{
130-
FormatToStream(*this, "%ff", f);
131+
FormatToStream(*this, "%0.6f", f);
131132
return *this;
132133
}
133134

@@ -145,7 +146,7 @@ MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s)
145146

146147
MemoryOutStream& MemoryOutStream::operator <<(double const d)
147148
{
148-
FormatToStream(*this, "%f", d);
149+
FormatToStream(*this, "%0.6f", d);
149150
return *this;
150151
}
151152

src/tests/TestMemoryOutStream.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstring>
55
#include <climits>
66
#include <cstdlib>
7+
#include <cfloat>
78

89
using namespace UnitTest;
910
using namespace std;
@@ -152,15 +153,15 @@ TEST(WritingStringLongerThanCapacityFitsInNewBuffer)
152153
TEST(WritingIntLongerThanCapacityFitsInNewBuffer)
153154
{
154155
MemoryOutStream stream(8);
155-
stream << "aaaa" << 123456;;
156+
stream << "aaaa" << 123456;
156157
CHECK_EQUAL("aaaa123456", stream.GetText());
157158
}
158159

159160
TEST(WritingFloatLongerThanCapacityFitsInNewBuffer)
160161
{
161162
MemoryOutStream stream(8);
162-
stream << "aaaa" << 123456.0f;;
163-
CHECK_EQUAL("aaaa123456.000000f", stream.GetText());
163+
stream << "aaaa" << 123456.0f;
164+
CHECK_EQUAL("aaaa123456.000000", stream.GetText());
164165
}
165166

166167
TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer)
@@ -170,6 +171,13 @@ TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer)
170171
CHECK_EQUAL("aaaa32145", stream.GetText());
171172
}
172173

174+
TEST(VerifyLargeDoubleCanBeStreamedWithoutCrashing)
175+
{
176+
MemoryOutStream stream(8);
177+
stream << DBL_MAX;
178+
CHECK(true);
179+
}
180+
173181
#endif
174182

175183
}

0 commit comments

Comments
 (0)