From patchwork Mon May 14 18:57:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 913195 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-477667-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="H1qde2u5"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40l91z6tz0z9s1R for ; Tue, 15 May 2018 04:57:58 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=kxa2DUWMV8IHS2AdDrnw3XG8jJ2bmEkssrzaI3W9ilJAEf1AsMUe4 pVcopqSoGf5Kf3SrohSxGI7CVmyEnUnEoi3A2b/Pg/ulLC4PnaKJjof2I2eAQhj/ J04/osfbV/Yu81Jije9GDSeoBeU8La/uf26Q+408tnDZY6A4rvF/8s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=N4L6supIn60Iz9tIKEtW8HBHXpM=; b=H1qde2u5IUEGK2qhn26M DU/g2fjPJSYe/r3rjpMEJnVbu0X3N748xwVaceoeUyBD0g1bELsrr7UhSfj99P1v gXetOs3b5ANrzAA/+qzCEq634jUgfocj5SpuHMHo0YfQp/b3WMqP2vY9OaaShtjU y3e3sOp89CGdWavrs3gnIqU= Received: (qmail 12827 invoked by alias); 14 May 2018 18:57:44 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 11472 invoked by uid 89); 14 May 2018 18:57:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=cvt, lx, UD:tcc, 1109 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 14 May 2018 18:57:41 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 80CFE3180130; Mon, 14 May 2018 18:57:40 +0000 (UTC) Received: from localhost (unknown [10.33.36.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E6D61995D; Mon, 14 May 2018 18:57:40 +0000 (UTC) Date: Mon, 14 May 2018 19:57:39 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/81256 fix exception handling in basic_filebuf::close Message-ID: <20180514185739.GA2078@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) As explained in the PR, we were incorrectly swallowing exceptions from basic_filebuf::close(). They should propagate from that function, but still be swallowed in the destructor. PR libstdc++/81256 * include/bits/fstream.tcc (basic_filebuf::close): Do not swallow exceptions from _M_terminate_output(). * include/std/fstream (basic_filebuf::~basic_filebuf): Swallow any exceptions from close(). * testsuite/27_io/basic_filebuf/close/81256.cc: New. Tested powerp64le-linux, committed to trunk. commit adcfbaa7023dc8d2d5cfa0b6971a99d0ed33a1f5 Author: Jonathan Wakely Date: Mon May 14 17:13:05 2018 +0100 PR libstdc++/81256 fix exception handling in basic_filebuf::close PR libstdc++/81256 * include/bits/fstream.tcc (basic_filebuf::close): Do not swallow exceptions from _M_terminate_output(). * include/std/fstream (basic_filebuf::~basic_filebuf): Swallow any exceptions from close(). * testsuite/27_io/basic_filebuf/close/81256.cc: New. diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc index f23ff7af4eb..08cf189ee06 100644 --- a/libstdc++-v3/include/bits/fstream.tcc +++ b/libstdc++-v3/include/bits/fstream.tcc @@ -239,13 +239,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (!_M_terminate_output()) __testfail = true; } - __catch(__cxxabiv1::__forced_unwind&) + __catch(...) { _M_file.close(); __throw_exception_again; } - __catch(...) - { __testfail = true; } } if (!_M_file.close()) diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream index 3a5895d68b0..05661d9d58f 100644 --- a/libstdc++-v3/include/std/fstream +++ b/libstdc++-v3/include/std/fstream @@ -244,7 +244,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ virtual ~basic_filebuf() - { this->close(); } + { + __try + { this->close(); } + __catch(...) + { } + } #if __cplusplus >= 201103L basic_filebuf& operator=(const basic_filebuf&) = delete; diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/close/81256.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/close/81256.cc new file mode 100644 index 00000000000..aef7364526b --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/close/81256.cc @@ -0,0 +1,109 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include +#include + +struct E : std::runtime_error +{ + E() : runtime_error("") { } +}; + +struct Cvt : std::codecvt +{ + explicit Cvt(size_t refs) : codecvt(refs) { } + + mutable int exceptions_thrown = 0; + +private: + int + do_encoding() const noexcept override + { return -1; } + + bool + do_always_noconv() const noexcept override + { return false; } + + result + do_unshift(state_type&, char*, char*, char*&) const override + { + ++exceptions_thrown; + throw E(); + } +}; + +struct filebuf : std::basic_filebuf +{ + explicit filebuf(Cvt* c) + { + std::locale loc(std::locale::classic(), c); + imbue(loc); + } +}; + +void +test01() +{ + // This facet needs to still be valid when ~basic_filebuf runs: + Cvt conv{1}; + { + filebuf fb(&conv); + fb.open("output.txt", std::wios::out); + fb.sputn(L"x", 1); + + bool caught = false; + try + { + /* [filebuf.members] p7: If one of these calls throws an exception, + * the exception is caught and rethrown after closing the file. */ + fb.close(); + } + catch (const E&) + { + caught = true; + } + VERIFY( conv.exceptions_thrown == 1 ); + VERIFY( caught ); + } + VERIFY( conv.exceptions_thrown == 1 ); +} + +void +test02() +{ + // This facet needs to still be valid when ~basic_filebuf runs: + Cvt conv{1}; + { + filebuf fb(&conv); + fb.open("output.txt", std::wios::out); + fb.sputn(L"x", 1); + /* [filebuf.cons] p5: If an exception occurs during the destruction + * of the object, including the call to close(), the exception is + * caught but not rethrown. */ + } + VERIFY( conv.exceptions_thrown == 1 ); +} + +int +main() +{ + test01(); + test02(); +}