Browse Source

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

Val213 11 months ago
parent
commit
7d580ef99d
1 changed files with 7 additions and 5 deletions
  1. 7 5
      kernel/src/syscall/misc.rs

+ 7 - 5
kernel/src/syscall/misc.rs

@@ -1,11 +1,11 @@
-use alloc::vec::Vec;
-use system_error::SystemError;
-
 use crate::{
     arch::{mm::LockedFrameAllocator, rand::rand},
     libs::rand::GRandFlags,
     mm::allocator::page_frame::FrameAllocator,
 };
+use alloc::vec::Vec;
+use core::cmp;
+use system_error::SystemError;
 
 use super::{user_access::UserBufferWriter, Syscall};
 
@@ -63,7 +63,6 @@ impl Syscall {
     }
 
     /// ## 将随机字节填入buf
-    ///
     /// ### 该系统调用与linux不一致,因为目前没有其他随机源
     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()) {
@@ -75,8 +74,11 @@ impl Syscall {
         let mut ret = Vec::new();
         let mut count = 0;
         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();
-            for offset in 0..4 {
+            for offset in 0..step {
                 ret.push((rand >> (offset * 2)) as u8);
                 count += 1;
             }