diff --git a/utils/markdown/links.go b/utils/markdown/links.go index df4aa74877..6aa56f256f 100644 --- a/utils/markdown/links.go +++ b/utils/markdown/links.go @@ -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 } diff --git a/utils/markdown/links_test.go b/utils/markdown/links_test.go index 082b32c2a6..0fe9b87219 100644 --- a/utils/markdown/links_test.go +++ b/utils/markdown/links_test.go @@ -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)