code: 9ferno

Download patch

ref: 80578f7dfdbe69b48c15d6b8bc18b86f15b04a0b
parent: 80be5158599a247dff7f7701cc883c118b15eaa6
author: henesy <unknown>
date: Mon Dec 14 05:11:56 EST 2020

string(2): add bound check on contains()

--- a/appl/lib/string.b
+++ b/appl/lib/string.b
@@ -498,14 +498,10 @@
 
 # Returns >0 if s∈in and <=0 if s∋in
 contains(in, s: string): int {
-	if(s == "") {
-		# Of course
+	if(in == "" || s == "") {
+		# Can't do anything
 		return 1;
 	}
-	if(len in < 1) {
-		# Can't have anything
-		return 0;
-	}
 	
 	# For each rune 'in' the original string
 	for(r0 := 0; r0 < len in ; r0++) {
@@ -513,7 +509,7 @@
 		r1: int;
 		
 		substring:
-		for(r1 = 0; r1 < len s; r1++) {
+		for(r1 = 0; r1 < len s && r0 + r1 < len in; r1++) {
 			if(in[r0 + r1] == s[r1]) {
 				# Match so far
 				continue substring;