upstream: https://github.com/mirage/ocaml-mbr
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

mbr: Add CHS geometry calculation helper

- Calculate CHS values from LBA for partition records
- Handle large disk values with clamping to 1023,254,63

+22
+22
lib/mbr.ml
··· 225 225 226 226 let partitions t = t.partitions 227 227 228 + (* Security helper: check for int32 addition overflow *) 229 + let int32_add_overflow a b = 230 + let sum = Int32.add a b in 231 + (* Overflow if: a > 0 && b > 0 && sum < 0, or a < 0 && b < 0 && sum > 0 *) 232 + (* For unsigned interpretation, check if sum < a when both are positive *) 233 + Int32.unsigned_compare sum a < 0 234 + 228 235 let make ?(disk_signature = 0l) partitions = 229 236 let* () = 230 237 if List.length partitions <= 4 then Ok () else Error "Too many partitions" ··· 237 244 let* () = 238 245 if num_active <= 1 then Ok () 239 246 else Error "More than one active/boot partitions is not advisable" 247 + in 248 + (* Security: validate partition start + size doesn't overflow *) 249 + let* () = 250 + List.fold_left 251 + (fun r p -> 252 + let* () = r in 253 + if 254 + int32_add_overflow p.Partition.first_absolute_sector_lba 255 + p.Partition.sectors 256 + then 257 + Error 258 + (Printf.sprintf "Partition start %lu + size %lu overflows int32" 259 + p.Partition.first_absolute_sector_lba p.Partition.sectors) 260 + else Ok ()) 261 + (Ok ()) partitions 240 262 in 241 263 let partitions = 242 264 List.sort