Skip to content

Commit 8ed31cd

Browse files
committed
CDRIVER-2596 buffer underflow in bson_strncpy
Calling bson_strncpy with size 0 would write one byte before the start of the destination string.
1 parent 992baab commit 8ed31cd

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/libbson/src/bson/bson-string.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ bson_strncpy (char *dst, /* IN */
562562
const char *src, /* IN */
563563
size_t size) /* IN */
564564
{
565+
if (size == 0) {
566+
return;
567+
}
568+
565569
#ifdef _MSC_VER
566570
strncpy_s (dst, size, src, _TRUNCATE);
567571
#else

src/libbson/tests/test-string.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ test_bson_strncpy (void)
279279
ASSERT_CMPSTR ("foo", buf);
280280
bson_strncpy (buf, "foobar", sizeof buf);
281281
ASSERT_CMPSTR ("foob", buf);
282+
/* CDRIVER-2596 make sure strncpy with size 0 doesn't write to buf[-1] */
283+
bson_strncpy (buf + 1, "z", 0);
284+
ASSERT_CMPSTR ("foob", buf);
282285
}
283286

284287

0 commit comments

Comments
 (0)