shithub: purgatorio

Download patch

ref: 6042bb8adffe111049edfcb8b2eb1ad84814ed69
parent: 84989770721391018dd6a247831cb79a43c1d86b
author: henesy <devnull@localhost>
date: Sun Dec 13 21: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;