Fix another afl-found bug with its tests
diff --git a/src/num.c b/src/num.c
index db262cb..aac41af 100644
--- a/src/num.c
+++ b/src/num.c
@@ -2137,16 +2137,14 @@
BcStatus s;
BcNum pow, temp, temp2, intn, frac;
ulong state1, state2, inc1, inc2;
- BcDig temp2_num[BC_RAND_NUM_SIZE];
BcDig pow_num[BC_RAND_NUM_SIZE];
- BcDig intn_num[BC_RAND_NUM_SIZE];
- bc_num_setup(&temp2, temp2_num, sizeof(temp2_num) / sizeof(BcDig));
bc_num_setup(&pow, pow_num, sizeof(pow_num) / sizeof(BcDig));
- bc_num_setup(&intn, intn_num, sizeof(intn_num) / sizeof(BcDig));
bc_num_init(&temp, n->len);
+ bc_num_init(&temp2, n->len);
bc_num_init(&frac, n->rdx);
+ bc_num_init(&intn, bc_num_int(n));
s = bc_num_mul(&rng->max, &rng->max, &pow, 0);
if (BC_ERR(s)) goto err;
@@ -2162,7 +2160,7 @@
bc_num_truncate(&temp, temp.scale);
bc_num_copy(&frac, &temp);
- memcpy(intn_num, n->num + n->rdx, BC_NUM_SIZE(bc_num_int(n)));
+ memcpy(intn.num, n->num + n->rdx, BC_NUM_SIZE(bc_num_int(n)));
intn.len = bc_num_int(n);
if (BC_NUM_NONZERO(&frac)) {
@@ -2200,7 +2198,9 @@
bc_rand_seed(rng, state1, state2, inc1, inc2);
err:
+ bc_num_free(&intn);
bc_num_free(&frac);
+ bc_num_free(&temp2);
bc_num_free(&temp);
return s;
}
diff --git a/tests/bc/all.txt b/tests/bc/all.txt
index 3decced..11235c1 100644
--- a/tests/bc/all.txt
+++ b/tests/bc/all.txt
@@ -42,4 +42,5 @@
void
rand
rand2
+rand3
lib2
diff --git a/tests/bc/rand3.txt b/tests/bc/rand3.txt
new file mode 100644
index 0000000..a2a85f6
--- /dev/null
+++ b/tests/bc/rand3.txt
@@ -0,0 +1,291 @@
+print "Gathering array...\n"
+
+s = seed
+
+sum = 0
+
+for (i = 0; i < 100; ++i) {
+ a[i] = rand()
+ sum += a[i]
+ b[i] = irand(sum)
+}
+
+print "Testing implementation...\n"
+
+if (maxrand() >= 2^64 - 1) {
+
+ seed = 54.86785590782347282592869373784717814475564948862907968939359536927733440\
+ 901359008180088183692646452982444316148757934570312500000
+
+ ibase = G
+ obase = G
+
+ for (i = 0; i < 64; ++i) {
+ rand()
+ }
+
+ 7B47F409
+ BA1D3330
+ 83D2F293
+ BFA4784B
+ CBED606E
+ BFC6A3AD
+ 812FFF6D
+ E61F305A
+ F9384B90
+ 32DB86FE
+ 1DC035F9
+ ED786826
+ 3822441D
+ 2BA113D7
+ 1C5B818B
+ A233956A
+ 84DA65E3
+ CED67292
+ B2C0FE06
+ 91817130
+
+ 55FE8917
+ 47E92091
+ 486AF299
+ B1E882BB
+ C261E845
+ 1A9B90F6
+ 7964E884
+ 5F36D7A4
+ 1EE2052D
+ 8519F5D5
+ 293D4E4F
+ 6D8F99FC
+ C3421509
+ A06CD7C6
+ E43064D3
+ E20F9BF0
+ 401B50B7
+ 8EF1FF3E
+ E357E2B2
+ A4AEEE37
+
+ 2AD4426A
+ 9D11BE94
+ 7290C556
+ 6E6F3787
+ 050C2EE3
+ 4FD73703
+ C6FF478B
+ 4B1CA1E1
+ 1654EA91
+ CD08B2F2
+ F7FF3DA8
+ 78B1B8DA
+ A100602C
+ 9588585F
+ DA028873
+ 66B4F376
+ 0E6B4B9A
+ 48167094
+ 0D58CDA0
+ 8F7238BE
+
+ F79983F3
+ 07E5D324
+ AD78DF52
+ 1532BA74
+ 1E4899E2
+ 6C75DF64
+ 171DDC36
+ F2D8D74A
+ 24E6D907
+ 4780FD32
+ 9ADF408C
+ A25544CF
+ EFC6A738
+ 1AA23A54
+ C5A13EBB
+ F739EDC9
+ C3A015FA
+ 3D5E1511
+ AFC4D7FB
+ 3F413B5E
+
+ 4660CB73
+ 88FC773F
+ D6BED59C
+ 63B3B54A
+ D67D3DDE
+ 23394F8B
+ 13384B44
+ DD8B3ABC
+ FF59A21E
+ 3BB16D7E
+ 6E01CB68
+ EC34790E
+ B26C42AD
+ D723C830
+ DFD10FCA
+ 7E362AA1
+ 826FF323
+ CB8F63B5
+ 9B3227E5
+ 9A61E339
+}
+else {
+
+ ibase = G
+ obase = G
+
+ 86B1DA1D72062B68
+ 1304AA46C9853D39
+ A3670E9E0DD50358
+ F9090E529A7DAE00
+ C85B9FD837996F2C
+ 606121F8E3919196
+ 7CE1C7FF478354BA
+ CBC4AC70E541310E
+ 74BE71999EC37F2C
+ B81F9C99A934F1A7
+ 120E9901A900C97F
+ 0F983BAD4B19F493
+ 5934619363660D96
+ D5A7FE2717A2014E
+ 6E437241C9E6676E
+ 6A75C9DD6329CD29
+ 2D9E477683673437
+ 51FB0CF3D4405437
+ 217BB90392D08B20
+ 47C528A018B07A82
+
+ 1B4E474C418C835E
+ BDB2BDA74A119ED6
+ C6DB79D0B9E43493
+ C3CF4834E94A41D1
+ AB8312FC7877C7DC
+ 094B108133E8B5EC
+ 37CA97AC830113BD
+ EF02D7347F9192BF
+ 959517DD9896C53A
+ 7A80EB7629EFE9F9
+ AE53C23F2B1CF57C
+ CA605CD189F6D5CD
+ 921C2704886A9622
+ B68C9FBF826AF7AA
+ 73F8C733124772C3
+ 6B57F7E459EFBCDF
+ 9DE7696DDB6B8E18
+ 02CA67560DC26877
+ A24E353080777DEC
+ 4D600156763FD65C
+
+ 5CDF9C7E26DD2C38
+ 6A32443BBBB16774
+ 3D8415FFECFB8B7F
+ 3090ED9C475635A3
+ 6DBF241361C3E652
+ 2CA9EF5A2AD971FC
+ 44FBE937A1CF0FFC
+ DB17CF0577CB7853
+ AA3747D98D31B24C
+ 5D9A104C5D7F43F7
+ BAE65E3E293B2C7B
+ 16A396F0DB4EF984
+ 6DD2BACDC4445A05
+ 7B7A13D1858E5CA8
+ F73722BCAA52447C
+ 31A2C7BBE77CBA00
+ 7FC8AF9003BA1ACE
+ 5703F11DD3F235EF
+ FA1952267EF836C7
+ BBFA558C9E2D51E2
+
+ 3A29661D8145AF36
+ 608DEA6358DABD7C
+ 9E34E9E53431B447
+ 325A05E35EA524EB
+ 63A87CCF0C80ABB1
+ 8EA183287A46F292
+ E2AA5F119CBF2A08
+ 2F3BEB0DE8B730C8
+ 4B8006A928CF8F5B
+ 57B8BA85069C201C
+ 3422D962DDF59474
+ FD744940BA7366A1
+ 23D24B06B5DA4F6F
+ AA187C608319D1DC
+ DC60CA6FEA738B8A
+ C9FC61DF96A769FE
+ 82E2457708658A20
+ 2BECEC9B3E7D93EC
+ 1340DAEC04588952
+ F533446AD5C50B1D
+
+ 31FD1C7F434A62CE
+ D16DAEDD1F281A39
+ 6B5D9648931D7057
+ 62FEE3392DBB06D5
+ 0358BC87B00DF25A
+ F3C882D22946175D
+ 65BA8F11B4516EFE
+ 2DA5A96E626DA4FE
+ DCC669F4CD6121F0
+ 7A47FAC054319CA2
+ 9661CFEE277284C8
+ 01E483A14F4EB23A
+ ADDC115507390607
+ 5AB47C343BD3B0BD
+ 4882FB3A3957B11F
+ 615B7C9C3626DD44
+ F79CF49562969219
+ 88C32C194EA78D27
+ DA8AFFE1353FF352
+ A7A3C331A64CB146
+
+ ibase = A
+
+ seed = 54.0950779151573258314404657465246373249101452529430389404296875000
+
+ ibase = G
+
+ for (i = 0; i < 64; ++i) {
+
+ rand()
+ }
+}
+
+print "Testing array...\n"
+
+ibase = A
+
+seed = s
+
+sum = 0
+
+for (i = 0; i < 100; ++i) {
+ a[i] == rand()
+ sum += a[i]
+ b[i] == irand(sum)
+}
+
+print "Exercising irand()...\n"
+
+scale = 256
+
+pow = (maxrand() + 1) ^ 4
+s = 2^2560+ 2^128 + (irand(pow) / pow)
+seed = s
+seed < s
+
+s = -459.125
+seed = s
+seed == -s
+
+irand(0)
+irand(0.0281927340987)
+irand(1)
+irand(1.0182739504871)
+irand(maxrand() + 1) <= maxrand()
+
+for (i = 0; i < 200; ++i) {
+ irand(20) < 20
+}
+
+halt
diff --git a/tests/bc/rand3_results.txt b/tests/bc/rand3_results.txt
new file mode 100644
index 0000000..25de7a9
--- /dev/null
+++ b/tests/bc/rand3_results.txt
@@ -0,0 +1,611 @@
+Gathering array...
+Testing implementation...
+86B1DA1D72062B68
+1304AA46C9853D39
+A3670E9E0DD50358
+F9090E529A7DAE00
+C85B9FD837996F2C
+606121F8E3919196
+7CE1C7FF478354BA
+CBC4AC70E541310E
+74BE71999EC37F2C
+B81F9C99A934F1A7
+120E9901A900C97F
+F983BAD4B19F493
+5934619363660D96
+D5A7FE2717A2014E
+6E437241C9E6676E
+6A75C9DD6329CD29
+2D9E477683673437
+51FB0CF3D4405437
+217BB90392D08B20
+47C528A018B07A82
+1B4E474C418C835E
+BDB2BDA74A119ED6
+C6DB79D0B9E43493
+C3CF4834E94A41D1
+AB8312FC7877C7DC
+94B108133E8B5EC
+37CA97AC830113BD
+EF02D7347F9192BF
+959517DD9896C53A
+7A80EB7629EFE9F9
+AE53C23F2B1CF57C
+CA605CD189F6D5CD
+921C2704886A9622
+B68C9FBF826AF7AA
+73F8C733124772C3
+6B57F7E459EFBCDF
+9DE7696DDB6B8E18
+2CA67560DC26877
+A24E353080777DEC
+4D600156763FD65C
+5CDF9C7E26DD2C38
+6A32443BBBB16774
+3D8415FFECFB8B7F
+3090ED9C475635A3
+6DBF241361C3E652
+2CA9EF5A2AD971FC
+44FBE937A1CF0FFC
+DB17CF0577CB7853
+AA3747D98D31B24C
+5D9A104C5D7F43F7
+BAE65E3E293B2C7B
+16A396F0DB4EF984
+6DD2BACDC4445A05
+7B7A13D1858E5CA8
+F73722BCAA52447C
+31A2C7BBE77CBA00
+7FC8AF9003BA1ACE
+5703F11DD3F235EF
+FA1952267EF836C7
+BBFA558C9E2D51E2
+3A29661D8145AF36
+608DEA6358DABD7C
+9E34E9E53431B447
+325A05E35EA524EB
+63A87CCF0C80ABB1
+8EA183287A46F292
+E2AA5F119CBF2A08
+2F3BEB0DE8B730C8
+4B8006A928CF8F5B
+57B8BA85069C201C
+3422D962DDF59474
+FD744940BA7366A1
+23D24B06B5DA4F6F
+AA187C608319D1DC
+DC60CA6FEA738B8A
+C9FC61DF96A769FE
+82E2457708658A20
+2BECEC9B3E7D93EC
+1340DAEC04588952
+F533446AD5C50B1D
+31FD1C7F434A62CE
+D16DAEDD1F281A39
+6B5D9648931D7057
+62FEE3392DBB06D5
+358BC87B00DF25A
+F3C882D22946175D
+65BA8F11B4516EFE
+2DA5A96E626DA4FE
+DCC669F4CD6121F0
+7A47FAC054319CA2
+9661CFEE277284C8
+1E483A14F4EB23A
+ADDC115507390607
+5AB47C343BD3B0BD
+4882FB3A3957B11F
+615B7C9C3626DD44
+F79CF49562969219
+88C32C194EA78D27
+DA8AFFE1353FF352
+A7A3C331A64CB146
+7B47F409
+BA1D3330
+83D2F293
+BFA4784B
+CBED606E
+BFC6A3AD
+812FFF6D
+E61F305A
+F9384B90
+32DB86FE
+1DC035F9
+ED786826
+3822441D
+2BA113D7
+1C5B818B
+A233956A
+84DA65E3
+CED67292
+B2C0FE06
+91817130
+55FE8917
+47E92091
+486AF299
+B1E882BB
+C261E845
+1A9B90F6
+7964E884
+5F36D7A4
+1EE2052D
+8519F5D5
+293D4E4F
+6D8F99FC
+C3421509
+A06CD7C6
+E43064D3
+E20F9BF0
+401B50B7
+8EF1FF3E
+E357E2B2
+A4AEEE37
+2AD4426A
+9D11BE94
+7290C556
+6E6F3787
+50C2EE3
+4FD73703
+C6FF478B
+4B1CA1E1
+1654EA91
+CD08B2F2
+F7FF3DA8
+78B1B8DA
+A100602C
+9588585F
+DA028873
+66B4F376
+E6B4B9A
+48167094
+D58CDA0
+8F7238BE
+F79983F3
+7E5D324
+AD78DF52
+1532BA74
+1E4899E2
+6C75DF64
+171DDC36
+F2D8D74A
+24E6D907
+4780FD32
+9ADF408C
+A25544CF
+EFC6A738
+1AA23A54
+C5A13EBB
+F739EDC9
+C3A015FA
+3D5E1511
+AFC4D7FB
+3F413B5E
+4660CB73
+88FC773F
+D6BED59C
+63B3B54A
+D67D3DDE
+23394F8B
+13384B44
+DD8B3ABC
+FF59A21E
+3BB16D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