From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id DD20A3898C69; Mon, 9 May 2022 12:02:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD20A3898C69 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9969] libstdc++: Fix test that fails on Solaris [PR104731] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 44cc3c94460de20d9ec51759d4279ac128be9dc8 X-Git-Newrev: 6190ebb07cdc2de339c19b236b67bc4106d2bb54 Message-Id: <20220509120244.DD20A3898C69@sourceware.org> Date: Mon, 9 May 2022 12:02:44 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 12:02:45 -0000 https://gcc.gnu.org/g:6190ebb07cdc2de339c19b236b67bc4106d2bb54 commit r11-9969-g6190ebb07cdc2de339c19b236b67bc4106d2bb54 Author: Jonathan Wakely Date: Fri May 6 14:31:06 2022 +0100 libstdc++: Fix test that fails on Solaris [PR104731] On Solaris the dirent::d_name member is a single char, causing this test to fail with warnings about buffer overflow. Change the test to use a union with additional space for writing a string to the d_name member. libstdc++-v3/ChangeLog: PR libstdc++/104731 * testsuite/27_io/filesystem/iterators/error_reporting.cc: Use a trailing char array as storage for dirent::d_name. (cherry picked from commit aa8bdfee1db818b9a56908ab0197ff02c54bf281) Diff: --- .../27_io/filesystem/iterators/error_reporting.cc | 35 ++++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/libstdc++-v3/testsuite/27_io/filesystem/iterators/error_reporting.cc b/libstdc++-v3/testsuite/27_io/filesystem/iterators/error_reporting.cc index 1f297a731a3..1c8ea4c9249 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/iterators/error_reporting.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/iterators/error_reporting.cc @@ -28,35 +28,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::copy(global_dirent.d_name, "file", 5); + state.d.d_reclen = 0; + std::char_traits::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::copy(global_dirent.d_name, "subdir", 7); + state.d.d_reclen = 60; + std::char_traits::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