From patchwork Wed Nov 23 10:21:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61022 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 464E93852C67 for ; Wed, 23 Nov 2022 10:22:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 464E93852C67 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669198936; bh=nkXVlqK61LT5L9481Z6ZHqR00sPKqqGOInpJzGjfIZM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=W9eQ3lTJoRvamVhvroFcueQDc7oOrTrg+sFUvIPVGLsdJhEWt+gMNfbjmvvUoKDf0 o081/ppWZz8fhXWmDO7tuf8qPAwRB7PO6c+75ANnIpDdNsVw4/gGpJb4ZF9Uhzexld RfJnpyrarWGRlgw1H2zRKIW0Y7Wlpr8tSgmlb1ik= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id F12643853D7F for ; Wed, 23 Nov 2022 10:21:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F12643853D7F Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-68-NGB9-s4fNRuBju8Tm_BfVQ-1; Wed, 23 Nov 2022 05:21:19 -0500 X-MC-Unique: NGB9-s4fNRuBju8Tm_BfVQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0964B299E742; Wed, 23 Nov 2022 10:21:19 +0000 (UTC) Received: from localhost (unknown [10.33.36.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id C76031415114; Wed, 23 Nov 2022 10:21:18 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix unsafe use of dirent::d_name [PR107814] Date: Wed, 23 Nov 2022 10:21:16 +0000 Message-Id: <20221123102116.2194553-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux and sparc-sun-solaris2.11. Pushed to trunk. I'll backport to the branches too. -- >8 -- 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. --- .../filesystem/iterators/error_reporting.cc | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc b/libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc index f005b7d5293..aabed14679c 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc @@ -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::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