From patchwork Fri Dec 22 19:30:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 852539 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=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-88551-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="kxhUfyoO"; 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 3z3JXM6YsDz9s83 for ; Sat, 23 Dec 2017 06:31:15 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=dELUqCyXMdjb7rBQlBVXf2Xao3+nKli 6a71gnoyalWdZsoTYrhnPidZB6jwx+8HoRf48Y03YLxDquIAlagqMdr4nDrmN4HH eALBrLEJoDOxxvQkGkT11ADa1EdApgG9HNV5vk7BNtLQ5usGiMjAeBNoIWdJ6zft 4OhUJ7Ss63mY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=NcLdsvh/ZO0WPXCya5iNIkPrhx8=; b=kxhUf yoOTgDmZkcGEb7aJWjk/OvGZGhguQIJOLwXeFhHzJIo7A5iX5Bn7I1anlC4/QfTb JheMMgAAsu1cOxNAJtMtgWn68F4oitc/epi2g36tclAyh3G4eswkOWgxnk0hAYLD oDLUWN4UcOHQpAAix7D58P8RMqiyJMcqTJsT3k= Received: (qmail 127789 invoked by alias); 22 Dec 2017 19:31:09 -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 126959 invoked by uid 89); 22 Dec 2017 19:31:08 -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= X-HELO: hall.aurel32.net From: Aurelien Jarno To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH v2 1/3] tst-realloc: do not check for errno on success [BZ #22611] Date: Fri, 22 Dec 2017 20:30:59 +0100 Message-Id: <20171222193101.3396-2-aurelien@aurel32.net> In-Reply-To: <20171222193101.3396-1-aurelien@aurel32.net> References: <20171222193101.3396-1-aurelien@aurel32.net> POSIX explicitly says that applications should check errno only after failure, so the errno value can be clobbered on success as long as it is not set to zero. Changelog: [BZ #22611] * malloc/tst-realloc.c (do_test): Remove the test checking that errno is unchanged on success. --- ChangeLog | 6 ++++++ malloc/tst-realloc.c | 4 ---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index b296ec0ad6..c8157a4e43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-12-22 Aurelien Jarno + + [BZ #22611] + * malloc/tst-realloc.c (do_test): Remove the test checking that errno + is unchanged on success. + 2017-12-22 Eric Blake Avoid gcc warnings on cygwin diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c index 31a58bd026..d942c6e536 100644 --- a/malloc/tst-realloc.c +++ b/malloc/tst-realloc.c @@ -66,10 +66,6 @@ do_test (void) if (p == NULL) merror ("realloc (NULL, 10) failed."); - /* errno should be clear on success (POSIX). */ - if (p != NULL && save != 0) - merror ("errno is set but should not be"); - free (p); p = calloc (20, 1); From patchwork Fri Dec 22 19:31:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 852540 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=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-88552-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="KpcNMEbf"; 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 3z3JXY2cYRz9s7n for ; Sat, 23 Dec 2017 06:31:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=R5Xim7CPSmWoezia37wTXwu+njsRCuE 5yT0E33y50tB4EPSuMY5VH3rbazD+IQKT84I3D/jKmV+8NtM9U5U0vYKkBK5qE5t hmL6J6C1rZLQyoKTzFRNs0GcVOd48FgFHnSufn9aiY08yP/UcClNk+YMWrbxDIkY GMdszeT8DCXo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=e3kKp4yLtqnpTaz0x/CzAvyR4qo=; b=KpcNM EbfOE1ALgjGbLPaJXUX0uL+EhueZqYBK9gC4A3qiZYyoKrNeCFeUYz0KxNru/UZP DbH9JJFnRVwd8wV/20wRpgevOIGwovKVyoNf4akas98xYQwCEeKpNrhKZaagrev3 927nUVl9GHNZMzIZoxMv+qXox5R6rJCRAKwJeg= Received: (qmail 127937 invoked by alias); 22 Dec 2017 19:31:09 -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 127491 invoked by uid 89); 22 Dec 2017 19:31:09 -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= X-HELO: hall.aurel32.net From: Aurelien Jarno To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH v2 2/3] manual: clarify errno value on success [BZ #22615] Date: Fri, 22 Dec 2017 20:31:00 +0100 Message-Id: <20171222193101.3396-3-aurelien@aurel32.net> In-Reply-To: <20171222193101.3396-1-aurelien@aurel32.net> References: <20171222193101.3396-1-aurelien@aurel32.net> The current glibc manual is ambiguous about the errno value on success and suggests that it is left unchanged. Some functions might and sometimes do change the errno value, however they never set it to 0. This patch from Zack Weinberg clarifies this section of the manual. Changelog: [BZ #22615] * manual/errno.texi (Checking for Errors): Explicitly say that errno might be set on success. --- ChangeLog | 6 ++++++ manual/errno.texi | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index c8157a4e43..8368b3006d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-12-22 Zack Weinberg + + [BZ #22615] + * manual/errno.texi (Checking for Errors): Explicitly say that errno + might be set on success. + 2017-12-22 Aurelien Jarno [BZ #22611] diff --git a/manual/errno.texi b/manual/errno.texi index 3e0b862c4e..73272fd884 100644 --- a/manual/errno.texi +++ b/manual/errno.texi @@ -47,20 +47,20 @@ However, a properly written signal handler saves and restores the value of @code{errno}, so you generally do not need to worry about this possibility except when writing signal handlers. -The initial value of @code{errno} at program startup is zero. Many -library functions are guaranteed to set it to certain nonzero values -when they encounter certain kinds of errors. These error conditions are -listed for each function. These functions do not change @code{errno} -when they succeed; thus, the value of @code{errno} after a successful -call is not necessarily zero, and you should not use @code{errno} to -determine @emph{whether} a call failed. The proper way to do that is -documented for each function. @emph{If} the call failed, you can -examine @code{errno}. - -Many library functions can set @code{errno} to a nonzero value as a -result of calling other library functions which might fail. You should -assume that any library function might alter @code{errno} when the -function returns an error. +The initial value of @code{errno} at program startup is zero. In many +cases, when a library function encounters an error, it will set +@code{errno} to a non-zero value to indicate what specific error +condition occurred. The documentation for each function lists the +error conditions that are possible for that function. Not all library +functions use this mechanism; some return an error code directly, +instead. + +@strong{Warning:} Many library functions may set @code{errno} to some +meaningless non-zero value even if they did not encounter any errors, +and even if they return error codes directly. Therefore, it is +usually incorrect to check @emph{whether} an error occurred by +inspecting the value of @code{errno}. The proper way to check for +error is documented for each function. @strong{Portability Note:} @w{ISO C} specifies @code{errno} as a ``modifiable lvalue'' rather than as a variable, permitting it to be From patchwork Fri Dec 22 19:31:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 852542 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=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-88554-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="KXJ66zh4"; 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 3z3JXv055xz9s7f for ; Sat, 23 Dec 2017 06:31:42 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=nVzh80XALY1wfnXgRvNrDEWnCmvahE1 Q5bucerHIcOUlqFsgVfpKyImgYKMKhd/8cYOpjinWVtNYX/UaFcyj50MqMuFa5jR QADRq2azxK4gfQiaEtUA4hN4l11prjupm5RsPZvlcgMWT5bq1vBsLc7kXxi3Jf6I AiMqQSZBsa70= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=vRPGbVdXk2nH2kIodF1kZRfzAYU=; b=KXJ66 zh4zNp7GEfc/RBsHYqxwztM2H6ndvUB1mQN2+o6/NHISoYb0npgZyom+i9hOticc ULJ39odX95QsN85v29TbdcR4SWBpJfd2w/oGg3jbRbaSHJaIS4SocJd+ZLpaQNLb V7fqiSbbZATyMCHefd6V2cZPQYctTaN5ORCE98= Received: (qmail 128148 invoked by alias); 22 Dec 2017 19:31:09 -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 127369 invoked by uid 89); 22 Dec 2017 19:31:08 -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= X-HELO: hall.aurel32.net From: Aurelien Jarno To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH v2 3/3] scandir: fix wrong assumption about errno [BZ #17804] Date: Fri, 22 Dec 2017 20:31:01 +0100 Message-Id: <20171222193101.3396-4-aurelien@aurel32.net> In-Reply-To: <20171222193101.3396-1-aurelien@aurel32.net> References: <20171222193101.3396-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. Improve comments. --- ChangeLog | 6 ++++++ dirent/scandir-tail.c | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8368b3006d..713c5ade6d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-12-22 Aurelien Jarno + + [BZ #17804] + * dirent/scandir-tail.c (SCANDIR_TAIL): Move __set_errno (0) at the + end of the loop. Improve comments. + 2017-12-22 Zack Weinberg [BZ #22615] diff --git a/dirent/scandir-tail.c b/dirent/scandir-tail.c index 068c644c4e..927791fad0 100644 --- a/dirent/scandir-tail.c +++ b/dirent/scandir-tail.c @@ -53,16 +53,14 @@ SCANDIR_TAIL (DIR *dp, { int selected = (*select) (d); - /* The SELECT function might have changed errno. It was - zero before and it need to be again to make the later - tests work. */ + /* The SELECT function might have set errno to non-zero on + success. It was zero before and it need to be again to + make the later tests work. */ __set_errno (0); 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 set errno to non-zero on success. It was zero before + and it need to be again to make the latter tests work. */ + __set_errno (0); } if (__glibc_likely (errno == 0))