Skip to content

Commit e8073cb

Browse files
authored
fix(rust): prevent panic in doc link parsing (#2879)
This commit fixes a panic in the `isLinkDestination` function that occurred when a URL was located at the very end of a line. The function was attempting to access the character after the URL without checking bounds, leading to an index out of range error. The fix involves: 1. Checking if the URL is preceded by `](` to confirm it is part of a link destination. 2. Safely handling the case where the URL is at the end of the line (inferring a closing parenthesis). 3. Checking for the closing `)` only if the index is within bounds. This ensures that truncated links or bare URLs at the end of lines are handled gracefully without crashing the generator.
1 parent c20a5f3 commit e8073cb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/sidekick/rust/codec.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,15 @@ func escapeUrls(line string) string {
10201020

10211021
// isLinkDestination verifies whether the url is part of a link destination.
10221022
func isLinkDestination(line string, matchStart, matchEnd int) bool {
1023-
return strings.HasSuffix(line[:matchStart], "](") && line[matchEnd] == ')'
1023+
if !strings.HasSuffix(line[:matchStart], "](") {
1024+
return false
1025+
}
1026+
// If the url is at the end of the line, we assume the user meant to close
1027+
// the link.
1028+
if matchEnd == len(line) {
1029+
return true
1030+
}
1031+
return line[matchEnd] == ')'
10241032
}
10251033

10261034
func processList(list *ast.List, indentLevel int, documentationBytes []byte, elementID string) []string {

internal/sidekick/rust/codec_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,9 @@ http://www.unicode.org/cldr/charts/30/supplemental/territory_information.html
17561756
http://www.unicode.org/reports/tr35/#Unicode_locale_identifier.
17571757
https://cloud.google.com/apis/design/design_patterns#integer_types
17581758
https://cloud.google.com/apis/design/design_patterns#integer_types.
1759-
Hyperlink: <a href="https://hyperlink.com">Content</a>`
1759+
Hyperlink: <a href="https://hyperlink.com">Content</a>
1760+
URL at end of line: https://example10.com
1761+
Truncated link: [text](https://example11.com`
17601762
want := []string{
17611763
"/// blah blah <https://cloud.google.com> foo bar",
17621764
"/// [link](https://example1.com)",
@@ -1773,6 +1775,8 @@ Hyperlink: <a href="https://hyperlink.com">Content</a>`
17731775
"/// <https://cloud.google.com/apis/design/design_patterns#integer_types>",
17741776
"/// <https://cloud.google.com/apis/design/design_patterns#integer_types>.",
17751777
"/// Hyperlink: <a href=\"https://hyperlink.com\">Content</a>",
1778+
"/// URL at end of line: <https://example10.com>",
1779+
"/// Truncated link: [text](https://example11.com",
17761780
}
17771781

17781782
wkt := &packagez{

0 commit comments

Comments
 (0)