Skip to content
This repository
Browse code

fix leading-zero-guard condition on bdigit is smaller than 32bit

  • Loading branch information...
commit 1351eacab386c92ae374c5b9762673128bbe1bd0 1 parent 80d1bc1
Watson authored January 10, 2012

Showing 1 changed file with 9 additions and 2 deletions. Show diff stats Hide diff stats

  1. 11  random.c
11  random.c
@@ -209,18 +209,25 @@ fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
209 209
 static VALUE
210 210
 make_seed_value(const void *ptr)
211 211
 {
  212
+    const long len = DEFAULT_SEED_LEN/SIZEOF_BDIGITS;
212 213
     BDIGIT *digits;
213 214
     NEWOBJ(big, struct RBignum);
214 215
     OBJSETUP(big, rb_cBignum, T_BIGNUM);
215 216
 
216 217
     RBIGNUM_SET_SIGN(big, 1);
217  
-    rb_big_resize((VALUE)big, DEFAULT_SEED_LEN / SIZEOF_BDIGITS + 1);
  218
+    rb_big_resize((VALUE)big, len + 1);
218 219
     digits = RBIGNUM_DIGITS(big);
219 220
 
220 221
     MEMCPY(digits, ptr, char, DEFAULT_SEED_LEN);
221 222
 
222 223
     /* set leading-zero-guard if need. */
223  
-    digits[RBIGNUM_LEN(big)-1] = digits[RBIGNUM_LEN(big)-2] <= 1 ? 1 : 0;
  224
+    digits[len] =
  225
+#if SIZEOF_INT32 / SIZEOF_BDIGITS > 1
  226
+	digits[len-2] <= 1 && digits[len-1] == 0
  227
+#else
  228
+	digits[len-1] <= 1
  229
+#endif
  230
+	? 1 : 0;
224 231
 
225 232
     return rb_big_norm((VALUE)big);
226 233
 }

0 notes on commit 1351eac

Please sign in to comment.
Something went wrong with that request. Please try again.