Remove potential buffer overflow
This commit is contained in:
parent
4a6fc9d140
commit
f1d6467eb7
|
@ -33,25 +33,19 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PathAppendTest, PathLengthGreaterThanMaxPathLen) {
|
TEST(PathAppendTest, PathLengthGreaterThanMaxPathLen) {
|
||||||
const char test_folder_name[] = "/abcdefg";
|
const std::string test_folder("/abcdefg");
|
||||||
const int test_folder_name_len = 8;
|
const int test_folder_len = test_folder.size();
|
||||||
const int path_len_4098 = 4098;
|
const int path_len_4098 = 4098;
|
||||||
char very_long_path[path_len_4098] = { 0 };
|
std::string very_long_path;
|
||||||
char *p = very_long_path;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MAX_PATH_LEN is an integer multiple of test_folder_name_len,
|
* MAX_PATH_LEN is an integer multiple of test_folder_len,
|
||||||
* so it would fit perfectly in the 4096 bytes of very_long_path.
|
* so it would fit perfectly in 4096 bytes,
|
||||||
* According to strncat manual, if the length of the C string in
|
|
||||||
* source is less than num, only the content up to the terminating
|
|
||||||
* null-character is copied.
|
|
||||||
*/
|
*/
|
||||||
for (int i = 0; i < path_len_4098; i += test_folder_name_len) {
|
for (int i = path_len_4098 / test_folder_len; i != 0; i--)
|
||||||
strncat(p, test_folder_name, test_folder_name_len + 1);
|
very_long_path += test_folder;
|
||||||
p += test_folder_name_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *path = path_append(very_long_path, test_file_name);
|
char *path = path_append(very_long_path.c_str(), test_file_name);
|
||||||
ASSERT_NE(nullptr, path);
|
ASSERT_NE(nullptr, path);
|
||||||
ASSERT_EQ(path[MAX_PATH_LEN - 1], 'g');
|
ASSERT_EQ(path[MAX_PATH_LEN - 1], 'g');
|
||||||
ASSERT_EQ(path[MAX_PATH_LEN + 0], '/');
|
ASSERT_EQ(path[MAX_PATH_LEN + 0], '/');
|
||||||
|
|
Loading…
Reference in New Issue