Skip to content

Commit e587158

Browse files
committedApr 12, 2018
Resolve ASAN issues
1 parent 34ea75a commit e587158

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed
 

‎Sources/FileCheck/CheckString.swift

+42-35
Original file line numberDiff line numberDiff line change
@@ -386,45 +386,52 @@ private func diagnoseFailedCheck(
386386
return
387387
}
388388

389-
for i in 0..<min(buffer.count, 4096) {
390-
let strIndex = buffer.index(buffer.startIndex, offsetBy: i)
391-
let char = buffer[strIndex]
392-
if char == "\n" {
393-
NumLinesForward += 1
394-
}
389+
let exampleStringLen = exampleString.count
390+
exampleString.withCString { (exampleBuf) -> Void in
391+
for i in 0..<min(buffer.count, 4096) {
392+
let strIndex = buffer.index(buffer.startIndex, offsetBy: i)
393+
let char = buffer[strIndex]
394+
if char == "\n" {
395+
NumLinesForward += 1
396+
}
395397

396-
// Patterns have leading whitespace stripped, so skip whitespace when
397-
// looking for something which looks like a pattern.
398-
if char == " " || char == "\t" {
399-
continue;
400-
}
398+
// Patterns have leading whitespace stripped, so skip whitespace when
399+
// looking for something which looks like a pattern.
400+
if char == " " || char == "\t" {
401+
continue;
402+
}
401403

402-
let subEndIdx: String.Index
403-
if buffer.count < exampleString.count + i {
404-
subEndIdx = buffer.endIndex
405-
} else {
406-
subEndIdx = buffer.index(buffer.startIndex, offsetBy: exampleString.count + i)
407-
}
408-
let subBuffer = buffer[strIndex..<subEndIdx]
409-
410-
// Compute the "quality" of this match as an arbitrary combination of
411-
// the match distance and the number of lines skipped to get to this
412-
// match.
413-
let distance = editDistance(from: subBuffer, to: exampleString)
414-
let quality = Double(distance) + (Double(NumLinesForward) / 100.0)
415-
if quality < BestQuality || BestLine == nil {
416-
BestLine = i
417-
BestQuality = quality
404+
let subEndIdx: String.Index
405+
if buffer.count < exampleString.count + i {
406+
subEndIdx = buffer.endIndex
407+
} else {
408+
subEndIdx = buffer.index(buffer.startIndex, offsetBy: exampleString.count + i)
409+
}
410+
let subBuffer = buffer[strIndex..<subEndIdx]
411+
412+
// Compute the "quality" of this match as an arbitrary combination of
413+
// the match distance and the number of lines skipped to get to this
414+
// match.
415+
let bodyBufLen = subBuffer.count
416+
let distance = subBuffer.withCString { (bodyBuf) -> Int in
417+
return editDistance(from: bodyBuf, fromLength: bodyBufLen,
418+
to: exampleBuf, toLength: exampleStringLen)
419+
}
420+
let quality = Double(distance) + (Double(NumLinesForward) / 100.0)
421+
if quality < BestQuality || BestLine == nil {
422+
BestLine = i
423+
BestQuality = quality
424+
}
418425
}
419-
}
420426

421-
if let Best = BestLine, BestQuality < 50 {
422-
buffer.utf8CString.withUnsafeBufferPointer { buf in
423-
let otherPatternLoc = CheckLocation.inBuffer(
424-
buf.baseAddress!.advanced(by: Best),
425-
UnsafeBufferPointer(start: buf.baseAddress?.advanced(by: Best), count: buf.count - Best)
426-
)
427-
diagnose(.note, at: otherPatternLoc, with: "possible intended match here", options: options)
427+
if let Best = BestLine, BestQuality < 50 {
428+
buffer.utf8CString.withUnsafeBufferPointer { buf in
429+
let otherPatternLoc = CheckLocation.inBuffer(
430+
buf.baseAddress!.advanced(by: Best),
431+
UnsafeBufferPointer(start: buf.baseAddress?.advanced(by: Best), count: buf.count - Best)
432+
)
433+
diagnose(.note, at: otherPatternLoc, with: "possible intended match here", options: options)
434+
}
428435
}
429436
}
430437
}

‎Sources/FileCheck/EditDistance.swift

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
/// - returns: the minimum number of element insertions, removals, or
77
/// replacements needed to transform one of the given sequences into the
88
/// other. If zero, the sequences are identical.
9-
func editDistance(from fa : Substring, to ta : Substring) -> Int {
10-
guard !fa.isEmpty else {
11-
return ta.count
9+
func editDistance(
10+
from fa : UnsafePointer<CChar>, fromLength: Int,
11+
to ta : UnsafePointer<CChar>, toLength: Int
12+
) -> Int {
13+
guard fromLength != 0 else {
14+
return toLength
1215
}
1316

14-
guard !ta.isEmpty else {
15-
return fa.count
17+
guard toLength != 0 else {
18+
return fromLength
1619
}
1720

1821
// The algorithm implemented below is the "classic"
@@ -27,12 +30,14 @@ func editDistance(from fa : Substring, to ta : Substring) -> Int {
2730
// only the entries to the left, top, and top-left are needed. The left
2831
// entry is in `row[x-1]`, the top entry is what's in `row[x]` from the last
2932
// iteration, and the top-left entry is stored in Previous.
30-
var pre = [Int](0..<(ta.count + 1))
31-
var cur = [Int](repeating: 0, count: ta.count + 1)
33+
var pre = [Int](0..<(toLength + 1))
34+
var cur = [Int](repeating: 0, count: toLength + 1)
3235

33-
for (i, ca) in fa.enumerated() {
36+
for i in 0..<fromLength {
37+
let ca = fa[i]
3438
cur[0] = i + 1;
35-
for (j, cb) in ta.enumerated() {
39+
for j in 0..<toLength {
40+
let cb = ta[j]
3641
cur[j + 1] = min(
3742
// deletion
3843
pre[j + 1] + 1, min(
@@ -43,5 +48,5 @@ func editDistance(from fa : Substring, to ta : Substring) -> Int {
4348
}
4449
swap(&cur, &pre)
4550
}
46-
return pre[ta.count]
51+
return pre[toLength]
4752
}

‎Sources/FileCheck/FileCheck.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,19 @@ public func fileCheckOutput(
161161
contents = buf
162162
}
163163

164-
let buf = contents.cString(using: .utf8)?.withUnsafeBufferPointer { buffer in
165-
return readCheckStrings(in: buffer, withPrefixes: validPrefixes, checkNot: checkNot, options: options, prefixRE)
166-
}
167-
guard let checkStrings = buf, !checkStrings.isEmpty else {
164+
guard let cstr = contents.cString(using: .utf8) else {
168165
return false
169166
}
170167

171-
return check(input: input, against: checkStrings, withGlobals: globals, options: options)
168+
return cstr.withUnsafeBufferPointer { buffer in
169+
let checkStrings = readCheckStrings(in: buffer, withPrefixes: validPrefixes,
170+
checkNot: checkNot, options: options, prefixRE)
171+
guard !checkStrings.isEmpty else {
172+
return false
173+
}
174+
175+
return check(input: input, against: checkStrings, withGlobals: globals, options: options)
176+
}
172177
}
173178

174179
private func overrideFDAndCollectOutput(file : FileCheckFD, of block : () -> ()) -> String {
@@ -322,11 +327,7 @@ extension CheckLocation {
322327
}
323328

324329
var endPtr = ptr
325-
while endPtr != buf.baseAddress!.advanced(by: buf.endIndex) && endPtr.successor().pointee != ("\n" as Character).utf8CodePoint {
326-
endPtr = endPtr.successor()
327-
}
328-
// One more for good measure.
329-
if endPtr != buf.baseAddress!.advanced(by: buf.endIndex) {
330+
while buf.baseAddress!.advanced(by: buf.endIndex) - endPtr > 1 && endPtr.pointee != ("\n" as Character).utf8CodePoint {
330331
endPtr = endPtr.successor()
331332
}
332333
return substring(in: buf, with: NSMakeRange(buf.baseAddress!.distance(to: startPtr), startPtr.distance(to: endPtr)))
@@ -409,12 +410,11 @@ private func readCheckStrings(in buf : UnsafeBufferPointer<CChar>, withPrefixes
409410
continue
410411
}
411412

412-
notPattern.utf8CString.withUnsafeBufferPointer { buf in
413-
let patBuf = UnsafeBufferPointer<CChar>(start: buf.baseAddress, count: buf.count - 1)
414-
let pat = Pattern(checking: .not, in: buf, pattern: patBuf, withPrefix: "IMPLICIT-CHECK", at: 0, options: options)!
413+
notPattern.utf8CString.withUnsafeBufferPointer { notBuf in
414+
let patBuf = UnsafeBufferPointer<CChar>(start: notBuf.baseAddress, count: notBuf.count - 1)
415+
let pat = Pattern(checking: .not, in: notBuf, pattern: patBuf, withPrefix: "IMPLICIT-CHECK", at: 0, options: options)!
415416
// Compute the message from this buffer now for diagnostics later.
416-
let msg = CheckLocation.inBuffer(buf.baseAddress!, buf).message
417-
implicitNegativeChecks.append(Pattern(copying: pat, at: .string("IMPLICIT-CHECK-NOT: " + msg)))
417+
implicitNegativeChecks.append(Pattern(copying: pat, at: .string("IMPLICIT-CHECK-NOT: " + pat.fixedString)))
418418
}
419419
}
420420
var dagNotMatches = implicitNegativeChecks

0 commit comments

Comments
 (0)
Please sign in to comment.