From e05986a9e4f46eb3fc2a3d2c500da5e4081b0fd6 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 12 Dec 2017 08:43:17 -0800 Subject: [PATCH] Avoid panic by not exceeding bounds of string.. Signed-off-by: James Phillips --- language/lexer/lexer.go | 37 +++++++++++++++++++----------------- language/lexer/lexer_test.go | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/language/lexer/lexer.go b/language/lexer/lexer.go index b370bcad..9d66f758 100644 --- a/language/lexer/lexer.go +++ b/language/lexer/lexer.go @@ -381,6 +381,23 @@ func blockStringValue(in string) string { // Expand a block string's raw value into independent lines. lines := splitLinesRegex.Split(in, -1) + // Remove leading blank lines. + for { + if isBlank := lineIsBlank(lines[0]); !isBlank { + break + } + lines = lines[1:] + } + + // Remove trailing blank lines. + for { + i := len(lines) - 1 + if isBlank := lineIsBlank(lines[i]); !isBlank { + break + } + lines = append(lines[:i], lines[i+1:]...) + } + // Remove common indentation from all lines but first commonIndent := -1 for i := 1; i < len(lines); i++ { @@ -395,27 +412,13 @@ func blockStringValue(in string) string { } if commonIndent > 0 { for i, line := range lines { + if commonIndent > len(line) { + continue + } lines[i] = line[commonIndent:] } } - // Remove leading blank lines. - for { - if isBlank := lineIsBlank(lines[0]); !isBlank { - break - } - lines = lines[1:] - } - - // Remove trailing blank lines. - for { - i := len(lines) - 1 - if isBlank := lineIsBlank(lines[i]); !isBlank { - break - } - lines = append(lines[:i], lines[i+1:]...) - } - // Return a string of the lines joined with U+000A. return strings.Join(lines, "\n") } diff --git a/language/lexer/lexer_test.go b/language/lexer/lexer_test.go index 6a9627f8..b32cd76e 100644 --- a/language/lexer/lexer_test.go +++ b/language/lexer/lexer_test.go @@ -480,6 +480,22 @@ func TestLexer_LexesBlockStrings(t *testing.T) { Value: " white space ", }, }, + { + Body: ` + """ + my great description + spans multiple lines + + with breaks + """ + `, + Expected: Token{ + Kind: TokenKind[BLOCK_STRING], + Start: 5, + End: 89, + Value: "my great description\nspans multiple lines\n\nwith breaks", + }, + }, { Body: `"""contains " quote"""`, Expected: Token{