Skip to content

Commit e0df85c

Browse files
authored
dev: random should copy size bytes of data to buffer (#9012)
1 parent e7cddf3 commit e0df85c

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

Diff for: components/drivers/misc/rt_random.c

+40-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,26 @@ static rt_uint16_t calc_random(void)
2121

2222
static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
2323
{
24-
rt_uint16_t rand = calc_random();
25-
ssize_t ret = sizeof(rand);
26-
rt_memcpy(buffer, &rand, ret);
24+
rt_uint16_t rand;
25+
ssize_t ret = 0;
26+
while (size >= sizeof(rand))
27+
{
28+
/* update rand */
29+
rand = calc_random();
30+
31+
*(rt_uint16_t *)buffer = rand;
32+
buffer = (char *)buffer + sizeof(rand);
33+
ret += sizeof(rand);
34+
size -= sizeof(rand);
35+
}
36+
37+
if (size)
38+
{
39+
rand = calc_random();
40+
memcpy(buffer, &rand, size);
41+
ret += size;
42+
}
43+
2744
return ret;
2845
}
2946

@@ -95,9 +112,26 @@ static rt_uint16_t calc_urandom(void)
95112

96113
static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
97114
{
98-
rt_uint16_t rand = calc_urandom();
99-
ssize_t ret = sizeof(rand);
100-
rt_memcpy(buffer, &rand, ret);
115+
rt_uint16_t rand;
116+
ssize_t ret = 0;
117+
while (size >= sizeof(rand))
118+
{
119+
/* update rand */
120+
rand = calc_urandom();
121+
122+
*(rt_uint16_t *)buffer = rand;
123+
buffer = (char *)buffer + sizeof(rand);
124+
ret += sizeof(rand);
125+
size -= sizeof(rand);
126+
}
127+
128+
if (size)
129+
{
130+
rand = calc_urandom();
131+
memcpy(buffer, &rand, size);
132+
ret += size;
133+
}
134+
101135
return ret;
102136
}
103137

0 commit comments

Comments
 (0)