[tint] Minor change for fix tests

Fix tests will not be able to find an anchor string if the testing code
(the c++) has a longer match than the (empty) literal string.

As writting this is not a case that can be handled by fix-tests and we
fall back to simply reporting an error.

Change-Id: Ia156be02ac7177c9d76769174b469899eba3f976
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/211535
Commit-Queue: Peter McNeeley <petermcneeley@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/tools/src/cmd/fix-tests/main.go b/tools/src/cmd/fix-tests/main.go
index f819313..6647585 100644
--- a/tools/src/cmd/fix-tests/main.go
+++ b/tools/src/cmd/fix-tests/main.go
@@ -291,11 +291,17 @@
 			}
 
 			// trim away the number of unmatched characters from the end of expected to the end of the replacement.
-			replace_str = replace_str[mr_largest.start : len(replace_str)-(len(expected_str)-mr_largest.end)]
-			expected_str = expected_str[mr_largest.start:mr_largest.end]
+			replace_str_end := len(replace_str) - (len(expected_str) - mr_largest.end)
+			if replace_str_end >= mr_largest.start && replace_str_end <= len(replace_str) {
+				replace_str = replace_str[mr_largest.start:replace_str_end]
+				expected_str = expected_str[mr_largest.start:mr_largest.end]
+			} else {
+				// It is not safe to attempt a replace if the replacement string would have negative (nonsense) size.
+				expected_str = ""
+			}
 
-			// Do not try to replace empty strings.
-			if len(replace_str) <= 0 || len(expected_str) <= 0 {
+			// Do not try to replace on empty strings.
+			if len(expected_str) <= 0 {
 				return "", fmt.Errorf("could not fix 'EXPECT_EQ' pattern in '%v'\n\nA: '%v'\n\nB: '%v'", file, a, b)
 			}