From patchwork Thu Sep 5 10:12:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1981186 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=C+dBleMO; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WzwDL2r1jz1yfv for ; Thu, 5 Sep 2024 20:13:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3B72438654A1 for ; Thu, 5 Sep 2024 10:13:20 +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 54A82386587A for ; Thu, 5 Sep 2024 10:12:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54A82386587A 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 54A82386587A 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=1725531178; cv=none; b=PVVJPEKZhe//plEpa4KoSj0v3gl455Lc/C358fMuue903lpoTZrmcqte7Scsn9PPL7hMrX2afEwlSXr0vMmb9R+WkbHRrOmPy7GrIxd18TGWqUQqkSAToUoQZQjdEzOdRmrHmb7e1nfT5COSdNsfePV7M5gBtptHddUpAe5UQ70= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725531178; c=relaxed/simple; bh=zhs5w0hj1xpmICy4pq5rImGI0r3Rdp4vwMp0Ry6tVqg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HUzFsSOPa7qQy0F/6bE0BKyfvi/8CSf6jJIQ3ccj/N6tqD2WUrcmQM9fiwhx7uT9rFWVH4yPHoLmEeXmMbh451wE6mF/9lvh4yF22MrBIbEZKudvJhxXyGIfYYetiVzw54EQcRshum5r4BtNynUsgxufvLiJFRpR6/eAQjDUA48= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725531175; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=LL7xsBsGMb+xQz7rtPT3IPkZrbJckIyMBrKOfOmNbVE=; b=C+dBleMOYkPDu5+tf4XrjJssukkohWBEjDdD+7yKp3yzhpre+1Xbcd6mAlbaMQwYRH2oWU 3fi0k3lYKO4iLLp/wQEPbYRss6hxrkKABxBIbuWD32DETlTS2GKphuqkejsj08UR8q5DKU vadHbgPatGA8ePDMqJ2JZxYdhGViEYA= Received: from mx-prod-mc-05.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-147-_MqZJ_ISMXSvvnp46DV9Qg-1; Thu, 05 Sep 2024 06:12:53 -0400 X-MC-Unique: _MqZJ_ISMXSvvnp46DV9Qg-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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B1E9F1955F2E for ; Thu, 5 Sep 2024 10:12:52 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.26]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E523519560AD for ; Thu, 5 Sep 2024 10:12:51 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH COMMITTED] support: Add Date: Thu, 05 Sep 2024 12:12:48 +0200 Message-ID: <87o752fly7.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) 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=-9.3 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, RCVD_IN_SBL_CSS, 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~incoming=patchwork.ozlabs.org@sourceware.org I botched patch split, so this is what I committed. The combination of the two patches is identical to what DJ reviewed. ---8<--- Use static functions for readdir/readdir_r, so that -D_FILE_OFFSET_BITS=64 does not improperly redirect calls to the wrong implementation. Reviewed-by: DJ Delorie --- support/Makefile | 6 +++ support/support_readdir_check.c | 30 ++++++++++++++ support/support_readdir_r_check.c | 35 ++++++++++++++++ support/tst-xdirent.c | 76 ++++++++++++++++++++++++++++++++++ support/xclosedir.c | 28 +++++++++++++ support/xdirent.h | 86 +++++++++++++++++++++++++++++++++++++++ support/xfdopendir.c | 30 ++++++++++++++ support/xopendir.c | 30 ++++++++++++++ 8 files changed, 321 insertions(+) base-commit: b09a520bb6d98d465818aadfd0641751ce824053 diff --git a/support/Makefile b/support/Makefile index 26bd3d38e4..8fb4d2c500 100644 --- a/support/Makefile +++ b/support/Makefile @@ -77,6 +77,8 @@ libsupport-routines = \ support_quote_blob \ support_quote_blob_wide \ support_quote_string \ + support_readdir_check \ + support_readdir_r_check \ support_record_failure \ support_run_diff \ support_select_modifies_timeout \ @@ -119,6 +121,7 @@ libsupport-routines = \ xclock_settime_time64 \ xclone \ xclose \ + xclosedir \ xconnect \ xcopy_file_range \ xdlfcn \ @@ -126,6 +129,7 @@ libsupport-routines = \ xdup2 \ xfchmod \ xfclose \ + xfdopendir \ xfgets \ xfopen \ xfork \ @@ -147,6 +151,7 @@ libsupport-routines = \ xmunmap \ xnewlocale \ xopen \ + xopendir \ xpipe \ xpoll \ xposix_memalign \ @@ -331,6 +336,7 @@ tests = \ tst-test_compare_string \ tst-test_compare_string_wide \ tst-timespec \ + tst-xdirent \ tst-xreadlink \ tst-xsigstack \ # tests diff --git a/support/support_readdir_check.c b/support/support_readdir_check.c new file mode 100644 index 0000000000..5687004276 --- /dev/null +++ b/support/support_readdir_check.c @@ -0,0 +1,30 @@ +/* Error-checking helper for xreaddir, xreaddir64. + 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 + +void * +support_readdir_check (const char *name, void *result, int saved_errno) +{ + if (result == NULL && errno != 0) + FAIL_EXIT1 ("%s: %m", name); + errno = saved_errno; + return result; +} diff --git a/support/support_readdir_r_check.c b/support/support_readdir_r_check.c new file mode 100644 index 0000000000..6bbb0d0b32 --- /dev/null +++ b/support/support_readdir_r_check.c @@ -0,0 +1,35 @@ +/* Error-checking helper for xreaddir_r, xreaddir64_r. + 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 + +int +support_readdir_r_check (const char *name, int result, void *buf, void *ptr) +{ + if (result != 0) + { + errno = result; + FAIL_EXIT1 ("%s: %m", name); + } + if (buf != ptr) + FAIL_EXIT1 ("%s: buffer pointer and returned pointer differ: %p != %p", + name, buf, ptr); + return result; +} diff --git a/support/tst-xdirent.c b/support/tst-xdirent.c new file mode 100644 index 0000000000..642483165a --- /dev/null +++ b/support/tst-xdirent.c @@ -0,0 +1,76 @@ +/* Compile test for error-checking wrappers for + 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 +#include +#include + +static int +do_test (void) +{ + { + DIR *d = xopendir ("."); + struct dirent *e = xreaddir (d); + /* Assume that the "." special entry always comes first. */ + TEST_COMPARE_STRING (e->d_name, "."); + while (xreaddir (d) != NULL) + ; + xclosedir (d); + } + + { + DIR *d = xopendir ("."); + struct dirent64 *e = xreaddir64 (d); + TEST_COMPARE_STRING (e->d_name, "."); + while (xreaddir64 (d) != NULL) + ; + xclosedir (d); + } + + /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ + DIAG_PUSH_NEEDS_COMMENT; + DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + + { + DIR *d = xopendir ("."); + struct dirent buf = { 0, }; + TEST_VERIFY (xreaddir_r (d, &buf)); + TEST_COMPARE_STRING (buf.d_name, "."); + while (xreaddir_r (d, &buf)) + ; + xclosedir (d); + } + + { + DIR *d = xopendir ("."); + struct dirent64 buf = { 0, }; + TEST_VERIFY (xreaddir64_r (d, &buf)); + TEST_COMPARE_STRING (buf.d_name, "."); + while (xreaddir64_r (d, &buf)) + ; + xclosedir (d); + } + + DIAG_POP_NEEDS_COMMENT; + + return 0; +} + +#include diff --git a/support/xclosedir.c b/support/xclosedir.c new file mode 100644 index 0000000000..b490df5598 --- /dev/null +++ b/support/xclosedir.c @@ -0,0 +1,28 @@ +/* Error-checking wrapper for closedir. + 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 + +void +xclosedir (DIR *dir) +{ + if (closedir (dir) != 0) + FAIL_EXIT1 ("closedir: %m"); +} diff --git a/support/xdirent.h b/support/xdirent.h new file mode 100644 index 0000000000..8465d70ec1 --- /dev/null +++ b/support/xdirent.h @@ -0,0 +1,86 @@ +/* Error-checking wrappers for + 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 + . */ + +#ifndef SUPPORT_XDIRENT_H +#define SUPPORT_XDIRENT_H + +#include +#include +#include +#include +#include + +__BEGIN_DECLS + +DIR *xopendir (const char *path); +DIR *xfdopendir (int fd); +void xclosedir (DIR *); + +void *support_readdir_check (const char *, void *, int); + +static __attribute__ ((unused)) struct dirent * +xreaddir (DIR *stream) +{ + int saved_errno = errno; + errno = 0; + struct dirent *result = readdir (stream); + return support_readdir_check ("readdir", result, saved_errno); +} + +static __attribute__ ((unused)) struct dirent64 * +xreaddir64 (DIR *stream) +{ + int saved_errno = errno; + errno = 0; + struct dirent64 *result = readdir64 (stream); + return support_readdir_check ("readdir64", result, saved_errno); +} + +/* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ +DIAG_PUSH_NEEDS_COMMENT; +DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + +int support_readdir_r_check (const char *, int, void *, void *); + +static __attribute__ ((unused)) bool +xreaddir_r (DIR *stream, struct dirent *buf) +{ + struct dirent *ptr; + int ret = readdir_r (stream, buf, &ptr); + if (ret == 0 && ptr == NULL) + return false; + support_readdir_r_check ("readdir_r", ret, buf, ptr); + return true; +} + +static __attribute__ ((unused)) bool +xreaddir64_r (DIR *stream, struct dirent64 *buf) +{ + struct dirent64 *ptr; + int ret = readdir64_r (stream, buf, &ptr); + if (ret == 0 && ptr == NULL) + return false; + support_readdir_r_check ("readdir64_r", ret, buf, ptr); + return true; +} + +DIAG_POP_NEEDS_COMMENT; + +__END_DECLS + +#endif /* SUPPORT_XDIRENT_H */ diff --git a/support/xfdopendir.c b/support/xfdopendir.c new file mode 100644 index 0000000000..d881d28c73 --- /dev/null +++ b/support/xfdopendir.c @@ -0,0 +1,30 @@ +/* Error-checking wrapper for fdopendir. + 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 + +DIR * +xfdopendir (int fd) +{ + DIR *result = fdopendir (fd); + if (result == NULL) + FAIL_EXIT1 ("fdopendir (%d): %m", fd); + return result; +} diff --git a/support/xopendir.c b/support/xopendir.c new file mode 100644 index 0000000000..e4aee07fee --- /dev/null +++ b/support/xopendir.c @@ -0,0 +1,30 @@ +/* Error-checking wrapper for opendir. + 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 + +DIR * +xopendir (const char *path) +{ + DIR *result = opendir (path); + if (result == NULL) + FAIL_EXIT1 ("opendir (\"%s\"): %m", path); + return result; +}