Skip to contentSkip to conversation

use symbolic constants in generic lseek code

PATCH In review Chris Snook 2 participants 4 messages Feb 20, 2007 – Feb 21, 2007 VFS

lore
Share & tools
lore
#0 lore raw

cover use symbolic constants in generic lseek code

The generic lseek code in fs/read_write.c uses hardcoded values for SEEK_{SET,CUR,END}.

Patch 1 fixes the case statements to use the symbolic constants in include/linux/fs.h, and should not be at all controversial.

Patch 2 adds a SEEK_MAX and uses it to validate user arguments. This makes the code a little cleaner and also enables future extensions (such as SEEK_DATA and SEEK_HOLE). If anyone has a problem with this, please speak up.

-- Chris

#1 lore raw

PATCH 1/2 use symbolic constants in generic lseek code

From: Chris Snook <[email protected]>

Convert magic numbers to SEEK_* values from fs.h

--

fs/read_write.c +6 -6
@@ -37,10 +37,10 @@ loff_t generic_file_llseek(struct file *
 
 	mutex_lock(&inode->i_mutex);
 	switch (origin) {
-		case 2:
+		case SEEK_END:
 			offset += inode->i_size;
 			break;
-		case 1:
+		case SEEK_CUR:
 			offset += file->f_pos;
 	}
 	retval = -EINVAL;
@@ -63,10 +63,10 @@ loff_t remote_llseek(struct file *file,
 
 	lock_kernel();
 	switch (origin) {
-		case 2:
+		case SEEK_END:
 			offset += i_size_read(file->f_path.dentry->d_inode);
 			break;
-		case 1:
+		case SEEK_CUR:
 			offset += file->f_pos;
 	}
 	retval = -EINVAL;
@@ -94,10 +94,10 @@ loff_t default_llseek(struct file *file,
 
 	lock_kernel();
 	switch (origin) {
-		case 2:
+		case SEEK_END:
 			offset += i_size_read(file->f_path.dentry->d_inode);
 			break;
-		case 1:
+		case SEEK_CUR:
 			offset += file->f_pos;
 	}
 	retval = -EINVAL;
#2 lore raw

PATCH 2/2 use use SEEK_MAX to validate user lseek arguments

In reply to Chris Snook

From: Chris Snook <[email protected]>

Add SEEK_MAX and use it to validate lseek arguments from userspace.

--

fs/read_write.c +2 -2
diff -urp b/fs/read_write.c c/fs/read_write.c
@@ -139,7 +139,7 @@ asmlinkage off_t sys_lseek(unsigned int
 		goto bad;
 
 	retval = -EINVAL;
-	if (origin <= 2) {
+	if (origin <= SEEK_MAX) {
 		loff_t res = vfs_llseek(file, offset, origin);
 		retval = res;
 		if (res != (loff_t)retval)
@@ -166,7 +166,7 @@ asmlinkage long sys_llseek(unsigned int
 		goto bad;
 
 	retval = -EINVAL;
-	if (origin > 2)
+	if (origin > SEEK_MAX)
 		goto out_putf;
 
 	offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
include/linux/fs.h +1 -0
diff -urp b/include/linux/fs.h c/include/linux/fs.h
@@ -30,6 +30,7 @@
 #define SEEK_SET	0	/* seek relative to beginning of file */
 #define SEEK_CUR	1	/* seek relative to current file position */
 #define SEEK_END	2	/* seek relative to end of file */
+#define SEEK_MAX	SEEK_END
 
 /* And dynamically-tunable limits and defaults: */
 struct files_stat_struct {
Acked-by on the v1 series
#3 lore raw
In reply to Chris Snook
Quoted 6 lines · Chris Snook

Patch 1 fixes the case statements to use the symbolic constants in include/linux/fs.h, and should not be at all controversial.

Patch 2 adds a SEEK_MAX and uses it to validate user arguments. This makes the code a little cleaner and also enables future extensions (such as SEEK_DATA and SEEK_HOLE). If anyone has a problem with this, please speak up.

Seems reasonable.