From owner-freebsd-standards@FreeBSD.ORG Fri Jan 24 13:24:39 2014 Return-Path: Delivered-To: freebsd-standards@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2CD47EB5; Fri, 24 Jan 2014 13:24:39 +0000 (UTC) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id E5BD21CA4; Fri, 24 Jan 2014 13:24:38 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 23CFDB8407; Fri, 24 Jan 2014 14:24:36 +0100 (CET) Received: by snail.stack.nl (Postfix, from userid 1677) id 07E2028497; Fri, 24 Jan 2014 14:24:36 +0100 (CET) Date: Fri, 24 Jan 2014 14:24:35 +0100 From: Jilles Tjoelker To: Bryan Drewery Subject: Re: closedir(3) handling NULL Message-ID: <20140124132435.GA90996@stack.nl> References: <20140124014105.GC37334@admin.xzibition.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20140124014105.GC37334@admin.xzibition.com> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: bapt@FreeBSD.org, freebsd-standards@FreeBSD.org X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Jan 2014 13:24:39 -0000 On Thu, Jan 23, 2014 at 07:41:05PM -0600, Bryan Drewery wrote: > I found that Linux handles closedir(NULL) fine and returns EINVAL. POSIX > [1] specifies that EBADF should be returned if "The dirp argument does > not refer to an open directory stream" > [1] http://pubs.opengroup.org/onlinepubs/009696799/functions/closedir.html > I've updated fdclosedir(3) as well to behave the same. > I'll also update the manpage if there is no objection. If you do this, it is to improve compatibility with poorly written software and not for POSIX compliance. POSIX only permits passing null pointers where explicitly specified (e.g. time()); otherwise, passing a null pointer is undefined behaviour like passing any argument outside the required domain. On another note, I don't understand when the condition [EBADF] The dirp argument does not refer to an open directory stream. can actually apply. As far as I understand, only valid open directory streams (opendir() has been called and closedir() has not been called) may be passed to closedir() and other functions. If the pointer is not a null pointer, detecting whether it refers to a valid open directory stream is not possible using common methods (you could maintain a list of directory streams but doing that is against the spirit of C and I am quite sure that POSIX did not intend to require implementations to do that). In the current code in FreeBSD, [EBADF] may also happen if an application closes a directory stream's file descriptor from under it and no other file descriptor is opened in its place. In some cases like the file_name argument to realpath(), NULL is specified to cause an [EINVAL] return; I think we are stuck with that but should not add more such cases. Note that this [EINVAL] return also means that realpath(NULL, p) is valid to do and should not trigger undefined behaviour detectors. -- Jilles Tjoelker