aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Thomas Van Lenten <thomasvl@google.com>2020-10-02 09:46:13 -0400
committerGravatar Thomas Van Lenten <thomasvl@google.com>2020-10-02 15:34:46 -0400
commit1e750858d0a3fa31bf9e591cb065dcb031677a9b (patch)
tree18555807ca59765712874922b02dc62367eee815
parent845205f12d5aeb0275a59259b5c658104a7d9197 (diff)
Fix issue found via asan
Don't try to use more bytes then there are in the string.
-rw-r--r--Foundation/GTMRegex.m8
-rw-r--r--Foundation/GTMRegexTest.m9
2 files changed, 15 insertions, 2 deletions
diff --git a/Foundation/GTMRegex.m b/Foundation/GTMRegex.m
index 38077be..3d6f96f 100644
--- a/Foundation/GTMRegex.m
+++ b/Foundation/GTMRegex.m
@@ -652,11 +652,15 @@ static NSString *const kReplacementPattern =
}
- (NSString *)description {
- return [NSString stringWithFormat:@"%@<%p> { regex=\"%@\", allSegments=%s, string=\"%.20s...\" }",
+ // `[utf8StrBuf_ bytes]` won't be null terminated, must manually ensure we
+ // don't ask for more bytes then there are.
+ NSUInteger len = (int)[utf8StrBuf_ length];
+ return [NSString stringWithFormat:@"%@<%p> { regex=\"%@\", allSegments=%s, string=\"%.*s%s\" }",
[self class], self,
regex_,
(allSegments_ ? "YES" : "NO"),
- [utf8StrBuf_ bytes]];
+ (int)(MIN(len, 20)), [utf8StrBuf_ bytes],
+ (len > 20 ? "..." : "")];
}
@end
diff --git a/Foundation/GTMRegexTest.m b/Foundation/GTMRegexTest.m
index 75f5aad..d43b731 100644
--- a/Foundation/GTMRegexTest.m
+++ b/Foundation/GTMRegexTest.m
@@ -885,6 +885,15 @@
XCTAssertNotNil(seg);
XCTAssertGreaterThan([[seg description] length], (NSUInteger)10,
@"failed to get a reasonable description for regex string segment");
+
+ // Truncation on the input string (and handling sort lengths)
+ enumerator = [regex segmentEnumeratorForString:@"aaabbbccc"];
+ XCTAssertNotNil(enumerator);
+ XCTAssertTrue([[enumerator description] hasSuffix:@", string=\"aaabbbccc\" }"]);
+ enumerator = [regex segmentEnumeratorForString:@"aaabbbcccdddeeefffggghhh"];
+ XCTAssertNotNil(enumerator);
+ XCTAssertTrue([[enumerator description] hasSuffix:@", string=\"aaabbbcccdddeeefffgg...\" }"]);
+
// regex w/ other options
regex = [GTMRegex regexWithPattern:@"a+"
options:(kGTMRegexOptionIgnoreCase | kGTMRegexOptionSupressNewlineSupport)];