Subject: bin/34584: usr.bin/cksum leaves files open
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <gcw@primenet.com.au>
List: netbsd-bugs
Date: 09/22/2006 06:20:01
>Number:         34584
>Category:       bin
>Synopsis:       cksum not closing filescriptors after finishing processing files
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Sep 22 06:20:00 +0000 2006
>Originator:     Geoff C. Wing
>Release:        NetBSD 4.99.2
>Organization:
>Environment:
System: NetBSD g.primenet.com.au 4.99.2 NetBSD 4.99.2 (G) #0: Thu Sep 21 13:31:51 EST 2006 gcw@g.primenet.com.au:/usr/netbsd/src/sys/arch/i386/compile/G i386
Architecture: i386
Machine: i386
>Description:
	The cksum group of programs leaves file descriptors open when in
	check "-c" mode.  Obviously causes failure with a list of many files.
>How-To-Repeat:
	.
>Fix:

(With lots of context)

--- usr.bin/cksum/cksum.c.org	2006-05-08 11:15:33.000000000 +1000
+++ usr.bin/cksum/cksum.c	2006-09-22 15:00:30.000000000 +1000
@@ -429,40 +429,41 @@
 					ok = 0;
 			} else {
 				if ((fd = open(filename, O_RDONLY, 0)) < 0) {
 					if (check_warn)
 						warn("%s", filename);
 					rval = 1;
 					ok = 0;
 				} else {
 					if (cfncn(fd, &val, &len)) 
 						ok = 0;
 					else {
 						u_int32_t should_val;
 						
 						should_val =
 						  strtoul(cksum, NULL, 10);
 						if (val == should_val)
 							ok = 1;
 						else
 							ok = 0;
 					}
+					close(fd);
 				}
 			}
 
 			if (! ok) {
 				if (hash)
 					printf("(%s) ", hash->hashname);
 				printf("%s: FAILED\n", filename);
 				badcnt++;
 			}
 			cnt++;
 
 		}
 		fclose(f);
 
 		if (badcnt > 0) 
 			rval = 1;
 		
 	} else {
 		/*
 		 * Calculate checksums