From patchwork Thu Aug 29 16:02:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Merey X-Patchwork-Id: 96713 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 7FB75385ED40 for ; Thu, 29 Aug 2024 16:02:56 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C31853858402 for ; Thu, 29 Aug 2024 16:02:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C31853858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C31853858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724947353; cv=none; b=qBtgD5Q47iBR/cQRy971WnOV43jJ0c7UDybtehgpHHQAcbU2DTf0nfu5gom+k8u3BwZyom+o6R5KaQ9le/+bcwj/i5uPXfQ2i32pBezqmGqncSUXiB0vfBzfyfNuwksapT+9/HwTONW5CEksfmymjXucnZt3mQaiPckLzsdtRHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724947353; c=relaxed/simple; bh=LzoPLQyuLW0rjcfNzNtj+U7LYpOUzAK9ncpsvvZIVzo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qQusPP3VomAyhlNg/HUhZXnZ5XU7BrdDdGN6/e7AyzAlgCkfHSDVI32p3MMEbk+77r5KSYs0B0C46prSTnFh1nSJWsC+3XsVfdM25FKELfUTt9jDl9VFS5wj0dpHyafSA8Coi+DK5cnoxP4ZRlMEntsuLlF3E3UL3EkSheGHrZc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724947351; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tauNypOjM8eZB3AwK9yw+8fSD8CX/+Z3Ri9wuG/bOrE=; b=hoAOqAxGpKUvouluW8l+5uGGPKLMOkBnj0Cayk6PguPZbUeNSaKAhZ4+M04mj1OB4MtYcN cljtlmkBBmP8fm+6cH7JSYiLOMwK2w9W2tcToX9knaaUhCyipCfmmYhiEwbwwgqug+eYzx CAsZ0b8VnecgJ+Ri8ZmrswDNiPyIaOU= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-33-mujVSBCrMJOB34w6TwV7zQ-1; Thu, 29 Aug 2024 12:02:30 -0400 X-MC-Unique: mujVSBCrMJOB34w6TwV7zQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6653F1955BF9 for ; Thu, 29 Aug 2024 16:02:29 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.16.232]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5D06519560A3; Thu, 29 Aug 2024 16:02:28 +0000 (UTC) From: Aaron Merey To: libc-alpha@sourceware.org Cc: Aaron Merey Subject: [PATCH] Test fclose on an unopened file. Date: Thu, 29 Aug 2024 12:02:25 -0400 Message-ID: <20240829160225.2641690-1-amerey@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Add new file libio/tst-fclosed-unopened.c that tests whether fclose on an unopened file returns EOF. fclose returning EOF for unopened files is not part of the external contract but there are dependancies on this behaviour. For example, gnulib's close_stdout in lib/closeout.c. Tested for x86_64. Signed-off-by: Aaron Merey --- libio/Makefile | 1 + libio/tst-fclose-unopened.c | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 libio/tst-fclose-unopened.c diff --git a/libio/Makefile b/libio/Makefile index 6a507b67ea..59f3ee0b7c 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -95,6 +95,7 @@ tests = \ tst-eof \ tst-ext \ tst-ext2 \ + tst-fclose-unopened \ tst-fdopen-seek-failure \ tst-fgetc-after-eof \ tst-fgetwc \ diff --git a/libio/tst-fclose-unopened.c b/libio/tst-fclose-unopened.c new file mode 100644 index 0000000000..2c7cea0b52 --- /dev/null +++ b/libio/tst-fclose-unopened.c @@ -0,0 +1,40 @@ +/* Test using fclose on an unopened file. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Verify that fclose on an unopened file returns EOF. This is not part + of the fclose external contract but there are dependancies on this + behaviour. */ + +static int +do_test (void) +{ + TEST_VERIFY (fclose (stdin) == 0); + + /* Attempt to close the unopened file and verify that EOF is returned. + Calling fclose on a file twice normally causes a use-after-free bug, + however the standard streams are an exception since they are not + deallocated by fclose. */ + TEST_VERIFY (fclose (stdin) == EOF); + + return 0; +} + +#include