Skip to content

Commit 7e2419d

Browse files
BUGFIX: Buffer overflow in value ptr helper (#23)
1 parent d9adf57 commit 7e2419d

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ add_executable(TestDriver
151151
"test/unit/util/cryptUtilsTest.cpp"
152152
"test/unit/util/dateAndTimeUtilsTest.cpp"
153153
"test/unit/util/stringTrimTest.cpp"
154+
"test/unit/util/valuePtrHelperTest.cpp"
154155
"test/constants.cpp"
155156
"test/gtestTest.cpp"
156157
)

src/util/valuePtrHelper.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ void writeNullTermStringToPtr(SQLPOINTER InfoValuePtr,
2323
rsize_t nullcharPosition = length + 1;
2424
if (InfoValuePtr) {
2525
char* infoCharPtr = reinterpret_cast<char*>(InfoValuePtr);
26+
// strcpy_s is documented to write a null terminator to the
27+
// destination character array.
2628
strcpy_s(infoCharPtr, nullcharPosition, s.c_str());
27-
infoCharPtr[nullcharPosition] = '\0';
2829
}
2930
if (StringLengthPtr) {
3031
*StringLengthPtr = static_cast<T>(length);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "gtest/gtest.h"
2+
#include <string>
3+
4+
#include "../../../src/util/valuePtrHelper.hpp"
5+
6+
TEST(ValuePtrHelperTest, CanWriteNullTermStringToPtr) {
7+
// Setup
8+
std::string s = "hello";
9+
char buffer[20] = {};
10+
short len = 0;
11+
12+
// Test
13+
writeNullTermStringToPtr(buffer, s, &len);
14+
15+
16+
// Assert
17+
EXPECT_STREQ(s.c_str(), buffer);
18+
EXPECT_EQ(len, s.length());
19+
}
20+
21+
TEST(ValuePtrHelperTest, CanWriteEmptyStringToPtr) {
22+
// Setup
23+
std::string s = "";
24+
char buffer[5] = {};
25+
short len = 0;
26+
27+
// Test
28+
writeNullTermStringToPtr(buffer, s, &len);
29+
30+
// Assert
31+
EXPECT_STREQ(s.c_str(), buffer);
32+
EXPECT_EQ(len, 0);
33+
}
34+
35+
TEST(ValuePtrHelperTest, NullCharInCorrectPosition) {
36+
/*
37+
Setup
38+
39+
The buffer is actually 8 characters, but we're going to tell
40+
the function that it's 4 characters. Then we can look at the
41+
resulting buffer to confirm that it has the null terminator
42+
in the correct place (and no extras that would have
43+
overflowed the buffer).
44+
45+
Buffer: "bbbbbbbb"
46+
String: "aaaa"
47+
Length: 4
48+
Expected Result: "aaaa\0bbb"
49+
*/
50+
std::string s = "aaaa";
51+
char buffer[9] = {"bbbbbbbb"};
52+
short len = 4;
53+
54+
// Test
55+
writeNullTermStringToPtr(buffer, s, &len);
56+
57+
// Assert
58+
EXPECT_STREQ(s.c_str(), buffer); // Correct data
59+
EXPECT_EQ(len, s.length()); // Correct length
60+
EXPECT_EQ(buffer[4], '\0'); // Check for null terminator
61+
EXPECT_EQ(buffer[5], 'b'); // Check for correct padding
62+
EXPECT_EQ(buffer[6], 'b'); // Check for correct padding
63+
EXPECT_EQ(buffer[7], 'b'); // Check for correct padding
64+
EXPECT_EQ(buffer[8], '\0'); // Check for original null terminator.
65+
}

0 commit comments

Comments
 (0)