From de565c721a0ab9fb28c9a9e64a6098ce12201e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 27 Jan 2023 02:59:54 +0300 Subject: [PATCH] Eliminate potential panic in sys_fill_exact --- 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(())