]> granicus.if.org Git - nethack/commitdiff
win32 vs random.c
authornethack.rankin <nethack.rankin>
Sun, 23 Dec 2007 00:59:25 +0000 (00:59 +0000)
committernethack.rankin <nethack.rankin>
Sun, 23 Dec 2007 00:59:25 +0000 (00:59 +0000)
     From a bug report.
Pointer subtraction yields a result of type ptrdiff_t which can be
bigger than type long, potentially resulting in warnings about value
truncation--in these cases, when assigning to a long.

sys/share/random.c

index 02b50c5d4059f999c97045a05a38871820aa7783..6905f48aa76e019e98b3a6bb68e8c3e1ab42707f 100644 (file)
@@ -30,6 +30,8 @@
  *     means the defined()s would be ok again...
  *     - change fprintf(stderr, "x(%d)y\n", z) to impossible("x(%d)y", z)
  *     - remove useless variable `j' from srandom()
+ *     - cast result of pointer subtraction to long since ptrdiff_t could
+ *     be bigger than that and trigger warnings when assigning to long
  */
 
 #include "hack.h"
@@ -250,7 +252,7 @@ initstate( seed, arg_state, n )
        register  char          *ostate         = (char *)( &state[ -1 ] );
 
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
-       else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
+       else  state[ -1 ] = (long)(MAX_TYPES*(rptr - state) + rand_type);
        if(  n  <  BREAK_1  )  {
            if(  n  <  BREAK_0  )  {
                impossible(
@@ -291,7 +293,7 @@ initstate( seed, arg_state, n )
        end_ptr = &state[ rand_deg ];   /* must set end_ptr before srandom */
        srandom( seed );
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
-       else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
+       else  state[ -1 ] = (long)(MAX_TYPES*(rptr - state) + rand_type);
        return( ostate );
 }
 
@@ -320,7 +322,7 @@ setstate( arg_state )
        char                    *ostate         = (char *)( &state[ -1 ] );
 
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
-       else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
+       else  state[ -1 ] = (long)(MAX_TYPES*(rptr - state) + rand_type);
        switch(  type  )  {
            case  TYPE_0:
            case  TYPE_1: