diff mbox series

btrfs-progs: corrupt-block: fix memory leak in debug_corrupt_sector()

Message ID ebd4d57790941a9c1edef52e09ce0bb4838ab27d.1717474840.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: corrupt-block: fix memory leak in debug_corrupt_sector() | expand

Commit Message

Qu Wenruo June 4, 2024, 4:21 a.m. UTC
ASAN build (make D=asan) would cause memory leak for
btrfs-corrupt-block inside debug_corrupt_sector().

This can be reproduced by fsck/013 test case.

The cause is pretty simple, we just malloc a sector and forgot to free
it.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 btrfs-corrupt-block.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Qu Wenruo June 4, 2024, 4:37 a.m. UTC | #1
在 2024/6/4 13:51, Qu Wenruo 写道:
> ASAN build (make D=asan) would cause memory leak for
> btrfs-corrupt-block inside debug_corrupt_sector().
> 
> This can be reproduced by fsck/013 test case.
> 
> The cause is pretty simple, we just malloc a sector and forgot to free
> it.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Missing the issue tag:

Issue: #806

That's already fixed in my github repo and would create a pull request 
for it.

Thanks,
Qu
> ---
>   btrfs-corrupt-block.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
> index 124597333771..e88319891910 100644
> --- a/btrfs-corrupt-block.c
> +++ b/btrfs-corrupt-block.c
> @@ -70,7 +70,7 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
>   			if (ret < 0) {
>   				errno = -ret;
>   				error("cannot read bytenr %llu: %m", logical);
> -				return ret;
> +				goto out;
>   			}
>   			printf("corrupting %llu copy %d\n", logical, mirror_num);
>   			memset(buf, 0, sectorsize);
> @@ -78,7 +78,7 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
>   			if (ret < 0) {
>   				errno = -ret;
>   				error("cannot write bytenr %llu: %m", logical);
> -				return ret;
> +				goto out;
>   			}
>   		}
>   
> @@ -90,7 +90,8 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
>   		if (mirror_num > num_copies)
>   			break;
>   	}
> -
> +out:
> +	free(buf);
>   	return 0;
>   }
>
Josef Bacik June 4, 2024, 3:55 p.m. UTC | #2
On Tue, Jun 04, 2024 at 01:51:33PM +0930, Qu Wenruo wrote:
> ASAN build (make D=asan) would cause memory leak for
> btrfs-corrupt-block inside debug_corrupt_sector().
> 
> This can be reproduced by fsck/013 test case.
> 
> The cause is pretty simple, we just malloc a sector and forgot to free
> it.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef
diff mbox series

Patch

diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index 124597333771..e88319891910 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -70,7 +70,7 @@  static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
 			if (ret < 0) {
 				errno = -ret;
 				error("cannot read bytenr %llu: %m", logical);
-				return ret;
+				goto out;
 			}
 			printf("corrupting %llu copy %d\n", logical, mirror_num);
 			memset(buf, 0, sectorsize);
@@ -78,7 +78,7 @@  static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
 			if (ret < 0) {
 				errno = -ret;
 				error("cannot write bytenr %llu: %m", logical);
-				return ret;
+				goto out;
 			}
 		}
 
@@ -90,7 +90,8 @@  static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror
 		if (mirror_num > num_copies)
 			break;
 	}
-
+out:
+	free(buf);
 	return 0;
 }