File: random-length.c

package info (click to toggle)
trinity 1.9%2Bgit20230109.87f1530-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,116 kB
  • sloc: ansic: 33,463; sh: 536; makefile: 163
file content (53 lines) | stat: -rw-r--r-- 833 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>

#include "arch.h"	// page_size
#include "sanitise.h"
#include "random.h"

unsigned long get_len(void)
{
	int i = 0;

	if (RAND_BOOL()) {
		switch (rnd() % 4) {
		case 0:	return sizeof(char);
		case 1:	return sizeof(int);
		case 2:	return sizeof(long);
		case 3: return page_size;
		}
	}

	i = rand32();

	/* short circuit if 0 */
	if (i == 0)
		return 0;

	switch (rnd() % 6) {
	case 0:	i &= 0xff;
		break;
	case 1: i &= page_size - 1;
		break;
	case 2:	i &= 0xffff;
		break;
	case 3:	i &= 0xffffff;
		break;
	case 4:	i &= 0xffffffff;
		break;
	case 5:
		// Pass through
		break;
	}

	/* again, short circuit if 0 */
	if (i == 0)
		return 0;

	/* we might get lucky if something is counting ints/longs etc. */
	if (ONE_IN(4)) {
		int _div = 1 << RAND_RANGE(1, 4);	/* 2,4,8 or 16 */
		i /= _div;
	}

	return i;
}