initstate_r in C crashing

Posted by Zeno on Sun 26 Jun 2011 07:50 PM — 10 posts, 39,171 views.

USA #0
I am getting a crash when trying to use initstate_r:

Quote:
(gdb) run
Starting program: /home/user/test.out

Program received signal SIGSEGV, Segmentation fault.
0x40052d00 in initstate_r () from /lib/libc.so.6


The code:
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define STATELEN    256             /* random number state buffer */

main()
{
 char randomStateBuffer[STATELEN];
 struct random_data randData;

 printf("Before initstate");
   /* seed the random number generator */
    initstate_r (time(NULL), (char *)&randomStateBuffer, STATELEN,
   (struct random_data *)&randData);
 printf("initstate done");

}


I asked this on SO a while ago but it wasn't solved: http://stackoverflow.com/questions/4167034/c-initstate-r-crashing
USA #1
randomStateBuffer is already a char*, so using & on it results in char**. I know that was mentioned in an answer on SO, but it should definitely be changed.

I'm pretty sure you don't need the (struct random_data*) cast either, because it already is a (struct random_data*) after you use &. Not something that would make things break though.

I honestly don't see anything else that would make it crash. A post on a mailing list [1] suggests that, oddly enough, you need to do this before calling initstate_r():

randData.state = (int32_t*)randomStateBuffer;



[1]: http://lists.debian.org/debian-glibc/2005/08/msg00492.html
Amended on Sun 26 Jun 2011 08:21 PM by Twisol
USA #2
Actually, this code seems to compile and run without segfaulting. Apparently you need to zero out the random_data structure first.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct random_data random_data;

int main() {
  char statebuf[256];
  random_data randomData;
  
  memset(&randomData, 0, sizeof(randomData));
  
  initstate_r(0, statebuf, 256, &randomData);
  return 0;
}
USA #3
That code still crashes for me. :(

(gdb) run
Starting program: /home/p4r/a.out

Program received signal SIGSEGV, Segmentation fault.
0x40052c33 in initstate_r () from /lib/libc.so.6
(gdb) bt
#0  0x40052c33 in initstate_r () from /lib/libc.so.6
#1  0x080483f8 in main ()
USA #4
What command are you using to compile? I have `gcc -o foo foo.c`. I'm using GCC 4.4.5.

What OS? I'm on Ubuntu 10.10.
USA #5
p4r@zeno:~$ gcc -o test test.c
p4r@zeno:~$ gdb test
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i486-slackware-linux"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) run
Starting program: /home/p4r/test

Program received signal SIGSEGV, Segmentation fault.
0x40052c33 in initstate_r () from /lib/libc.so.6
(gdb) bt
#0  0x40052c33 in initstate_r () from /lib/libc.so.6
#1  0x080483f8 in main ()
(gdb)


I'm on Slackware 10.2.0

Old yeah, but I've tried upgrading libc and it didn't fix this crash.
Amended on Sun 26 Jun 2011 09:37 PM by Zeno
Australia Forum Administrator #6
Show us your current code Zeno?
Australia Forum Administrator #7
You shouldn't need to cast something simple like that. Casting and then getting a crash tends to indicate a bad cast.
USA #8
Twisol's code is what I tried, still crashed. Code I tried originally is in the OP.

Oh yeah, valgrind. Here's what I get: http://pastebin.com/w7qcRgnr
Amended on Sun 26 Jun 2011 11:16 PM by Zeno
Australia Forum Administrator #9
Hmm. Try:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main() {
  char statebuf[256];
  struct random_data randomData;

  memset(&randomData, 0, sizeof(randomData));

  initstate_r(0, statebuf, sizeof(statebuf), &randomData);
  printf("initstate done\n");
  return 0;
}


If that doesn't work, not sure. Try the Marsenne Twister maybe?