Subject: kern/20762: close() on bogus FDs should return EBADF
To: None <gnats-bugs@gnats.netbsd.org>
From: None <stephenm@employees.org>
List: netbsd-bugs
Date: 03/16/2003 13:58:52
>Number:         20762
>Category:       kern
>Synopsis:       close() on bogus FDs should return EBADF
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Mar 16 20:28:00 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Stephen Ma
>Release:        NetBSD 1.6P 2003-03-07
>Organization:
	People's Front for the correct spelling of the word "Organisation"
>Environment:
System: NetBSD whitewater.local 1.6P NetBSD 1.6P (WHITEWATER) #2: Sat Mar 8 10:15:14 PST 2003 stephenm@whitewater.local:/v1/netbsd/obj/src/sys/arch/i386/compile/WHITEWATER i386
Architecture: i386
Machine: i386
kern_descrip.c:
     $NetBSD: kern_descrip.c,v 1.104 2003/03/01 09:19:53 yamt Exp $
>Description:
sys_close() incorrectly returns NULL when it's passed a file
descriptor outside of the range of create descriptors. It should
return EBADF.
>How-To-Repeat:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int
close_it(int fd) {
	if (close(fd)) {
		return errno;
	} else {
		return 0;
	}
}

int
main(int argc, char **argv)
{
	int ret;

	if (!(ret = close_it(3))) {
		printf("fail: close(3) returned 0\n");
	} else {
		printf("pass: close(3) return %d\n", ret);
	}

	if (!(ret = close_it(7777))) {
		printf("fail: close(7777) returned 0\n");
	} else {
		printf("pass: close(7777) return %d\n", ret);
	}
	
	return 0;
}

>Fix:
--- /v1/netbsd/src/sys/kern/kern_descrip.c	2003-03-01 03:04:46.000000000 -0800
+++ sys/kern/kern_descrip.c	2003-03-16 13:54:16.000000000 -0800
@@ -555,7 +555,7 @@
 	fdp = p->p_fd;
 
 	if ((u_int) fd >= fdp->fd_nfiles)
-		return (NULL);
+		return (EBADF);
 #if 0
 	if (fd_getfile(fdp, fd) == NULL)
 		return (EBADF);

>Release-Note:
>Audit-Trail:
>Unformatted: