Skip to content

Commit 1397f38

Browse files
committed
Add max length to string16 en/decoding
1 parent d78b57a commit 1397f38

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/network/network_string.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ void NetworkString::unitTesting()
5151
max = smax.getInt24();
5252
assert(max == 0x7fffff);
5353

54+
// Check max len handling in decodeString16
55+
BareNetworkString string16;
56+
string16.addUInt32(0).encodeString16(L"abcdefg").encodeString(std::string("hijklmnop"));
57+
max = string16.getUInt32();
58+
core::stringw out_1;
59+
std::string out_2;
60+
string16.decodeString16(&out_1, 4);
61+
assert(out_1 == L"abcd");
62+
string16.decodeString(&out_2);
63+
assert(out_2 == "hijklmnop");
64+
5465
// Check log message format
5566
BareNetworkString slog(28);
5667
for(unsigned int i=0; i<28; i++)
@@ -118,7 +129,8 @@ int BareNetworkString::decodeStringW(irr::core::stringw *out) const
118129
/** Encode string with max length of 16bit and utf32, used in motd or
119130
* chat. */
120131
BareNetworkString& BareNetworkString::encodeString16(
121-
const irr::core::stringw& value)
132+
const irr::core::stringw& value,
133+
uint16_t max_len)
122134
{
123135
size_t utf32_len = 0;
124136
const uint32_t* utf32 = NULL;
@@ -137,16 +149,17 @@ BareNetworkString& BareNetworkString::encodeString16(
137149
}
138150

139151
uint16_t str_len = (uint16_t)utf32_len;
140-
if (utf32_len > 65535)
141-
str_len = 65535;
152+
if (utf32_len > max_len)
153+
str_len = max_len;
142154
addUInt16(str_len);
143155
for (unsigned i = 0; i < str_len; i++)
144156
addUInt32(utf32[i]);
145157
return *this;
146158
} // encodeString16
147159

148160
// ----------------------------------------------------------------------------
149-
int BareNetworkString::decodeString16(irr::core::stringw* out) const
161+
int BareNetworkString::decodeString16(irr::core::stringw* out,
162+
uint16_t max_len)
150163
{
151164
uint16_t str_len = getUInt16();
152165
// Irrlicht string has bad append speed for large string
@@ -156,6 +169,11 @@ int BareNetworkString::decodeString16(irr::core::stringw* out) const
156169
out->reserve((str_len * 2) + 1);
157170
for (unsigned i = 0; i < str_len; i++)
158171
{
172+
if (i >= max_len)
173+
{
174+
skip((str_len - i) * 4);
175+
break;
176+
}
159177
uint32_t c = getUInt32();
160178
if (sizeof(wchar_t) == 2)
161179
{

src/network/network_string.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ friend class Crypto;
146146
/** Allows one to read a buffer from the beginning again. */
147147
void reset() { m_current_offset = 0; }
148148
// ------------------------------------------------------------------------
149-
BareNetworkString& encodeString16(const irr::core::stringw& value);
149+
BareNetworkString& encodeString16(const irr::core::stringw& value,
150+
uint16_t max_len = 65535);
150151
// ------------------------------------------------------------------------
151-
int decodeString16(irr::core::stringw* out) const;
152+
int decodeString16(irr::core::stringw* out,
153+
uint16_t max_len = 65535);
152154
// ------------------------------------------------------------------------
153155
BareNetworkString& encodeString(const std::string &value);
154156
BareNetworkString& encodeString(const irr::core::stringw &value);

0 commit comments

Comments
 (0)