Skip to content

Commit ddb2757

Browse files
committed
Enforce modern C syntax
1 parent 21355e4 commit ddb2757

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

random.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,23 @@ static int randombytes_linux_wait_for_entropy(int device)
135135
{
136136
/* We will block on /dev/random, because any increase in the OS' entropy
137137
* level will unblock the request. I use poll here (as does libsodium),
138-
* because we don't *actually* want to read from the device. */
138+
* because we don't *actually* want to read from the device.
139+
*/
139140
enum { IOCTL, PROC } strategy = IOCTL;
140141
const int bits = 128;
141-
struct pollfd pfd;
142-
int fd;
143142
FILE *proc_file;
144-
int retcode, retcode_error = 0; /* Used as return codes */
143+
int retcode_error = 0; /* Used as return codes */
145144
int entropy = 0;
146145

147146
/* If the device has enough entropy already, we will want to return early */
148-
retcode = randombytes_linux_read_entropy_ioctl(device, &entropy);
147+
int retcode = randombytes_linux_read_entropy_ioctl(device, &entropy);
149148
if (retcode != 0 && (errno == ENOTTY || errno == ENOSYS)) {
150149
/* The ioctl call on /dev/urandom has failed due to a
151150
* - ENOTTY (unsupported action), or
152-
* - ENOSYS (invalid ioctl; this happens on MIPS, see #22).
151+
* - ENOSYS (invalid ioctl; this happens on MIPS).
153152
*
154153
* We will fall back to reading from
155-
* `/proc/sys/kernel/random/entropy_avail`. This less ideal,
154+
* /proc/sys/kernel/random/entropy_avail . This is less ideal,
156155
* because it allocates a file descriptor, and it may not work
157156
* in a chroot. But at this point it seems we have no better
158157
* options left.
@@ -169,6 +168,7 @@ static int randombytes_linux_wait_for_entropy(int device)
169168
if (entropy >= bits)
170169
return 0;
171170

171+
int fd;
172172
do {
173173
fd = open("/dev/random", O_RDONLY);
174174
} while (fd == -1 && errno == EINTR); /* EAGAIN will not occur */
@@ -177,8 +177,7 @@ static int randombytes_linux_wait_for_entropy(int device)
177177
return -1;
178178
}
179179

180-
pfd.fd = fd;
181-
pfd.events = POLLIN;
180+
struct pollfd pfd = {.fd = fd, .events = POLLIN};
182181
for (;;) {
183182
retcode = poll(&pfd, 1, -1);
184183
if (retcode == -1 && (errno == EINTR || errno == EAGAIN)) {
@@ -224,8 +223,6 @@ static int randombytes_linux_wait_for_entropy(int device)
224223
static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
225224
{
226225
int fd;
227-
size_t offset = 0, count;
228-
ssize_t tmp;
229226
do {
230227
fd = open("/dev/urandom", O_RDONLY);
231228
} while (fd == -1 && errno == EINTR);
@@ -236,12 +233,12 @@ static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
236233
return -1;
237234
#endif
238235

236+
size_t offset = 0;
239237
while (n > 0) {
240-
count = n <= SSIZE_MAX ? n : SSIZE_MAX;
241-
tmp = read(fd, (char *) buf + offset, count);
242-
if (tmp == -1 && (errno == EAGAIN || errno == EINTR)) {
238+
size_t count = n <= SSIZE_MAX ? n : SSIZE_MAX;
239+
ssize_t tmp = read(fd, (char *) buf + offset, count);
240+
if (tmp == -1 && (errno == EAGAIN || errno == EINTR))
243241
continue;
244-
}
245242
if (tmp == -1)
246243
return -1; /* Unrecoverable IO error */
247244
offset += tmp;

scripts/aspell-pws

+5
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,8 @@ vla
154154
ctrl
155155
randombyte
156156
randombit
157+
ioctl
158+
syscall
159+
getrandom
160+
dev
161+
urandom

0 commit comments

Comments
 (0)