diff options
| author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
|---|---|---|
| committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
| commit | 959575a5ca598bf5f37fb5cebe7ed1d80d3d71f7 (patch) | |
| tree | acc8d60aedb12b70048e676e8a7349deb0010db8 /src/mongo/bson/util/builder_test.cpp | |
| parent | 76588293975fc059cf076779e4283e6ffaf8afff (diff) | |
New upstream version 6.0.20upstream
Diffstat (limited to 'src/mongo/bson/util/builder_test.cpp')
| -rw-r--r-- | src/mongo/bson/util/builder_test.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/mongo/bson/util/builder_test.cpp b/src/mongo/bson/util/builder_test.cpp index a4ed4c87115..0ad7518ffe5 100644 --- a/src/mongo/bson/util/builder_test.cpp +++ b/src/mongo/bson/util/builder_test.cpp @@ -38,12 +38,43 @@ TEST(Builder, String1) { ASSERT_EQUALS(small, "eliot"); BufBuilder bb; - bb.appendStr(small); + bb.appendCStr(small); + + ASSERT_EQUALS(bb.len(), small.size() + 1); + ASSERT_EQUALS(bb.buf()[small.size()], 0); ASSERT_EQUALS(0, strcmp(bb.buf(), "eliot")); ASSERT_EQUALS(0, strcmp("eliot", bb.buf())); } +TEST(Builder, StringNulByteHandling) { + auto hasNulByte = "hello\0world"_sd; + + { + // appendCStr() throws without changing bb; + BufBuilder bb; + ASSERT_THROWS_CODE(bb.appendCStr(hasNulByte), DBException, 9527900); + ASSERT_EQ(bb.len(), 0); + } + + { + // appendStrBytes appends embedded NUL without terminator. + BufBuilder bb; + bb.appendStrBytes(hasNulByte); + ASSERT_EQ(StringData(bb.buf(), bb.len()), hasNulByte); + } + + { + // appendStrBytesAndNul appends embedded NUL and NUL terminator. + BufBuilder bb; + bb.appendStrBytesAndNul(hasNulByte); + // Since hasNulByte points to a string literal, we know that + // *(hasNulByte.data() + hasNulByte.size()) is valid and == '\0' + ASSERT_EQ(StringData(bb.buf(), bb.len()), + StringData(hasNulByte.rawData(), hasNulByte.size() + 1)); + } +} + TEST(Builder, StringBuilderAddress) { const void* longPtr = reinterpret_cast<const void*>(-1); const void* shortPtr = reinterpret_cast<const void*>(static_cast<uintptr_t>(0xDEADBEEF)); |
