summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorShell <[email protected]>2024-05-30 20:09:02 +0800
committerGitHub <[email protected]>2024-05-30 20:09:02 +0800
commite0df85c8191076f4540addcad98c4d76b3d33b0c (patch)
tree2e1e63d4711c2873746ad41fe1a652b9dc35ddfd /components
parente7cddf3a5275937b38f95afa7540fcc3fc7e5354 (diff)
dev: random should copy size bytes of data to buffer (#9012)
Diffstat (limited to 'components')
-rw-r--r--components/drivers/misc/rt_random.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/components/drivers/misc/rt_random.c b/components/drivers/misc/rt_random.c
index 34d39943fa..da6b312e1e 100644
--- a/components/drivers/misc/rt_random.c
+++ b/components/drivers/misc/rt_random.c
@@ -21,9 +21,26 @@ static rt_uint16_t calc_random(void)
static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
- rt_uint16_t rand = calc_random();
- ssize_t ret = sizeof(rand);
- rt_memcpy(buffer, &rand, ret);
+ rt_uint16_t rand;
+ ssize_t ret = 0;
+ while (size >= sizeof(rand))
+ {
+ /* update rand */
+ rand = calc_random();
+
+ *(rt_uint16_t *)buffer = rand;
+ buffer = (char *)buffer + sizeof(rand);
+ ret += sizeof(rand);
+ size -= sizeof(rand);
+ }
+
+ if (size)
+ {
+ rand = calc_random();
+ memcpy(buffer, &rand, size);
+ ret += size;
+ }
+
return ret;
}
@@ -95,9 +112,26 @@ static rt_uint16_t calc_urandom(void)
static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
- rt_uint16_t rand = calc_urandom();
- ssize_t ret = sizeof(rand);
- rt_memcpy(buffer, &rand, ret);
+ rt_uint16_t rand;
+ ssize_t ret = 0;
+ while (size >= sizeof(rand))
+ {
+ /* update rand */
+ rand = calc_urandom();
+
+ *(rt_uint16_t *)buffer = rand;
+ buffer = (char *)buffer + sizeof(rand);
+ ret += sizeof(rand);
+ size -= sizeof(rand);
+ }
+
+ if (size)
+ {
+ rand = calc_urandom();
+ memcpy(buffer, &rand, size);
+ ret += size;
+ }
+
return ret;
}