shithub: mc

Download patch

ref: 77246bfe1b9f147b1b255e811fb8b2032c49dc9e
parent: 34d97453258079f514c3297f88663fc23ff6e080
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jul 13 21:37:36 EDT 2019

Fix crash with large shifts in bigint.

--- a/lib/std/bigint.myr
+++ b/lib/std/bigint.myr
@@ -945,18 +945,22 @@
 	off = (s : uint64) / 32
 	shift = (s : uint64) % 32
 
-	/* blit over the base values */
-	for var i = 0; i < a.dig.len - off; i++
-		a.dig[i] = a.dig[i + off]
-	;;
-	a.dig = a.dig[:a.dig.len - off]
-
-	/* and shift over by the remainder */
-	carry = 0
-	for var i = a.dig.len; i > 0; i--
-		t = ((a.dig[i - 1]  : uint64))
-		a.dig[i - 1] = (carry | (t >> shift): uint32)
-		carry = t << (32 - shift)
+	if off > a.dig.len
+		a.dig = a.dig[:0]
+	else
+		/* blit over the base values */
+		for var i = 0; i < a.dig.len - off; i++
+			a.dig[i] = a.dig[i + off]
+		;;
+		a.dig = a.dig[:a.dig.len - off]
+	
+		/* and shift over by the remainder */
+		carry = 0
+		for var i = a.dig.len; i > 0; i--
+			t = ((a.dig[i - 1]  : uint64))
+			a.dig[i - 1] = (carry | (t >> shift): uint32)
+			carry = t << (32 - shift)
+		;;
 	;;
 	-> trim(a)
 }
--- a/lib/std/test/bigint.myr
+++ b/lib/std/test/bigint.myr
@@ -21,6 +21,7 @@
 		[.name = "format-zero", .fn = fmtzero],
 		[.name = "division", .fn = smokediv],
 		[.name = "modulo", .fn = smokemod],
+		[.name = "shift", .fn = shiftoff],
 		[.name = "add-negatives", .fn = addneg],
 		[.name = "sub-negatives", .fn = subneg],
 	][:])
@@ -261,6 +262,13 @@
 	run(c, std.mk(`Sub (\
 		std.mk(`Val "-8"), \
 		std.mk(`Val "-8"))), \
+		"0")
+}
+
+const shiftoff = {c
+	run(c, std.mk(`Shr ( \
+		std.mk(`Val "1"), \
+		std.mk(`Val "65"))), \
 		"0")
 }