Skip to content

Commit 53c8156

Browse files
committed
btrfs-progs: mkfs: call deflateEnd() after zlib compression
ASAN reports memory leak when zlib is used. The missing part is deflateEnd() that frees structures allocated at deflateInit(). Add it to all exit paths. Signed-off-by: David Sterba <[email protected]>
1 parent 1af2543 commit 53c8156

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

mkfs/rootdir.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,31 +498,40 @@ static ssize_t zlib_compress_extent(bool first_sector, u32 sectorsize,
498498
strm.avail_in = sectorsize;
499499

500500
ret = deflate(&strm, Z_SYNC_FLUSH);
501-
502501
if (ret != Z_OK) {
503502
error("deflate failed: %s", strm.msg);
504-
return -EINVAL;
503+
ret = -EINVAL;
504+
goto out;
505505
}
506506

507-
if (strm.avail_out < BTRFS_MAX_COMPRESSED - sectorsize)
508-
return -E2BIG;
507+
if (strm.avail_out < BTRFS_MAX_COMPRESSED - sectorsize) {
508+
ret = -E2BIG;
509+
goto out;
510+
}
509511

510512
strm.avail_in += in_size - sectorsize;
511513
}
512514

513515
ret = deflate(&strm, Z_FINISH);
514516

515517
if (ret == Z_OK) {
516-
return -E2BIG;
518+
ret = -E2BIG;
519+
goto out;
517520
} else if (ret != Z_STREAM_END) {
518521
error("deflate failed: %s", strm.msg);
519-
return -EINVAL;
522+
ret = -EINVAL;
523+
goto out;
520524
}
521525

522526
if (out_buf + BTRFS_MAX_COMPRESSED - (void *)strm.next_out > sectorsize)
523-
return (void *)strm.next_out - out_buf;
527+
ret = (void *)strm.next_out - out_buf;
528+
else
529+
ret = -E2BIG;
530+
531+
out:
532+
deflateEnd(&strm);
524533

525-
return -E2BIG;
534+
return ret;
526535
}
527536

528537
#if COMPRESSION_LZO
@@ -902,7 +911,6 @@ static ssize_t zlib_compress_inline_extent(char *buf, u64 size, char **comp_buf)
902911
strm.avail_in = size;
903912

904913
zlib_ret = deflate(&strm, Z_FINISH);
905-
906914
if (zlib_ret != Z_OK && zlib_ret != Z_STREAM_END) {
907915
error("deflate failed: %s", strm.msg);
908916
ret = -EINVAL;
@@ -912,11 +920,13 @@ static ssize_t zlib_compress_inline_extent(char *buf, u64 size, char **comp_buf)
912920
if (zlib_ret == Z_STREAM_END && strm.avail_out > 0) {
913921
*comp_buf = out;
914922
ret = size - strm.avail_out;
923+
UASSERT(ret >= 0);
915924
} else {
916925
ret = -E2BIG;
917926
}
918927

919928
out:
929+
deflateEnd(&strm);
920930
if (ret < 0)
921931
free(out);
922932

0 commit comments

Comments
 (0)