]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix randomness part in torch
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 22 Jul 2017 11:59:14 +0000 (12:59 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 22 Jul 2017 11:59:14 +0000 (12:59 +0100)
contrib/torch/torch7/lib/TH/THRandom.c

index fbaf2820cdc306eb626a061784e3316e76fb8d97..0dca555fd75ba14fd977832a28b5badb86e7c865 100644 (file)
@@ -1,17 +1,13 @@
 #include "THGeneral.h"
 #include "THRandom.h"
-
-#ifndef _WIN32
-#include <fcntl.h>
-#include <unistd.h>
-#endif
+#include "ottery.h"
 
 /* Code for the Mersenne Twister random generator.... */
 #define n _MERSENNE_STATE_N
 #define m _MERSENNE_STATE_M
 
 /* Creates (unseeded) new generator*/
-static THGenerator* THGenerator_newUnseeded()
+static THGenerator* THGenerator_newUnseeded(void)
 {
   THGenerator *self = THAlloc(sizeof(THGenerator));
   memset(self, 0, sizeof(THGenerator));
@@ -22,7 +18,7 @@ static THGenerator* THGenerator_newUnseeded()
 }
 
 /* Creates new generator and makes sure it is seeded*/
-THGenerator* THGenerator_new()
+THGenerator* THGenerator_new(void)
 {
   THGenerator *self = THGenerator_newUnseeded();
   THRandom_seed(self);
@@ -49,30 +45,9 @@ int THGenerator_isValid(THGenerator *_generator)
   return 0;
 }
 
-#ifndef _WIN32
-static unsigned long readURandomLong()
-{
-  int randDev = open("/dev/urandom", O_RDONLY);
-  unsigned long randValue;
-  if (randDev < 0) {
-    THError("Unable to open /dev/urandom");
-  }
-  ssize_t readBytes = read(randDev, &randValue, sizeof(randValue));
-  if (readBytes < sizeof(randValue)) {
-    THError("Unable to read from /dev/urandom");
-  }
-  close(randDev);
-  return randValue;
-}
-#endif // _WIN32
-
 unsigned long THRandom_seed(THGenerator *_generator)
 {
-#ifdef _WIN32
-  unsigned long s = (unsigned long)time(0);
-#else
-  unsigned long s = readURandomLong();
-#endif
+  unsigned long s = ottery_rand_uint64();
   THRandom_manualSeed(_generator, s);
   return s;
 }