freebsd-dev/lib/libc
Bruce Evans b01f0b7d76 Obtained from: 1.1.5
getcwd() has two off-by-one bugs in FreeBSD-2.0:

1. getcwd(buf, size) fails when the size is just large enough.
2. getcwd(buf + 1, 1) incorrectly succeeds when the current directory
   is "/".  buf[0] and buf[2] are clobbered.

(I modified Bruce's original patch to return the proper error code
[ERANGE] in the case of #2, but otherwise... -DG)

This program demonstrates the bug:

---
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    char buf[5];
    int errors;

    errors = 0;
    if (chdir("/tmp") != 0) {
        perror("chdir");
        abort();
    }
    if (getcwd(buf, 5) == NULL) {
        perror("oops, getcwd failed for buffer size = size required");
        ++errors;
    }
    if (chdir("/") != 0) {
        perror("chdir");
        abort();
    }
    buf[0] = 0;
    buf[2] = 1;
    if (getcwd(buf + 1, 1) != NULL) {
        fprintf(stderr,
                "oops, getcwd succeeded for buffer size = one too small\n");
        ++errors;
    }
    if (buf[0] != 0) {
        fprintf(stderr,
                "oops, getcwd scribbled on memory before start of buffer\n");
        ++errors;
    }
    if (buf[2] != 1) {
        fprintf(stderr,
                "oops, getcwd scribbled on memory after end of buffer\n");
        ++errors;
    }
    exit(errors == 0 ? 0 : 1);
}
1994-12-12 01:29:13 +00:00
..
amd64
compat-43
db Added the mpool.3 manpage to the installed manpages. It is referred from the 1994-12-11 22:03:05 +00:00
gen Obtained from: 1.1.5 1994-12-12 01:29:13 +00:00
gmon
i386
locale
net In _gethostbydnsaddr(), force RES_RECURSE into _res.options. This is 1994-12-01 22:25:38 +00:00
quad
regex
rpc Comment out the man page of rstat.1 from Makefile.inc. There is no rstat 1994-12-11 22:08:10 +00:00
stdio
stdlib
stdtime
string Obtained from: 386BSD-0.1 patchkit; also fixed in 1.1.5 1994-12-12 01:23:33 +00:00
sys
xdr
yp
Makefile