Skip to content

dev: random should copy size bytes of data to buffer #9012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions components/drivers/misc/rt_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Loading