Some string_get_size() calls (e.g.: string_get_size(1, 512, STRING_UNITS_10, ..., ...) string_get_size(15, 64, STRING_UNITS_10, ..., ...) ) result in an infinite loop. The problem is that if size is equal to divisor[units]/blk_size and is smaller than divisor[units] we'll end up with size == 0 when we start doing sf_cap calculations: For string_get_size(1, 512, STRING_UNITS_10, ..., ...) case: ... remainder = do_div(size, divisor[units]); -> size is 0, remainder is 1 remainder *= blk_size; -> remainder is 512 ... size *= blk_size; -> size is still 0 size += remainder / divisor[units]; -> size is still 0 The caller causing the issue is sd_read_capacity(), the problem was noticed on Hyper-V, such weird size was reported by host when scanning collides with device removal. This is probably a separate issue worth fixing, this patch is intended to prevent the library routine from infinite looping.
Unified · Split
| @@ -59,7 +59,11 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, · 58 unmodified lines | |||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | exp = divisor[units] / (u32)blk_size; | 61 | exp = divisor[units] / (u32)blk_size; |
| 62 | if (size >= exp) { | 62 | /* |
| 63 | * size must be strictly greater than exp here to ensure that remainder | ||
| 64 | * is greater than divisor[units] coming out of the if below. | ||
| 65 | */ | ||
| 66 | if (size > exp) { | ||
| 63 | remainder = do_div(size, divisor[units]); | 67 | remainder = do_div(size, divisor[units]); |
| 64 | remainder *= blk_size; | 68 | remainder *= blk_size; |
| 65 | i++; | 69 | i++; |