From patchwork Mon Jul 8 22:02:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1958123 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Re8jydKn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WHym10Xydz1xpd for ; Tue, 9 Jul 2024 08:02:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9190138471C6 for ; Mon, 8 Jul 2024 22:02:30 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id 42C8B3858D37 for ; Mon, 8 Jul 2024 22:02:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42C8B3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 42C8B3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720476132; cv=none; b=XGiqY8SOvf757Bm3t44sa1YJovBGlKmWGQTwqt1nxpyrIgW6+iUtsT2ngtbWgHD9dIZyMx3cAQjVP38h9QKCwmfi8WzuRS/7HEhB14A+8wPQwNH4L7Q8L45je0WJMblAx5CRRkj2A4qsdy8iwHLhr+STpXFByb+7Zgq9TNI6ksU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720476132; c=relaxed/simple; bh=B07Kb4zNnRxUN8WRZudpA6uD+zHW8gyNgbVs6sqKciM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=eOfHLdncFELNHHjfSgGBYfaaJD2SVoLXkngC7jq2o7I6YkOwjf2BD5LXU3d3VVjpyngOTD2lV+AYio/tRJ+FBTfix9mS5U6RQ8H8DH9LbbGjiMdOK+LmOooog++XiQyYOLU5oZhX3+Vn+4jmL7uh2UfMGygZdf1HF5mJieX/oqI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-70df213542bso2221034a12.3 for ; Mon, 08 Jul 2024 15:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720476126; x=1721080926; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=Wz9ChXbbjc2a8bCJcilVCd1bTb3SjZditCcKLfi8vsI=; b=Re8jydKnJOVQYnj0CIGDQtWWfLm/b781jJUBKKjqxmUmQl78H5X/zyR+1iDhwCFclb SBfMiVieV7ZeBUytK0FE5OQOCdgg35I6E0s48GDQ2bfVytSoQrB5ot+UhhggaiR3yuGT I8Xj/VbAa28EP6iNFlWhgsxzlZQEsxYZrwwdMPn/oKfhSXBd3r5RZDSB3S/qG7kBLUOu J/dKqnb2479MEXzwun8O952hpkMSe6ZqtWOMuQpRLJjSzmLcsYUYQtthvDTF6tpAO5fx /WVdreawa/PZMRBNprptXYkQA/0MKS3DKV8J8AR3Xacuq8DYHFTsAoPXZ6yLngzBpVAx f+IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720476126; x=1721080926; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Wz9ChXbbjc2a8bCJcilVCd1bTb3SjZditCcKLfi8vsI=; b=d4DvuWi3VvqDNPVLY5eqF9tsppG4JOzDRg4T2CFxfPJOzc3kWfUq9fgI3zcxXh7b8R 95IL5bMukShiyVXeOsdJVzDBPjzwxnH1UJkF7HO/JlB0flnsTM8WG5hZHtkU1Ew2An2W X36yOt4Uz6uh3PUXm1Q1wGFE+r9xgMecAn1nXR7SAlTEWdGcJRNUwSJSGlFoEaDaswEW KGVhJtnXidng5hb6Ud4QQllTjRUlSkMi54ErkuwrD44iQe4bLWvoJR9FCYO9ebku7H8d tI0yirpKul1VFWpYl9QWZ/9Jk4iP8YowiS1T6g7Q1dzolMhg57i2Rv7GSZsVztGnBbe7 il8g== X-Gm-Message-State: AOJu0Yz38quHh60TRLuLeQ/RcSUTw9pLzCgIDeEirb87weatgGSZEsOX QWEYPn6g36FfSAQxCt6sHIk3fzwfnDTTcJKmYjBgP980fUTHMhRbBKUjc4evD8OZMg== X-Google-Smtp-Source: AGHT+IEkTYdWnTZ5Sc+U80jGVodjrLJODdA1CmZxFBFDWd6CeizWij0FfpvJKxxhFvDhExg6cVn2fg== X-Received: by 2002:a05:6a20:914f:b0:1c2:8d16:c683 with SMTP id adf61e73a8af0-1c298243926mr676208637.32.1720476126148; Mon, 08 Jul 2024 15:02:06 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.30.228]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-77d680cae23sm325428a12.78.2024.07.08.15.02.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jul 2024 15:02:05 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 0DD8DC0227 for ; Mon, 8 Jul 2024 15:02:02 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH] libio: Check _IO_list_all != NULL [BZ #31963] Date: Mon, 8 Jul 2024 15:02:01 -0700 Message-ID: <20240708220201.3252178-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Spam-Status: No, score=-3019.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: 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~incoming=patchwork.ozlabs.org@sourceware.org When fopen is called after all FILEs have been closed, _IO_list_all is NULL. Check _IO_list_all != NULL in _IO_link_in. This fixes BZ #31963. Signed-off-by: H.J. Lu --- libio/Makefile | 1 + libio/genops.c | 3 ++- libio/tst-fopen1.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 libio/tst-fopen1.c diff --git a/libio/Makefile b/libio/Makefile index 8720381fdc..5f30fe99e2 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -98,6 +98,7 @@ tests = \ tst-fgetc-after-eof \ tst-fgetwc \ tst-fgetws \ + tst-fopen1 \ tst-fopenloc2 \ tst-fputws \ tst-freopen \ diff --git a/libio/genops.c b/libio/genops.c index 994ee9c0b1..99f5e80f20 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -119,7 +119,8 @@ _IO_link_in (struct _IO_FILE_plus *fp) if (_IO_vtable_offset ((FILE *) fp) == 0) { fp->file._prevchain = (FILE **) &_IO_list_all; - _IO_list_all->file._prevchain = &fp->file._chain; + if (_IO_list_all != NULL) + _IO_list_all->file._prevchain = &fp->file._chain; } _IO_list_all = fp; #ifdef _IO_MTSAFE_IO diff --git a/libio/tst-fopen1.c b/libio/tst-fopen1.c new file mode 100644 index 0000000000..5a06cb8b1d --- /dev/null +++ b/libio/tst-fopen1.c @@ -0,0 +1,45 @@ +/* Test fopen after closing all FILEs. + 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 + +static void +close_stdio (void) +{ + fclose (stdin); + fclose (stdout); + fclose (stderr); +} + +static void +final (void) +{ + FILE *f = fopen ("/dev/null", "w"); + fprintf (f, "final\n"); + fclose (f); +} + +static int +do_test (void) +{ + close_stdio (); + final (); + return 0; +} + +#include