From patchwork Thu Dec 21 22:54:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 25067 Received: (qmail 50274 invoked by alias); 21 Dec 2017 22:55:06 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 50178 invoked by uid 89); 21 Dec 2017 22:55:05 -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, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=Hx-languages-length:1662 X-HELO: hall.aurel32.net From: Aurelien Jarno To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH 3/3] scandir: fix wrong assumption about errno [BZ #17804] Date: Thu, 21 Dec 2017 23:54:53 +0100 Message-Id: <20171221225453.13158-4-aurelien@aurel32.net> In-Reply-To: <20171221225453.13158-1-aurelien@aurel32.net> References: <20171221225453.13158-1-aurelien@aurel32.net> malloc and realloc may set errno to ENOMEM even if they are successful. The scandir code wrongly assume that they do not change errno, this causes scandir to fail with ENOMEM even if malloc succeed. The code already handles that readdir might set errno by calling __set_errno (0) to clear the error. Move that part at the end of the loop to also take malloc and realloc into account. Changelog: [BZ #17804] * dirent/scandir-tail.c (SCANDIR_TAIL): Move __set_errno (0) at the end of the loop. Reviewed-by: Carlos O'Donell --- ChangeLog | 6 ++++++ dirent/scandir-tail.c | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7bf30d27ef..31fddf5794 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-12-21 Aurelien Jarno + + [BZ #17804] + * dirent/scandir-tail.c (SCANDIR_TAIL): Move __set_errno (0) at the + end of the loop. + 2017-12-21 Aurelien Jarno [BZ #22615] diff --git a/dirent/scandir-tail.c b/dirent/scandir-tail.c index 068c644c4e..00fe9ef030 100644 --- a/dirent/scandir-tail.c +++ b/dirent/scandir-tail.c @@ -61,8 +61,6 @@ SCANDIR_TAIL (DIR *dp, if (!selected) continue; } - else - __set_errno (0); if (__glibc_unlikely (c.cnt == vsize)) { @@ -81,6 +79,11 @@ SCANDIR_TAIL (DIR *dp, if (vnew == NULL) break; v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize); + + /* Ignore errors from readdir, malloc or realloc. These functions + might have changed errno. It was zero before and it need to be + again to make the latter tests work. */ + __set_errno (0); } if (__glibc_likely (errno == 0))