Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4959dde

Browse files
committed
fix(scripts/apidocgen/postprocess): surface masked scanner errors
extractSectionName now checks scanner.Err() when the first Scan fails, so a section whose first line exceeds bufio.Scanner's token limit reports a scanning error instead of a misleading missing-header error. Switch the heading check to strings.CutPrefix and add a test covering the scanner-error path. Ports the format-agnostic generator hardening from #27297 (CRF-8, CRF-15). The front-matter-parsing branch and sectionPreview helper there are specific to that PR's template-level front-matter emission and do not apply here, where the widdershins templates still emit the "# {name}" heading that the postprocessor reads and swaps. Linear: DOCS-483 > This PR was created with AI assistance (Coder Agents).
1 parent 80c0def commit 4959dde

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

scripts/apidocgen/postprocess/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,21 @@ func writeDocs(sections [][]byte) error {
239239
func extractSectionName(section []byte) (string, error) {
240240
scanner := bufio.NewScanner(bytes.NewReader(section))
241241
if !scanner.Scan() {
242+
// Scan returns false on EOF or error. A first line past
243+
// bufio.Scanner's token limit surfaces only in Err(); report it as a
244+
// scanning error rather than mislabeling it a missing header.
245+
if err := scanner.Err(); err != nil {
246+
return "", xerrors.Errorf("scanning section: %w", err)
247+
}
242248
return "", xerrors.Errorf("section header was expected")
243249
}
244250

245251
header := scanner.Text()
246-
if !strings.HasPrefix(header, "# ") {
252+
name, ok := strings.CutPrefix(header, "# ")
253+
if !ok {
247254
return "", xerrors.Errorf("section header %q must start with %q", header, "# ")
248255
}
249-
return strings.TrimSpace(header[2:]), nil
256+
return strings.TrimSpace(name), nil
250257
}
251258

252259
func toMdFilename(sectionName string) string {

scripts/apidocgen/postprocess/main_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"bufio"
5+
"strings"
46
"testing"
57

68
"github.com/stretchr/testify/require"
@@ -56,8 +58,8 @@ func TestPrependFrontMatterKeepsBodyWithoutHeading(t *testing.T) {
5658

5759
// TestExtractSectionName covers extractSectionName's contract, the load-bearing
5860
// guard prependFrontMatter relies on: the first line must be a "# {name}"
59-
// heading, and a section without one (or an empty section) is rejected rather
60-
// than mis-sliced.
61+
// heading, and a section without one (or an empty section) is rejected, not
62+
// sliced into a bogus name.
6163
func TestExtractSectionName(t *testing.T) {
6264
t.Parallel()
6365

@@ -70,4 +72,11 @@ func TestExtractSectionName(t *testing.T) {
7072

7173
_, err = extractSectionName(nil)
7274
require.Error(t, err)
75+
76+
// A first line past bufio.Scanner's token limit makes Scan return false
77+
// with the reason only in Err(); surface it as a scanning error instead of
78+
// a missing-header error.
79+
_, err = extractSectionName([]byte(strings.Repeat("a", bufio.MaxScanTokenSize+1)))
80+
require.Error(t, err)
81+
require.Contains(t, err.Error(), "scanning section")
7382
}

0 commit comments

Comments
 (0)