apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jor...@apache.org
Subject cvs commit: apr/misc/unix rand.c
Date Fri, 13 Dec 2002 22:40:33 GMT
jorton      2002/12/13 14:40:33

  Modified:    include  apr_general.h
               misc/unix rand.c
  Log:
  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.
  
  Revision  Changes    Path
  1.76      +3 -3      apr/include/apr_general.h
  
  Index: apr_general.h
  ===================================================================
  RCS file: /home/cvs/apr/include/apr_general.h,v
  retrieving revision 1.75
  retrieving revision 1.76
  diff -u -r1.75 -r1.76
  --- apr_general.h	10 Nov 2002 08:35:16 -0000	1.75
  +++ apr_general.h	13 Dec 2002 22:40:33 -0000	1.76
  @@ -262,12 +262,12 @@
   
   /* TODO: I'm not sure this is the best place to put this prototype...*/
   /**
  - * Generate a string of random bytes.
  + * Generate random bytes.
    * @param buf Random bytes go here
  - * @param length size of the buffer
  + * @param length number of bytes to read
    */
   APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char * buf, 
  -                                                    int length);
  +                                                    apr_size_t length);
   
   #endif
   /** @} */
  
  
  
  1.17      +25 -13    apr/misc/unix/rand.c
  
  Index: rand.c
  ===================================================================
  RCS file: /home/cvs/apr/misc/unix/rand.c,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- rand.c	14 Nov 2002 19:55:00 -0000	1.16
  +++ rand.c	13 Dec 2002 22:40:33 -0000	1.17
  @@ -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;
  
  
  

Mime
View raw message