Closed Bug 867769 Opened 6 years ago Closed 6 years ago

NS_MakeRandomString generates the same string when called twice within the same second

Categories

(Core :: XPCOM, defect)

19 Branch
ARM
Gonk (Firefox OS)
defect
Not set

Tracking

()

RESOLVED FIXED
mozilla23

People

(Reporter: dhylands, Assigned: dhylands)

Details

Attachments

(1 file)

NS_MakeRandomString reseeds the random generator each time its called.

It essentially uses the current time measured in seconds as the seed, so if you call it twice within the same second, you'll get the same random string back.

I think it should only set the seed once.

DeviceStorage's Add function currently uses this, and it seems quite plausible that Add could be called more that once in the same second.
Assignee: nobody → dhylands
Attachment #744291 - Flags: review?(benjamin)
Comment on attachment 744291 [details] [diff] [review]
Make it so we only set the seed the first time that NS_MakeRandomString is called.

Review of attachment 744291 [details] [diff] [review]:
-----------------------------------------------------------------

::: xpcom/glue/nsCRTGlue.cpp
@@ +253,5 @@
>    // and salt rand with that.
> +  static unsigned int seed = 0;
> +  if (seed == 0) {
> +    double fpTime = double(PR_Now());
> +    seed = (unsigned int)(fpTime * 1e-6 + 0.5)   // use 1e-6, granularity of PR_Now() on the mac is seconds

nit: Forgot to refresh before attaching the patch, and this line is missing a semi-colon.
Attachment #744291 - Flags: review?(benjamin) → review+
https://hg.mozilla.org/mozilla-central/rev/c10f86bf3439
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla23
You need to log in before you can comment on or make changes to this bug.