Skip to content

Commit ec15fce

Browse files
committed
fix: encode() UB pointer arithmetic for small buffers
1 parent 4f466ce commit ec15fce

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

include/boost/url/impl/encode.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ encode(
132132
auto const end = dest + size;
133133
auto const last = it + s.size();
134134
auto const dest0 = dest;
135-
auto const end3 = end - 3;
136135

137136
if (!opt.space_as_plus)
138137
{
@@ -147,7 +146,7 @@ encode(
147146
++it;
148147
continue;
149148
}
150-
if (dest > end3)
149+
if (end - dest < 3)
151150
return dest - dest0;
152151
encode(dest, c);
153152
++it;
@@ -177,7 +176,7 @@ encode(
177176
++it;
178177
continue;
179178
}
180-
if(dest > end3)
179+
if(end - dest < 3)
181180
return dest - dest0;
182181
encode(dest, c);
183182
++it;

test/unit/encode.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,25 @@ class encode_test
196196
}
197197
}
198198

199+
void
200+
testEncodeZeroDest()
201+
{
202+
// encode() with zero-size dest buffer
203+
// must not perform UB pointer arithmetic
204+
{
205+
char buf[1] = {};
206+
std::size_t n = encode(
207+
buf, 0, "test", pchars);
208+
BOOST_TEST_EQ(n, 0u);
209+
}
210+
}
211+
199212
void
200213
run()
201214
{
202215
testEncode();
203216
testEncodeExtras();
217+
testEncodeZeroDest();
204218
testJavadocs();
205219
}
206220
};

0 commit comments

Comments
 (0)