Page MenuHomePhabricator

The seed for math.random() should be reset before #invoke
Closed, ResolvedPublic

Description

Since the seed is not reset, math.random() can be used to propagate a global state between indenpendent module invocations, which should be avoided.

Example: http://fr.wikipedia.org/w/index.php?title=Module:Sandbox&oldid=101826839


Version: unspecified
Severity: normal

Details

Reference
bz62291

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 22 2014, 3:02 AM
bzimport set Reference to bz62291.

Since math.random is supposed to be an interface to the C rand() function,[1] and that function is explicitly defined as being seeded with 1 if srand (math.randomseed) hasn't been called,[2] I'd suggest fixing this by calling math.randomseed( 1 ) at the top of mw.executeFunction() (in engines/LuaCommon/lualib/mw.lua).

If no one objects in a reasonable period of time (and no one beats me to it), I'll submit a Gerrit change to do this at some point in the not-too-distant future.

[1]: http://www.lua.org/manual/5.1/manual.html#pdf-math.random
[2]: http://manpages.debian.net/cgi-bin/man.cgi?query=rand&sektion=3&format=html

After thinking about it again, this probably requires some extra care in case of nested modules invocations. Otherwise, the random generator might be unexpectedly reset when frame:expandTemplate() is called, for instance.

(In reply to orlodrim from comment #2)

After thinking about it again, this probably requires some extra care in
case of nested modules invocations. Otherwise, the random generator might be
unexpectedly reset when frame:expandTemplate() is called, for instance.

Good catch!

Change 122862 had a related patch set uploaded by Anomie:
Prevent passing information between #invokes using math.random

https://gerrit.wikimedia.org/r/122862

Change 122862 merged by jenkins-bot:
Prevent passing information between #invokes using math.random

https://gerrit.wikimedia.org/r/122862