From owner-freebsd-bugs@FreeBSD.ORG Thu Mar 20 05:10:02 2008 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 14550106564A for ; Thu, 20 Mar 2008 05:10:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id DE2EE8FC14 for ; Thu, 20 Mar 2008 05:10:01 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id m2K5A1Hk094122 for ; Thu, 20 Mar 2008 05:10:01 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id m2K5A1O7094121; Thu, 20 Mar 2008 05:10:01 GMT (envelope-from gnats) Resent-Date: Thu, 20 Mar 2008 05:10:01 GMT Resent-Message-Id: <200803200510.m2K5A1O7094121@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Eric Schuele Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C7E0B106564A for ; Thu, 20 Mar 2008 05:09:58 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id AF48A8FC21 for ; Thu, 20 Mar 2008 05:09:58 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m2K59vlT036246 for ; Thu, 20 Mar 2008 05:09:57 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.2/8.14.1/Submit) id m2K59vIm036244; Thu, 20 Mar 2008 05:09:57 GMT (envelope-from nobody) Message-Id: <200803200509.m2K59vIm036244@www.freebsd.org> Date: Thu, 20 Mar 2008 05:09:57 GMT From: Eric Schuele To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/121897: [PATCH] realpath(3) segmentation fault X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Mar 2008 05:10:02 -0000 >Number: 121897 >Category: bin >Synopsis: [PATCH] realpath(3) segmentation fault >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Mar 20 05:10:01 UTC 2008 >Closed-Date: >Last-Modified: >Originator: Eric Schuele >Release: FreeBSD 7.0-STABLE >Organization: >Environment: FreeBSD fangorn.nxdomain.org 7.0-STABLE FreeBSD 7.0-STABLE #0: Wed Mar 12 12:50:03 CDT 2008 root@fangorn.nxdomain.org:/usr/obj/usr/src/sys/CUSTOM i386 >Description: The following code seems to behave a little better on linux than FreeBSD 7.0. #include #include main () { char buf[1024]; char *s = realpath(NULL, buf); } On FreeBSD I get a segmentation fault, while linux gracefully sets errno and goes on with its life. Do note that changing the above NULL to something a little more sane of course works. The man page states: "All but the last component of pathname must exist when realpath() is called." So, on one hand maybe I was warned. Upon looking at the FreeBSD implementation of realpath(3) its obvious that a null pointer is not acceptable. Now below is the first few lines of GNU libc's realpath implementation: if (name == NULL) { /* As per Single Unix Specification V2 we must return an error if either parameter is a null pointer. We extend this to allow the RESOLVED parameter to be NULL in case the we are expected to allocate the room for the return value. */ __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { /* As per Single Unix Specification V2 we must return an error if the name argument points to an empty string. */ __set_errno (ENOENT); return NULL; } Seems reasonable we should have a similar approach since SUS requests as much. I have attached a patch which fixes the issue. >How-To-Repeat: compile and run the following prog: #include #include main () { char buf[1024]; char *s = realpath(NULL, buf); } >Fix: See attached patch. Patch attached with submission follows: --- realpath.c.0 2008-03-19 23:50:39.000000000 -0500 +++ realpath.c 2008-03-19 23:50:57.000000000 -0500 @@ -59,6 +59,15 @@ int serrno, slen; char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; + if (path == NULL) { + errno = EINVAL; + return (NULL); + } + if (path[0] == '\0') { + errno = ENOENT; + return (NULL); + } + serrno = errno; symlinks = 0; if (path[0] == '/') { >Release-Note: >Audit-Trail: >Unformatted: