Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 7 Sep 1999 01:40:02 -0700 (PDT)
From:      <jkoshy@FreeBSD.org>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: kern/6184: No error if resulting file pos in lseek is negative
Message-ID:  <199909070840.BAA89146@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR kern/6184; it has been noted by GNATS.

From: <jkoshy@FreeBSD.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: phk@FreeBSD.org, des@FreeBSD.org
Subject: Re: kern/6184: No error if resulting file pos in lseek is negative
Date: Tue, 7 Sep 1999 01:34:55 -0700 (PDT)

 This is a multipart MIME message.
 
 --==_Exmh_-3322228240
 Content-Type: text/plain
 
 
 The patch given in the PR seems fine.  I've touched it up a little to
 avoid some style nits and have reworded the manual page to make the
 behaviour clearer.  A small test program is included.
 
 Any reason why this shouldn't go into -current?  If there aren't any
 objections, I'd like to commit this and take the change up with JKH for
 merging to 3.3-RC.
 
 Regards,
 Koshy
 
 
 --==_Exmh_-3322228240
 Content-Type: text/plain ; name="a.c"
 Content-Description: test-program
 Content-Disposition: attachment; filename="a.c"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <errno.h>
 
 int main(int argc, char **argv)
 {
   char *Name = "myfile";
   int pos, fd = open(Name,O_WRONLY | O_CREAT,0777);
 
   write(fd,"ABCDEFGH",8);
   close(fd);
 
   fd = open(Name,O_RDONLY);
 
 #define fail 1
 #define succeed 0
 #define TEST_LSEEK(whence, value, shouldfail) do {\
 	printf("> " #whence "(%d): ", value); \
 	pos = lseek(fd, value, whence); \
 	printf((shouldfail ? (pos == -1) : (pos >= 0)) ? "pass\n" : "fail\n"); \
 	(void) lseek(fd, 0, SEEK_SET); \
 	} while (0)
 	
 
   TEST_LSEEK(SEEK_CUR, -5, fail);
   TEST_LSEEK(SEEK_CUR, +5, succeed);
   TEST_LSEEK(SEEK_CUR, +10, succeed);
 
   TEST_LSEEK(SEEK_SET, -5, fail);
   TEST_LSEEK(SEEK_SET, +5, succeed);
   TEST_LSEEK(SEEK_SET, +10, succeed);
 
   TEST_LSEEK(SEEK_END, -10, fail);
   TEST_LSEEK(SEEK_END, -5, succeed);
   TEST_LSEEK(SEEK_END, +10, succeed);
 
   return 0;
 }
 
 --==_Exmh_-3322228240
 Content-Type: text/plain ; name="foo"
 Content-Description: revised-lseek-patch
 Content-Disposition: attachment; filename="foo"
 
 Index: sys/kern/vfs_syscalls.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/kern/vfs_syscalls.c,v
 retrieving revision 1.130
 diff -u -r1.130 vfs_syscalls.c
 --- vfs_syscalls.c	1999/08/12 20:38:32	1.130
 +++ vfs_syscalls.c	1999/09/07 12:28:15
 @@ -1423,6 +1423,7 @@
  	register struct file *fp;
  	struct vattr vattr;
  	int error;
 +	off_t ofs;
  
  	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
  	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
 @@ -1431,21 +1432,23 @@
  		return (ESPIPE);
  	switch (SCARG(uap, whence)) {
  	case L_INCR:
 -		fp->f_offset += SCARG(uap, offset);
 +		ofs = fp->f_offset + SCARG(uap, offset);
  		break;
  	case L_XTND:
  		error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p);
  		if (error)
  			return (error);
 -		fp->f_offset = SCARG(uap, offset) + vattr.va_size;
 +		ofs = SCARG(uap, offset) + vattr.va_size;
  		break;
  	case L_SET:
 -		fp->f_offset = SCARG(uap, offset);
 +		ofs = SCARG(uap, offset);
  		break;
  	default:
  		return (EINVAL);
  	}
 -	*(off_t *)(p->p_retval) = fp->f_offset;
 +	if (ofs < 0)
 +		return (EINVAL);
 +	*(off_t *)(p->p_retval) = fp->f_offset = ofs;
  	return (0);
  }
  
 Index: lib/libc/sys/lseek.2
 ===================================================================
 RCS file: /home/ncvs/src/lib/libc/sys/lseek.2,v
 retrieving revision 1.8
 diff -u -r1.8 lseek.2
 --- lseek.2	1998/04/19 22:20:08	1.8
 +++ lseek.2	1999/09/07 13:03:50
 @@ -95,6 +95,7 @@
  of the existing end-of-file of the file. If data is later written
  at this point, subsequent reads of the data in the gap return
  bytes of zeros (until data is actually written into the gap).
 +Setting the file offset to negative values is not permitted.
  .Pp
  Some devices are incapable of seeking.  The value of the pointer
  associated with such a device is undefined.
 @@ -120,7 +121,7 @@
  is associated with a pipe, socket, or FIFO.
  .It Bq Er EINVAL
  .Fa Whence
 -is not a proper value.
 +is not a proper value or the resulting file offset would be negative.
  .El
  .Sh SEE ALSO
  .Xr dup 2 ,
 
 --==_Exmh_-3322228240--
 
 
 


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




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