overlay(): start + count overflows int64 and wraps, so overlay('abcdefghij' placing 'XY' from 3 for 9223372036854775806) appends the whole input again

Open Beginner friendly
#176,137 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, sql
Domain
backend

Research direction

The bug is in the overlay builtin in pkg/sql/sem/builtins/builtins.go, where after := pos + size can overflow int64 and wrap negative. Start by reading the overlay implementation around line 11880 and the builtin's parameter docs near line 1562. Reproduce with the SQL statements in the issue; a test in the builtins test suite should show that a huge count returns the same result as a count past the end of the string. Done means the sum is saturated or an error is raised, and large counts return abXY.

Written by the indexing model from the issue text.

Description

C-bug O-community X-blathers-triaged

Describe the problem

overlay(input placing overlay_val from start_pos for count) computes the end position
pos + size in Go int with no overflow check (pkg/sql/sem/builtins/builtins.go:11880):

after := pos + size
if after < 0 {
	after = 0
} else if after > len(runes) {
	after = len(runes)
}
return tree.NewDString(string(runes[:pos]) + to + string(runes[after:])), nil

When (start_pos - 1) + count passes 2^63 - 1, after wraps negative and is clamped to 0, and
runes[0:], the whole input, is appended after overlay_val. Any count from 8 up to
2^63 - 3 replaces positions 3..10 and returns abXY. From 2^63 - 2 up, the result is
abXYabcdefghij: longer than the input, and containing all of it.

(A side note: the builtin's documentation calls the fourth argument end_pos
(parameter name at builtins.go:1562; :1572, "between start_pos and end_pos"), but the code and PostgreSQL both
treat it as a count.)

To Reproduce

  1. cockroach start-single-node --insecure
  2. In cockroach sql:
SELECT overlay('abcdefghij' placing 'XY' from 3 for 8)                   AS c8,
       overlay('abcdefghij' placing 'XY' from 3 for 9223372036854775805) AS c_2p63m3,
       overlay('abcdefghij' placing 'XY' from 3 for 9223372036854775806) AS c_2p63m2,
       overlay('abcdefghij' placing 'XY' from 3 for 9223372036854775807) AS c_2p63m1;
   c8  | c_2p63m3 |    c_2p63m2    |    c_2p63m1
-------+----------+----------------+-----------------
  abXY | abXY     | abXYabcdefghij | abXYabcdefghij

The function-call form does the same thing:
overlay('abcdefghij', 'abcdefghij', 3, 9223372036854775806) returns
ababcdefghijabcdefghij.

Expected behavior

abXY, the same as for any count past the end of the string, or an error. PostgreSQL's
text_overlay checks start + count with pg_add_s32_overflow and raises an error. On
PostgreSQL 17.11:

postgres=# select overlay('abcdefghij' placing 'XY' from 3 for 2147483646);
ERROR:  integer out of range

Saturating the sum would also work, for example
if size > len(runes)-pos { after = len(runes) } before the addition.

Environment:

  • CockroachDB version: CCL v26.4.0-alpha.3-dev-ccf50fb85e212756f4dae107ab7274a21def9fd5 (master edge build, 2026/10/14 06:08:31, go1.26.6); also v26.3.2
  • Server OS: Linux x86-64 (Ubuntu 22.04), cockroach start-single-node --insecure
  • Client app: cockroach sql

Jira issue: CRDB-69833

Dominant language
Go
Stars
32.6k
Forks
4.2k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cockroachdb/cockroach

All issues in cockroachdb/cockroach

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.