Fix buffer overrun using PutN (closes #672)
Fix inconsistent calling of template functions in PutN in stream.h. When used with a GenericStringBuffer<<UTF8>, MemoryPoolAllocator>, PutN would call PutReserve from stream.h, and PutUnsafe from stringbuffer.h. This resulted in bytes being added to the buffer without allocating space. This was not an issue when used with the default memory allocator, because in this case the specialized PutN is used from stringbuffer.h.
This commit is contained in:
parent
c79958a29b
commit
252e8122bf
@ -95,7 +95,7 @@ inline void PutUnsafe(Stream& stream, typename Stream::Ch c) {
|
|||||||
//! Put N copies of a character to a stream.
|
//! Put N copies of a character to a stream.
|
||||||
template<typename Stream, typename Ch>
|
template<typename Stream, typename Ch>
|
||||||
inline void PutN(Stream& stream, Ch c, size_t n) {
|
inline void PutN(Stream& stream, Ch c, size_t n) {
|
||||||
PutReserve<Stream>(stream, n);
|
PutReserve(stream, n);
|
||||||
for (size_t i = 0; i < n; i++)
|
for (size_t i = 0; i < n; i++)
|
||||||
PutUnsafe(stream, c);
|
PutUnsafe(stream, c);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,13 @@ TEST(StringBuffer, Put) {
|
|||||||
EXPECT_STREQ("A", buffer.GetString());
|
EXPECT_STREQ("A", buffer.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(StringBuffer, PutN_Issue672) {
|
||||||
|
GenericStringBuffer<UTF8<>, MemoryPoolAllocator<> > buffer;
|
||||||
|
EXPECT_EQ(0, buffer.GetSize());
|
||||||
|
rapidjson::PutN(buffer, ' ', 1);
|
||||||
|
EXPECT_EQ(1, buffer.GetSize());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(StringBuffer, Clear) {
|
TEST(StringBuffer, Clear) {
|
||||||
StringBuffer buffer;
|
StringBuffer buffer;
|
||||||
buffer.Put('A');
|
buffer.Put('A');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user