You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by "Ralf S. Engelschall" <rs...@apache.org> on 1998/03/05 08:50:33 UTC

cvs commit: apache-1.3/src/modules/standard mod_rewrite.c

rse         98/03/04 23:50:33

  Modified:    src/modules/standard mod_rewrite.c
  Log:
  Make sure the returned value of rand() is not greater then RAND_MAX on systems
  like SunOS where we guessed the RAND_MAX value. This the way Ben requested but
  without the "+1" because this actually leaded to an overflow warning under
  compiletime and is not really needed because this is random number generating
  functions where it doesn't count because of the additional bounding checks.
  I've tested it now under FreeBSD 2.1.5 _and_ SunOS 4.1.3 and it both correctly
  gives numbers between 1 and N when run with l=1 and h=N. Nothing more is
  needed....
  
  Revision  Changes    Path
  1.82      +1 -1      apache-1.3/src/modules/standard/mod_rewrite.c
  
  Index: mod_rewrite.c
  ===================================================================
  RCS file: /export/home/cvs/apache-1.3/src/modules/standard/mod_rewrite.c,v
  retrieving revision 1.81
  retrieving revision 1.82
  diff -u -r1.81 -r1.82
  --- mod_rewrite.c	1998/03/04 13:55:08	1.81
  +++ mod_rewrite.c	1998/03/05 07:50:31	1.82
  @@ -2796,7 +2796,7 @@
       char buf[50];
   
       rewrite_rand_init();
  -    sprintf(buf, "%.0f", (((double)rand()/RAND_MAX)*(h-l)));
  +    sprintf(buf, "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l)));
       i = atoi(buf)+1;
       if (i < l) i = l;
       if (i > h) i = h;
  
  
  

Re: cvs commit: apache-1.3/src/modules/standard mod_rewrite.c

Posted by Ben Laurie <be...@algroup.co.uk>.
Ralf S. Engelschall wrote:
> 
> rse         98/03/04 23:50:33
> 
>   Modified:    src/modules/standard mod_rewrite.c
>   Log:
>   Make sure the returned value of rand() is not greater then RAND_MAX on systems
>   like SunOS where we guessed the RAND_MAX value. This the way Ben requested but
>   without the "+1" because this actually leaded to an overflow warning under
>   compiletime and is not really needed because this is random number generating
>   functions where it doesn't count because of the additional bounding checks.

The snag is that if h is RAND_MAX and l is 0, then you will never get h.
No big deal for realistic scenarios, I'd imagine :-)

Cheers,

Ben.

-- 
Ben Laurie            |Phone: +44 (181) 735 0686|  Apache Group member
Freelance Consultant  |Fax:   +44 (181) 735 0689|http://www.apache.org
and Technical Director|Email: ben@algroup.co.uk |
A.L. Digital Ltd,     |Apache-SSL author    http://www.apache-ssl.org/
London, England.      |"Apache: TDG" http://www.ora.com/catalog/apache