libstdc++: Fix unsafe use of dirent::d_name [PR107814]

Copy the fix for PR 104731 to the equivalent experimental::filesystem
test.

libstdc++-v3/ChangeLog:

	PR libstdc++/107814
	* testsuite/experimental/filesystem/iterators/error_reporting.cc:
	Use a static buffer with space after it.
This commit is contained in:
Jonathan Wakely 2022-11-22 19:15:53 +00:00
parent 2650b6f914
commit 1cac00d013

View file

@ -29,35 +29,44 @@
int choice;
struct dirent global_dirent;
extern "C" struct dirent* readdir(DIR*)
{
// On some targets dirent::d_name is very small, but the OS allocates
// a trailing char array after the dirent struct. Emulate that here.
union State
{
struct dirent d;
char buf[sizeof(struct dirent) + 16] = {};
};
static State state;
char* d_name = state.buf + offsetof(struct dirent, d_name);
switch (choice)
{
case 1:
global_dirent.d_ino = 999;
state.d.d_ino = 999;
#if defined _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE && defined DT_REG
global_dirent.d_type = DT_REG;
state.d.d_type = DT_REG;
#endif
global_dirent.d_reclen = 0;
std::char_traits<char>::copy(global_dirent.d_name, "file", 5);
state.d.d_reclen = 0;
std::char_traits<char>::copy(d_name, "file", 5);
choice = 0;
return &global_dirent;
return &state.d;
case 2:
global_dirent.d_ino = 111;
state.d.d_ino = 111;
#if defined _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE && defined DT_DIR
global_dirent.d_type = DT_DIR;
state.d.d_type = DT_DIR;
#endif
global_dirent.d_reclen = 60;
std::char_traits<char>::copy(global_dirent.d_name, "subdir", 7);
state.d.d_reclen = 60;
std::char_traits<char>::copy(d_name, "subdir", 7);
choice = 1;
return &global_dirent;
return &state.d;
default:
errno = EIO;
return nullptr;
}
return &global_dirent;
return &state.d;
}
void