gpt icon indicating copy to clipboard operation
gpt copied to clipboard

Rounding issue of `size` in `add_partition`

Open phil-opp opened this issue 3 years ago • 2 comments

The add_partition method takes the partition size in bytes as argument. It then divides this size by the block size, which effectively rounds it down to the the next lower block size:

https://github.com/Quyzi/gpt/blob/894899bf756df3427487cff4a744c188d86fb3a0/src/lib.rs#L231-L242

Later, the last_lba of the partition is then created through first_lba + size_lba - 1:

https://github.com/Quyzi/gpt/blob/894899bf756df3427487cff4a744c188d86fb3a0/src/lib.rs#L257-L264

So if the given partition size is not aligned to the block size, it is silently rounded down. I would instead expect that it is rounded up, or alternatively an error message.


A side note about the checked_div usage above: The error message doesn't fit the error cause. The checked_div call returns None if a division by zero is attempted, which means that the None branch is only entered if self.config.lb_size is zero. From the error message it seems like you wanted to guard against a size < block_size instead.

phil-opp avatar Jan 14 '21 12:01 phil-opp

Fairly confident this was addressed in #74 as well (https://github.com/Quyzi/gpt/blob/9b6d725106ddd8962a4a8b1bb192455b841a9883/src/lib.rs#L234). Looks to me like it should be fine. Let me know if I'm missing anything!

oldgalileo avatar Oct 17 '21 15:10 oldgalileo

In case the rounding-down issue had to do with partitions that were one block long, I think I fixed that in #82

grahamc avatar Jun 12 '22 11:06 grahamc

Feel free to reopen if this issues isn't resolved

soerenmeier avatar Nov 18 '23 15:11 soerenmeier