Skip to content

Commit

Permalink
fix dot env parser infinite loop when a comment is the last line and …
Browse files Browse the repository at this point in the history
…there is no trailling new line
  • Loading branch information
antoinepalazzolo committed Nov 18, 2020
1 parent 0b9fc9d commit a0b6c6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Sources/Vapor/Utilities/DotEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,13 @@ extension DotEnvFile {
}

private mutating func skipComment() {
guard let commentLength = self.countDistance(to: .newLine) else {
return
let commentLength: Int
if let toNewLine = self.countDistance(to: .newLine) {
commentLength = toNewLine + 1 // include newline
} else {
commentLength = self.source.readableBytes
}
self.source.moveReaderIndex(forwardBy: commentLength + 1) // include newline
self.source.moveReaderIndex(forwardBy: commentLength)
}

private mutating func parseLine() -> Line? {
Expand Down
10 changes: 10 additions & 0 deletions Tests/VaporTests/DotEnvTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ final class DotEnvTests: XCTestCase {
.init(key: "BAR", value: "baz"),
])
}
func testCommentWithNoTrailingNewline() throws {
let env = "FOO=bar\n#BAR=baz"
var buffer = ByteBufferAllocator().buffer(capacity: 0)
buffer.writeString(env)
var parser = DotEnvFile.Parser(source: buffer)
let lines = parser.parse()
XCTAssertEqual(lines, [
.init(key: "FOO", value: "bar")
])
}
}

0 comments on commit a0b6c6b

Please sign in to comment.