修复get_ramdom的长度错误问题() (#677)

This commit is contained in:
Val213 2024-03-31 18:01:32 +08:00 committed by GitHub
parent 56cc4dbe27
commit 7d580ef99d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,11 +1,11 @@
use alloc::vec::Vec;
use system_error::SystemError;
use crate::{ use crate::{
arch::{mm::LockedFrameAllocator, rand::rand}, arch::{mm::LockedFrameAllocator, rand::rand},
libs::rand::GRandFlags, libs::rand::GRandFlags,
mm::allocator::page_frame::FrameAllocator, mm::allocator::page_frame::FrameAllocator,
}; };
use alloc::vec::Vec;
use core::cmp;
use system_error::SystemError;
use super::{user_access::UserBufferWriter, Syscall}; use super::{user_access::UserBufferWriter, Syscall};
@ -63,7 +63,6 @@ impl Syscall {
} }
/// ## 将随机字节填入buf /// ## 将随机字节填入buf
///
/// ### 该系统调用与linux不一致因为目前没有其他随机源 /// ### 该系统调用与linux不一致因为目前没有其他随机源
pub fn get_random(buf: *mut u8, len: usize, flags: GRandFlags) -> Result<usize, SystemError> { pub fn get_random(buf: *mut u8, len: usize, flags: GRandFlags) -> Result<usize, SystemError> {
if flags.bits() == (GRandFlags::GRND_INSECURE.bits() | GRandFlags::GRND_RANDOM.bits()) { if flags.bits() == (GRandFlags::GRND_INSECURE.bits() | GRandFlags::GRND_RANDOM.bits()) {
@ -75,8 +74,11 @@ impl Syscall {
let mut ret = Vec::new(); let mut ret = Vec::new();
let mut count = 0; let mut count = 0;
while count < len { while count < len {
// 对 len - count 的长度进行判断remain_len 小于4则循环次数和 remain_len 相等
let remain_len = len - count;
let step = cmp::min(remain_len, 4);
let rand = rand(); let rand = rand();
for offset in 0..4 { for offset in 0..step {
ret.push((rand >> (offset * 2)) as u8); ret.push((rand >> (offset * 2)) as u8);
count += 1; count += 1;
} }