Skip to content

Commit 4e973d9

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: yet more refinement of golang/go#70553
Add three new assertions. Updates golang/go#70553 Change-Id: Ia7cff183bbffd287a011804899bc4809edd1c926 Reviewed-on: https://go-review.googlesource.com/c/tools/+/663955 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f64b14a commit 4e973d9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Diff for: gopls/internal/golang/extracttofile.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func ExtractToNewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Han
9393
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
9494
}
9595

96+
// Expand the selection, and compute the portion to extract.
9697
start, end, firstSymbol, ok := selectedToplevelDecls(pgf, start, end)
9798
if !ok {
9899
return nil, fmt.Errorf("invalid selection")
@@ -109,7 +110,20 @@ func ExtractToNewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Han
109110
spaces := len(rest) - len(bytes.TrimLeft(rest, " \t\n"))
110111
end += token.Pos(spaces)
111112
pgf.CheckPos(end) // #70553
112-
// Inv: end is valid wrt pgf.Tok.
113+
if !(start <= end) {
114+
bug.Reportf("start: not before end")
115+
}
116+
// Inv: end is valid wrt pgf.Tok; env >= start.
117+
fileStart := pgf.File.FileStart
118+
pgf.CheckPos(fileStart) // #70553
119+
if !(0 <= start-fileStart) {
120+
bug.Reportf("start: out of bounds")
121+
}
122+
if !(int(end-fileStart) <= len(pgf.Src)) {
123+
bug.Reportf("end: out of bounds")
124+
}
125+
// Inv: 0 <= start-fileStart <= end-fileStart <= len(Src).
126+
src := pgf.Src[start-fileStart : end-fileStart]
113127

114128
replaceRange, err := pgf.PosRange(start, end)
115129
if err != nil {
@@ -176,9 +190,7 @@ func ExtractToNewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Han
176190
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
177191
}
178192

179-
fileStart := pgf.File.FileStart
180-
pgf.CheckPos(fileStart) // #70553
181-
buf.Write(pgf.Src[start-fileStart : end-fileStart])
193+
buf.Write(src)
182194

183195
newFileContent, err := format.Source(buf.Bytes())
184196
if err != nil {

0 commit comments

Comments
 (0)