Fix assert error in libfdisk's free space handling
PATCH 1/2 Fix assert error in libfdisk's free space handling
An off-by-one issue exists in fdisk_get_freespaces. It can trigger an assert, as seen here:
$ dd if=/dev/zero of=cfdisk.iso bs=1M count=1 $ losetup -f cfdisk.iso $ echo w | fdisk /dev/loop0 $ echo '1,2' | sfdisk /dev/loop0 --append $ echo '3,' | sfdisk /dev/loop0 --append $ sfdisk --list-free /dev/loop0 Aborted $ _
Problem here is an invalid "grain" processing. A grain is considered expected free space between partitions which can be required for proper alignment. Normally, it's 1 MB but in this case our iso is merely 1 MB so the grain is reduced to 1 byte.
The if-condition in question checks for "last + grain <= pa->start" and therefore even triggers if there is no space between them (due to equal check). Eventually, the start block address is higher than the end block address which triggers the assert().
Signed-off-by Tobias Stoeckmann <[email protected]>
--- libfdisk/src/table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdisk/src/table.c +1 -1
@@ -616,7 +616,7 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)/* We ignore small free spaces (smaller than grain) to keep partitions * aligned, the exception is space before the first partition where * we assume that cxt->first_lba is aligned. */ -if (last + grain <= pa->start +if (last + grain < pa->start || (last < pa->start && last == cxt->first_lba)) { rc = table_add_freespace(cxt, *tb, last + (last > cxt->first_lba ? 1 : 0),
Signature · 1 line
2.9.3
Quoted 2 lines · Tobias Stoeckmann
libfdisk/src/table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied, thanks.
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com