summaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorjorton <jorton@13f79535-47bb-0310-9956-ffa450edef68>2002-12-13 22:40:33 +0000
committerjorton <jorton@13f79535-47bb-0310-9956-ffa450edef68>2002-12-13 22:40:33 +0000
commit2e708d7e28376c458c52fd6d9d49d90b33217c6b (patch)
tree0a9cb4d89235232be9fbcc0220e2422ade676ae8 /misc
parent6e6697f140e2c017613704f5a1f255efa1dedcce (diff)
downloadlibapr-2e708d7e28376c458c52fd6d9d49d90b33217c6b.tar.gz
Changes to apr_generate_random_bytes:
- use apr_size_t for buffer size (please check on non-Unix platforms) - rewrite DEV_RANDOM implementation to re-open if an EOF is received (rather than go into an infinite loop), to cope with the odd /dev/random implementation on BSD/OS 4.1. Also don't leak the fd if the read() fails, and fix a warning with gcc -Wsign-compare. git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@64166 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'misc')
-rw-r--r--misc/unix/rand.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/misc/unix/rand.c b/misc/unix/rand.c
index 3ecb37263..727fc33c7 100644
--- a/misc/unix/rand.c
+++ b/misc/unix/rand.c
@@ -77,27 +77,39 @@
#if APR_HAS_RANDOM
APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf,
- int length)
+ apr_size_t length)
{
#ifdef DEV_RANDOM
- int rnd, rc;
- apr_size_t got, tot;
+ int fd = -1;
- if ((rnd = open(DEV_RANDOM, O_RDONLY)) == -1)
- return errno;
+ /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then
+ * gives EOF, so reading 'length' bytes may require opening the
+ * device several times. */
+ do {
+ apr_ssize_t rc;
- for (tot=0; tot<length; tot += got) {
- if ((rc = read(rnd, buf+tot, length-tot)) < 0) {
- return errno;
+ if (fd == -1)
+ if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1)
+ return errno;
+
+ rc = read(fd, buf, length);
+ if (rc < 0) {
+ int errnum = errno;
+ close(fd);
+ return errnum;
+ }
+ else if (rc == 0) {
+ close(fd);
+ fd = -1; /* force open() again */
}
else {
- got = rc;
+ buf += rc;
+ length -= rc;
}
- }
-
- close(rnd);
-
+ } while (length > 0);
+
+ close(fd);
#elif defined(OS2)
static UCHAR randbyte();
unsigned int idx;