From nobody@FreeBSD.ORG  Mon Jul 17 00:17:07 2000
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id 2053837B930; Mon, 17 Jul 2000 00:17:07 -0700 (PDT)
Message-Id: <20000717071707.2053837B930@hub.freebsd.org>
Date: Mon, 17 Jul 2000 00:17:07 -0700 (PDT)
From: chucha@jam.tario.ru
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@FreeBSD.org
Subject: fgets() and fread() on file without reading rights leads to SEGV
X-Send-Pr-Version: www-1.0

>Number:         19975
>Category:       misc
>Synopsis:       fgets() and fread() on file without reading rights leads to SEGV
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jul 17 00:20:01 PDT 2000
>Closed-Date:    Mon Jul 17 01:37:44 PDT 2000
>Last-Modified:  Mon Jul 17 01:38:15 PDT 2000
>Originator:     Pavel Chuchkalov
>Release:        3.0 and 3.2 ugraded to 4.0
>Organization:
-
>Environment:
FreeBSD 4.0-RELEASE i386
>Description:
If in program file is open, and this file does not have rights to read, fgets() and fread() will cause segmentation violation. fgets() and fread() on "failed" FILE* returned by fopen() crashes program also (use the source code below but do not 'touch test') - stupid but possible programmer's error. I don't know about read() call and others.
>How-To-Repeat:


#include <stdio.h>

extern int errno;

main() {
	FILE *file;
	char str[1024];

	file = fopen("test", "r");
//	fgets(str, sizeof(str), file);
//	fread(str, sizeof(str), 1, file);

	printf("feof(file) = %d, errno = %d, ferror(file) = %d\n",
		feof(file), errno, ferror(file));
}

>Fix:
I'm not competent in fixing, but I would like to be :) Please, CC: chucha@jam.tario.ru while discussing this problem, or tell me maillist (oh, how I'm tired of all of them! ;) where this problem will be discussed. Or, if I'm really wrong and my trouble is only in my mind, let me know also... Good luck!

>Release-Note:
>Audit-Trail:

From: Gregory Bond <gnb@itga.com.au>
To: chucha@jam.tario.ru
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: misc/19975: fgets() and fread() on file without reading rights leads to SEGV 
Date: Mon, 17 Jul 2000 18:02:17 +1000

 Read the man page for fopen().  It returns NULL if the file can't be opened.
 
 fread() (and fgets(), and even feof() etc) can only be used on valid
 filehandles.
 
 This PR can be closed.
 
 
State-Changed-From-To: open->closed 
State-Changed-By: alex 
State-Changed-When: Mon Jul 17 01:37:44 PDT 2000 
State-Changed-Why:  
Pilot error - see audit trail: 
fopen() returns NULL if non-readable, and thus the SEGV. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=19975 
>Unformatted:
