Skip to content

Commit 588372e

Browse files
committed
Free /dev/urandom file descriptor
Fixes #33
1 parent 5a53f54 commit 588372e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

randombytes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
243243
offset += tmp;
244244
n -= tmp;
245245
}
246+
close(fd);
246247
assert(n == 0);
247248
return 0;
248249
}

randombytes_test.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ static void test_issue_22(void) {
9696
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
9797
}
9898

99+
static void test_issue_33(void) {
100+
for (size_t idx = 0; idx < 100000; idx++) {
101+
uint8_t buf[20] = {};
102+
const int ret = randombytes(&buf, sizeof(buf));
103+
if (ret != 0) {
104+
printf("error: %s\n", strerror(errno));
105+
}
106+
assert(ret == 0);
107+
}
108+
}
109+
99110
// ======== Mock OS functions to simulate uncommon behavior ========
100111

101112
#if defined(__linux__) && defined(SYS_getrandom)
@@ -147,9 +158,11 @@ int main(void) {
147158
#if defined(__linux__) && !defined(SYS_getrandom)
148159
RUN_TEST(test_issue_17)
149160
RUN_TEST(test_issue_22)
161+
RUN_TEST(test_issue_33)
150162
#else
151163
SKIP_TEST(test_issue_17)
152164
SKIP_TEST(test_issue_22)
165+
SKIP_TEST(test_issue_33)
153166
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
154167
return 0;
155168
}

0 commit comments

Comments
 (0)