[MM-30344] utils/markdown: fix index out of range (#16250)

* utils/markdown: fix index out of range

* reflect review comments

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-12-03 13:57:42 +03:00
коммит произвёл GitHub
родитель df906cad9d
Коммит 9283143c3b
2 изменённых файлов: 24 добавлений и 3 удалений

Просмотреть файл

@@ -147,7 +147,7 @@ func parseImageDimensions(markdown string, position int) (raw Range, next int, o
// Read width
hasWidth := false
for isNumericByte(markdown[position]) {
for position < len(markdown)-1 && isNumericByte(markdown[position]) {
hasWidth = true
position += 1
}
@@ -158,14 +158,14 @@ func parseImageDimensions(markdown string, position int) (raw Range, next int, o
}
// Read the x
if markdown[position] != 'x' && markdown[position] != 'X' {
if (markdown[position] != 'x' && markdown[position] != 'X') || position == len(markdown)-1 {
return
}
position += 1
// Read height
hasHeight := false
for isNumericByte(markdown[position]) {
for position < len(markdown)-1 && isNumericByte(markdown[position]) {
hasHeight = true
position += 1
}

Просмотреть файл

@@ -136,6 +136,27 @@ func TestParseImageDimensions(t *testing.T) {
ExpectedNext: 0,
ExpectedOk: false,
},
"garbage 5": {
Input: `![alt](https://example.com =100x200`,
Position: 27,
ExpectedRange: Range{0, 0},
ExpectedNext: 0,
ExpectedOk: false,
},
"garbage 6": {
Input: `![alt](https://example.com =100x`,
Position: 27,
ExpectedRange: Range{0, 0},
ExpectedNext: 0,
ExpectedOk: false,
},
"garbage 7": {
Input: `![alt](https://example.com =x200`,
Position: 27,
ExpectedRange: Range{0, 0},
ExpectedNext: 0,
ExpectedOk: false,
},
} {
t.Run(name, func(t *testing.T) {
raw, next, ok := parseImageDimensions(tc.Input, tc.Position)