NetBSD Problem Report #7761
Received: (qmail 20508 invoked from network); 11 Jun 1999 17:02:07 -0000
Message-Id: <199906111702.TAA11298@calvin.augustsson.net>
Date: Fri, 11 Jun 1999 19:02:05 +0200 (CEST)
From: Lennart Augustsson <augustss@augustsson.net>
Reply-To: augustss@augustsson.net
To: gnats-bugs@gnats.netbsd.org
Subject: getcwd() fails mysteriously
X-Send-Pr-Version: 3.95
>Number: 7761
>Category: kern
>Synopsis: getcwd() fails mysteriously
>Confidential: no
>Severity: serious
>Priority: low
>Responsible: sommerfeld
>State: closed
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Fri Jun 11 10:05:01 +0000 1999
>Closed-Date: Sat Jun 19 18:02:15 +0000 1999
>Last-Modified: Sat Jun 19 18:02:55 +0000 1999
>Originator: Lennart Augustsson
>Release: NetBSD 1.4
>Organization:
>Environment:
System: NetBSD calvin 1.4 NetBSD 1.4 (CALVIN) #0: Sun May 9 18:41:36 CEST 1999 augustss@calvin:/usr/src/sys/arch/i386/compile/CALVIN i386
>Description:
If the size argument to getcwd() is large enough the call fails
with ERANGE. From what I can tell from the man page this is
wrong, and totally unintuitive.
Furthermore, it used to work before getcwd() went into the kernel.
>How-To-Repeat:
Run the following program. With a smaller b (e.g. 1000) it works.
#include <stdio.h>
#include <unistd.h>
#include <err.h>
char b[10000];
main()
{
char *r;
r = getcwd(b, sizeof b);
if (r == 0)
err(1, 0);
printf("path = '%s'\n", b);
}
>Fix:
Dunno.
>Release-Note:
>Audit-Trail:
From: Jaromir Dolecek <dolecek@ics.muni.cz>
To: augustss@augustsson.net
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: kern/7761: getcwd() fails mysteriously
Date: Sat, 12 Jun 1999 17:00:21 +0200 (MEST)
Lennart Augustsson wrote:
> Run the following program. With a smaller b (e.g. 1000) it works.
Well, according to vfs_getcwd.c, the call unconditionally returns
ERANGE when (len > 4*MAXPATHLEN). There seems to be a another bug in
the code -- sys___getcwd() uses only half of the specified length
(vfs_getcwd.c:getcwd_common() is called with limit == len/2).
Seems like algorithm of sys___getcwd() should be changed to:
1. allocate buffer with size min(len, MAXPATHLEN)
2. find the cwd; if the buffer size is not big enough, return ERANGE
3. otherwise, copyout the cwd into supplied buffer.
Also, I'm not sure why getcwd_common() is called with limit == len/2.
Bill Sommerfeld should be the definitive authority about it.
--
Jaromir Dolecek <dolecek@ics.muni.cz> http://www.ics.muni.cz/~dolecek/
"The only way how to get rid temptation is to yield to it." -- Oscar Wilde
Responsible-Changed-From-To: kern-bug-people->sommerfeld
Responsible-Changed-By: sommerfeld
Responsible-Changed-When: Sun Jun 13 11:49:16 PDT 1999
Responsible-Changed-Why:
it's my bug..
From: Bill Sommerfeld <sommerfeld@orchard.arlington.ma.us>
To: augustss@augustsson.net, dolecek@ics.muni.cz
Cc: gnats-bugs@netbsd.org
Subject: Re: kern/7761
Date: Sun, 13 Jun 1999 17:39:43 -0400
A few clarifications:
1) Jaromir's analysis is faulty. The getcwd_common() function takes
two limits: buffer length, and number of directories to traverse
before giving up (for loop detection..).
Each entryname takes up at least 2 characters in the buffer: one for
the name, one for the '/' separator, so an N byte buffer can hold at
most N/2 directory levels.
The full buffer passed in is usable, as a run of the regression test
code in regress/sys/kern/getcwd would clearly show.
2) pathnames longer than MAXPATHLEN are supported for compatibility
with the old getcwd() routine. I suggest changing either the kernel
or userspace to (silently) clip the passed in buffer length to
MAXPATHLEN*4 rather than rewriting it to copy out a component at time
and then returning an offset or some such to the start of the
pathname.
I probably won't be able to deal with this for the next week. if
someone else codes a fix in the meantime, send a patch my way and i'll
take a look at it..
- Bill
State-Changed-From-To: open->closed
State-Changed-By: sommerfeld
State-Changed-When: Sat Jun 19 11:02:15 PDT 1999
State-Changed-Why:
Fixed; kernel now silently rounds down length to MAXPATHLEN*4..
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.