@@ -109,6 +109,9 @@ pub enum AllocationQueryError {
109109
110110 /// Adding the overhead to the requested size overflowed
111111 RequestedRegionOverheadOverflow { request : i64 , overhead : i64 } ,
112+
113+ /// Converting from u64 to i64 truncated
114+ RequestedRegionSizeTruncated { request : u64 , e : String } ,
112115}
113116
114117impl From < AllocationQueryError > for external:: Error {
@@ -146,6 +149,13 @@ impl From<AllocationQueryError> for external::Error {
146149 "adding {overhead} to region size {request} overflowed"
147150 ) ,
148151 ) ,
152+
153+ AllocationQueryError :: RequestedRegionSizeTruncated {
154+ request,
155+ e,
156+ } => external:: Error :: internal_error ( & format ! (
157+ "converting {request} to i64 failed! {e}"
158+ ) ) ,
149159 }
150160 }
151161}
@@ -200,9 +210,17 @@ pub fn allocation_query(
200210 } ) ;
201211 }
202212
203- // After the above check, unconditionally cast from u64 to i64. The value is
204- // low enough that this shouldn't truncate.
205- let requested_size: i64 = requested_size. try_into ( ) . unwrap ( ) ;
213+ // After the above check, cast from u64 to i64. The value is low enough
214+ // (after the check above) that try_into should always return Ok.
215+ let requested_size: i64 = match requested_size. try_into ( ) {
216+ Ok ( v) => v,
217+ Err ( e) => {
218+ return Err ( AllocationQueryError :: RequestedRegionSizeTruncated {
219+ request : requested_size,
220+ e : e. to_string ( ) ,
221+ } ) ;
222+ }
223+ } ;
206224
207225 let reservation_percent = RegionReservationPercent :: TwentyFive ;
208226
0 commit comments