Skip to content

Commit 6397c1e

Browse files
authored
Merge pull request #6182 from tautschnig/rand
C library: model rand, rand_r so as not to return negative numbers
2 parents dfe252f + e595faf commit 6397c1e

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
int main()
5+
{
6+
int r = rand();
7+
assert(r >= 0);
8+
return 0;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
int main()
5+
{
6+
unsigned int seed;
7+
int r = rand_r(&seed);
8+
assert(r >= 0);
9+
return 0;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/library/stdlib.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,30 @@ long random(void)
564564
__CPROVER_assume(result>=0 && result<=2147483647);
565565
return result;
566566
}
567+
568+
/* FUNCTION: rand */
569+
570+
int __VERIFIER_nondet_int();
571+
572+
int rand(void)
573+
{
574+
__CPROVER_HIDE:;
575+
// We return a non-deterministic value instead of a random one.
576+
int result = __VERIFIER_nondet_int();
577+
__CPROVER_assume(result >= 0);
578+
return result;
579+
}
580+
581+
/* FUNCTION: rand_r */
582+
583+
int __VERIFIER_nondet_int();
584+
585+
int rand_r(unsigned int *seed)
586+
{
587+
__CPROVER_HIDE:;
588+
// We return a non-deterministic value instead of a random one.
589+
(void)*seed;
590+
int result = __VERIFIER_nondet_int();
591+
__CPROVER_assume(result >= 0);
592+
return result;
593+
}

0 commit comments

Comments
 (0)