Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base/libc.jl: fix crash when run before epoch #34442

Merged
merged 1 commit into from
Jan 20, 2020
Merged

base/libc.jl: fix crash when run before epoch #34442

merged 1 commit into from
Jan 20, 2020

Conversation

anaveragehuman
Copy link
Contributor

Fixes #34056

This makes sure Libc.srand() does what srand() in C would do if supplied with a negative value.

$ faketime -f "1969-01-01 00:00:00" ./julia -e 'println([ccall(:rand, Cint, ()) for _ in 1:4])'
Int32[1569920243, 1857134397, 196762781, 1154924542]

Matches the output in C:

$ cc x.c &&  faketime -f "1969-01-01 00:00:00" ./a.out
seed 4263449296:        1569920243 1857134397 196762781 1154924542 
seed 4263449296:        1569920243 1857134397 196762781 1154924542
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void showrand(unsigned int seed) {
  srand(seed);
  printf("seed %u:\t", seed);
  for (int i = 0; i < 4; i++) {
    printf("%i ", rand());
  }
  printf("\n");
}

int main() {
  long int t1 = time(NULL);
  long int t2 = UINT_MAX + t1 + 1; // t1 is negative

  showrand(t1);
  showrand(t2);
  return 0;
}

@KristofferC KristofferC merged commit f68753c into JuliaLang:master Jan 20, 2020
@anaveragehuman anaveragehuman deleted the time branch January 20, 2020 20:14
KristofferC pushed a commit that referenced this pull request Jan 21, 2020
KristofferC pushed a commit that referenced this pull request Jan 21, 2020
@KristofferC KristofferC mentioned this pull request Jan 21, 2020
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Julia fails to run _before_ epoch
3 participants