mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
fixed partial reads in read_random()
This commit is contained in:
parent
87eb7c13e2
commit
fcd07748dd
1 changed files with 15 additions and 7 deletions
22
lib/utils.c
22
lib/utils.c
|
@ -304,21 +304,29 @@ void * memdup(const void *src, size_t bytes)
|
|||
|
||||
int read_random(char *buf, size_t len)
|
||||
{
|
||||
int ret, fd;
|
||||
int fd;
|
||||
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ret = read(fd, buf, len);
|
||||
if (ret != len)
|
||||
ret = -1;
|
||||
else
|
||||
ret = 0;
|
||||
ssize_t bytes, total = 0;
|
||||
|
||||
while (total < len) {
|
||||
bytes = read(fd, buf + total, len - total);
|
||||
if (bytes < 0)
|
||||
goto out;
|
||||
|
||||
total += bytes;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
out:
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void printb(void *mem, size_t len)
|
||||
|
|
Loading…
Add table
Reference in a new issue