From 9ceb7e56ec88cdb3b63076bcbe744f6927d48387 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Fri, 27 Jan 2023 23:30:37 +0000 Subject: [PATCH] Eliminate potential panic in sys_fill_exact (#334) --- src/util_libc.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util_libc.rs b/src/util_libc.rs index bd9c7de1..de1455c5 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -8,6 +8,7 @@ #![allow(dead_code)] use crate::Error; use core::{ + cmp::min, mem::MaybeUninit, num::NonZeroU32, ptr::NonNull, @@ -78,7 +79,8 @@ pub fn sys_fill_exact( } else { // We don't check for EOF (ret = 0) as the data we are reading // should be an infinite stream of random bytes. - buf = &mut buf[(res as usize)..]; + let len = min(res as usize, buf.len()); + buf = &mut buf[len..]; } } Ok(())