Patchwork [08/31] libext2fs: Fix off-by-one error in file truncation

login
register
mail settings
Submitter Darrick J. Wong
Date Oct. 1, 2013, 1:27 a.m.
Message ID <20131001012734.28415.45404.stgit@birch.djwong.org>
Download mbox | patch
Permalink /patch/279287/
State Accepted
Headers show

Comments

Darrick J. Wong - Oct. 1, 2013, 1:27 a.m.
When told to truncate a file, ext2fs_file_set_size2 should start with the first
block past the end of the file.  The current calculation jumps one more block
ahead, with the result that it fails to hack off the last block.  Adding
blocksize-1 and dividing is sufficient to find the last block.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 lib/ext2fs/fileio.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)



--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Lukas Czerner - Oct. 7, 2013, 2:02 p.m.
On Mon, 30 Sep 2013, Darrick J. Wong wrote:

> Date: Mon, 30 Sep 2013 18:27:34 -0700
> From: Darrick J. Wong <darrick.wong@oracle.com>
> To: tytso@mit.edu, darrick.wong@oracle.com
> Cc: linux-ext4@vger.kernel.org
> Subject: [PATCH 08/31] libext2fs: Fix off-by-one error in file truncation
> 
> When told to truncate a file, ext2fs_file_set_size2 should start with the first
> block past the end of the file.  The current calculation jumps one more block
> ahead, with the result that it fails to hack off the last block.  Adding
> blocksize-1 and dividing is sufficient to find the last block.

Looks good.

Reviewed-by: Lukas Czerner <lczerner@redhat.com>

> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
>  lib/ext2fs/fileio.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> 
> diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c
> index ae5cbf1..d875630 100644
> --- a/lib/ext2fs/fileio.c
> +++ b/lib/ext2fs/fileio.c
> @@ -384,10 +384,10 @@ errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
>  	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
>  
>  	truncate_block = ((size + file->fs->blocksize - 1) >>
> -			  EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
> +			  EXT2_BLOCK_SIZE_BITS(file->fs->super));
>  	old_size = EXT2_I_SIZE(&file->inode);
>  	old_truncate = ((old_size + file->fs->blocksize - 1) >>
> -		      EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
> +		      EXT2_BLOCK_SIZE_BITS(file->fs->super));
>  
>  	/* If we're writing a large file, set the large_file flag */
>  	if (LINUX_S_ISREG(file->inode.i_mode) &&
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Theodore Ts'o - Oct. 8, 2013, 3:52 p.m.
On Mon, Sep 30, 2013 at 06:27:34PM -0700, Darrick J. Wong wrote:
> When told to truncate a file, ext2fs_file_set_size2 should start with the first
> block past the end of the file.  The current calculation jumps one more block
> ahead, with the result that it fails to hack off the last block.  Adding
> blocksize-1 and dividing is sufficient to find the last block.
> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>

Applied, thanks.

					- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c
index ae5cbf1..d875630 100644
--- a/lib/ext2fs/fileio.c
+++ b/lib/ext2fs/fileio.c
@@ -384,10 +384,10 @@  errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
 	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
 
 	truncate_block = ((size + file->fs->blocksize - 1) >>
-			  EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
+			  EXT2_BLOCK_SIZE_BITS(file->fs->super));
 	old_size = EXT2_I_SIZE(&file->inode);
 	old_truncate = ((old_size + file->fs->blocksize - 1) >>
-		      EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
+		      EXT2_BLOCK_SIZE_BITS(file->fs->super));
 
 	/* If we're writing a large file, set the large_file flag */
 	if (LINUX_S_ISREG(file->inode.i_mode) &&