Re: [HACKERS] Re: Cancel key now ready - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: [HACKERS] Re: Cancel key now ready
Date
Msg-id 199806091920.PAA03144@candle.pha.pa.us
Whole thread Raw
In response to Re: [HACKERS] Re: Cancel key now ready  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Here is a patch that will auto-seed any request for random from the
user.  This will prevent users from seeing random values that use our
postmaster cancel seed.


---------------------------------------------------------------------------

Index: src/backend/utils/adt/misc.c
===================================================================
RCS file: /usr/local/cvsroot/pgsql/src/backend/utils/adt/misc.c,v
retrieving revision 1.12
diff -c -r1.12 misc.c
*** misc.c    1998/02/24 03:47:26    1.12
--- misc.c    1998/06/09 19:16:16
***************
*** 13,18 ****
--- 13,19 ----
   */
  #include <sys/types.h>
  #include <sys/file.h>
+ #include <time.h>
  #include "postgres.h"
  #include "utils/datum.h"
  #include "catalog/pg_type.h"
***************
*** 60,65 ****
--- 61,69 ----
   * will return about 1/10 of the tuples in TEMP
   *
   */
+
+ static bool random_initialized = false;
+
  bool
  oidrand(Oid o, int32 X)
  {
***************
*** 68,73 ****
--- 72,88 ----
      if (X == 0)
          return true;

+     /*
+      *    We do this because the cancel key is actually a random, so we don't
+      *    want them to be able to request random numbers using our postmaster
+      *    seeded value.
+      */
+     if (!random_initialized)
+     {
+         srandom((unsigned int)time(NULL));
+         random_initialized = true;
+     }
+
      result = (random() % X == 0);
      return result;
  }
***************
*** 81,86 ****
--- 96,102 ----
  oidsrand(int32 X)
  {
      srand(X);
+     random_initialized = true;
      return true;
  }


--
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Postmaster not starting
Next
From: Brandon Ibach
Date:
Subject: Table corrupt?