Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Oct 2006 13:02:28 GMT
From:      trasz<trasz@pin.if.uz.zgora.p>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   bin/104295: freopen(NULL, ...) does bad things to file offset.
Message-ID:  <200610111302.k9BD2SbI024342@www.freebsd.org>
Resent-Message-ID: <200610111310.k9BDAHFI060386@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         104295
>Category:       bin
>Synopsis:       freopen(NULL, ...) does bad things to file offset.
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 11 13:10:17 GMT 2006
>Closed-Date:
>Last-Modified:
>Originator:     trasz
>Release:        6.1
>Organization:
>Environment:
FreeBSD traszkan.ds7 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #1: Fri Sep 29 12:04:37 CEST 2006     trasz@traszkan.ds7:/usr/obj/usr/src/sys/TRASHCAN  i386
>Description:
Freopen(3) does bad things to the file offset when supplied with NULL
as a file path (freopen(NULL, mode, fd);).  The test program below returns:

file offset before freopen is 100.
file offset after freopen is 2399.

>How-To-Repeat:
#include <stdio.h>

int main(void)
{
        int ret;
        FILE *f;

        f=fopen("/etc/passwd", "r");
        if (f==NULL) {
                perror("fopen");
                exit(-1);
        }

        ret=fseek(f, 100, SEEK_CUR);
        if (ret<0) {
                perror("fseek");    
                exit(-1);
        }

        ret=ftell(f);
        fprintf(stderr, "file offset before freopen is %d.\n", ret);

        f=freopen(NULL, "r", f);
        if (f==NULL) {
                perror("freopen");
                exit(-1);
        }

        ret=ftell(f);
        fprintf(stderr, "file offset after freopen is %d.\n", ret);

        return 0;
}

>Fix:
NOT TESTED:

--- freopen.c.orig      Wed Oct 11 14:59:25 2006
+++ freopen.c   Wed Oct 11 15:00:47 2006
@@ -217,5 +217,9 @@
        fp->_seek = __sseek;
        fp->_close = __sclose;
        FUNLOCKFILE(fp);
+
+       if (file == NULL)
+               rewind(fp);
+
        return (fp);
 }

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200610111302.k9BD2SbI024342>