Commit 00722f832c for golang

commit 00722f832c7efcc804240cd163da8d39543fa018
Author: Guido Iaquinti <guido.iaquinti@gmail.com>
Date:   Wed Dec 2 20:22:05 2026 +0000

    go/token: avoid overflow in File.Offset for positions before the file

    File.Offset and File.Position compute int(p) - f.base before clamping
    the result to [0, f.size]. For p within f.base of math.MinInt, the
    subtraction wraps around, so a position before the file is clamped to
    the file's end instead of its start, contrary to the Offset
    documentation.

    Clamp p to f.base-1 before subtracting, and use Offset in position.

    Fixes #83586.

    Change-Id: I1e1148ac86bcd8898f1d6a7147bc75923e32301e
    GitHub-Last-Rev: 7f1a83548f28461585cbecca24324aa552ae3ea6
    GitHub-Pull-Request: golang/go#83587
    Reviewed-on: https://go-review.googlesource.com/c/go/+/869341
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Alan Donovan <adonovan@google.com>
    Reviewed-by: Robert Griesemer <gri@google.com>
    Auto-Submit: Robert Griesemer <gri@google.com>

diff --git a/src/go/token/position.go b/src/go/token/position.go
index 4fb8031eea..7266058623 100644
--- a/src/go/token/position.go
+++ b/src/go/token/position.go
@@ -312,7 +312,10 @@ func (f *File) Pos(offset int) Pos {
 // in general, holds for the result offset:
 // f.Offset(f.Pos(offset)) == offset
 func (f *File) Offset(p Pos) int {
-	return f.fixOffset(int(p) - f.base)
+	// Clamp p to just before the file (f.base-1) so that int(p) - f.base
+	// cannot overflow. The resulting offset -1 is still out of bounds,
+	// so fixOffset maps it to 0, or reports it if debug is set.
+	return f.fixOffset(max(int(p), f.base-1) - f.base)
 }

 // Line returns the line number for the given file position p;
@@ -372,7 +375,7 @@ func (f *File) unpack(offset int, adjusted bool) (filename string, line, column
 }

 func (f *File) position(p Pos, adjusted bool) (pos Position) {
-	offset := f.fixOffset(int(p) - f.base)
+	offset := f.Offset(p)
 	pos.Offset = offset
 	pos.Filename, pos.Line, pos.Column = f.unpack(offset, adjusted)
 	return
diff --git a/src/go/token/position_test.go b/src/go/token/position_test.go
index 7971006ce5..9ff3869643 100644
--- a/src/go/token/position_test.go
+++ b/src/go/token/position_test.go
@@ -6,6 +6,7 @@ package token

 import (
 	"fmt"
+	"math"
 	"math/rand"
 	"slices"
 	"strings"
@@ -502,6 +503,9 @@ func TestIssue57490(t *testing.T) {
 	if got := f.Offset(Pos(-1)); got != 0 {
 		t.Errorf("offset = %d, want %d", got, 0)
 	}
+	if got := f.Offset(Pos(math.MinInt)); got != 0 { // p - base must not wrap around
+		t.Errorf("offset = %d, want %d", got, 0)
+	}
 	if got := f.Offset(Pos(base + fsize + 1)); got != fsize {
 		t.Errorf("offset = %d, want %d", got, fsize)
 	}
@@ -519,6 +523,9 @@ func TestIssue57490(t *testing.T) {
 	if got := f.Position(Pos(-1)).String(); got != want {
 		t.Errorf("position = %s, want %s", got, want)
 	}
+	if got := f.Position(Pos(math.MinInt)).String(); got != want {
+		t.Errorf("position = %s, want %s", got, want)
+	}
 	want = fmt.Sprintf("%s:1:%d", f.Name(), fsize+1)
 	if got := f.Position(Pos(fsize + 1)).String(); got != want {
 		t.Errorf("position = %s, want %s", got, want)