| Summary: | initstate() does not save the current position of the previous state array | ||
|---|---|---|---|
| Product: | glibc | Reporter: | Peter Bergner <bergner> |
| Component: | math | Assignee: | Andreas Jaeger <aj> |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | glibc-bugs |
| Priority: | P2 | ||
| Version: | 2.3.4 | ||
| Target Milestone: | --- | ||
| Host: | linux-i686 | Target: | linux-i686 |
| Build: | linux-i686 | Last reconfirmed: | |
| Project(s) to access: | ssh public key: | ||
| Bug Depends on: | |||
| Bug Blocks: | 724 | ||
| Attachments: |
patch to __initstate_r
patch against 2.3-branch 2005/03/15 |
||
Created attachment 400 [details]
patch to __initstate_r
Here's a patch to the cvs version of stdlib/random_r.c:__initstate_r() that
saves the current position of the previous state array before switching to the
new state array.
Subject: Bug 710 CVSROOT: /cvs/glibc Module name: libc Changes by: roland@sources.redhat.com 2005-02-10 09:40:12 Modified files: stdlib : Makefile random_r.c Added files: stdlib : tst-random2.c Log message: 2005-02-09 Jakub Jelinek <jakub@redhat.com> [BZ #710] * stdlib/random_r.c (__initstate_r): Save old state. * stdlib/Makefile (tests): Add tst-random2. * stdlib/tst-random2.c: New test. Reported by Peter Bergner <bergner@vnet.ibm.com>. Patches: http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/tst-random2.c.diff?cvsroot=glibc&r1=NONE&r2=1.1 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/Makefile.diff?cvsroot=glibc&r1=1.98&r2=1.99 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/random_r.c.diff?cvsroot=glibc&r1=1.18&r2=1.19 Subject: Bug 710 CVSROOT: /cvs/glibc Module name: libc Branch: glibc-2_3-branch Changes by: roland@sources.redhat.com 2005-02-16 11:23:59 Modified files: stdlib : Makefile random_r.c Added files: stdlib : tst-random2.c Log message: 2005-02-09 Jakub Jelinek <jakub@redhat.com> [BZ #710] * stdlib/random_r.c (__initstate_r): Save old state. * stdlib/Makefile (tests): Add tst-random2. * stdlib/tst-random2.c: New test. Reported by Peter Bergner <bergner@vnet.ibm.com>. Patches: http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/tst-random2.c.diff?cvsroot=glibc&only_with_tag=glibc-2_3-branch&r1=NONE&r2=1.1.4.1 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/Makefile.diff?cvsroot=glibc&only_with_tag=glibc-2_3-branch&r1=1.95.4.2&r2=1.95.4.3 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/stdlib/random_r.c.diff?cvsroot=glibc&only_with_tag=glibc-2_3-branch&r1=1.18&r2=1.18.4.1 This change causes initstate_r() to crash if provided with an initially empty rand_data struct. What about the attached patch in addition? Created attachment 436 [details]
patch against 2.3-branch 2005/03/15
These changes are in the 2.3 branch as well as the trunk now. |
The initstate() call does not save the current position of the previous state, so if we switch back with setstate(), the sequence of random numbers generated id different than if we had never called initstate/setstate. This works on AIX, MacOSX and FreeBSD. The test case is simple: Peter-Bergners-Computer:~ peter$ cat rand.c #include <stdlib.h> #include <stdio.h> int main (int argc, char **argv) { int i; srandom(1); for (i=0; i < 10; i++) { printf("%d\n", (int)random()); if (argc >= 2) { /* This should not perturb the random number sequence above */ char *os, state[128]; os = initstate (1, state, sizeof(state)); setstate (os); } } return 0; } Expected result: [bergner@otta bergner]$ gcc rand.c [bergner@otta bergner]$ ./a.out 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421 Incorrect result: [bergner@otta bergner]$ ./a.out 1 1804289383 940958272 77627160 1361779697 498448585 1782601122 919270010 55938899 1340091435 476760324 Correct result on MacOSX: Peter-Bergners-Computer:~ peter$ uname -v Darwin Kernel Version 7.7.0: Sun Nov 7 16:06:51 PST 2004; root:xnu/xnu-517.9.5.obj~1/RELEASE_PPC Peter-Bergners-Computer:~ peter$ gcc rand.c Peter-Bergners-Computer:~ peter$ ./a.out 1 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421