overlay(): start + count overflows int64 and wraps, so overlay('abcdefghij' placing 'XY' from 3 for 9223372036854775806) appends the whole input again
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
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
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
cockroach start-single-node --insecure- 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
cockroachdb/cockroach#176245 · 1 comment ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinPossibly taken @1AbdullahButt claimed this 14 days ago. OpenC-bug O-community X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cockroachdb/cockroach#176228 · 1 comment ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEMay be free again @darshpatel1052 claimed this 18 days ago, and no pull request is open. OpenC-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
cockroachdb/cockroach#176223 · 2 comments ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutPossibly taken @dajiaohuang claimed this 14 days ago. OpenC-bug O-community X-blathers-untriaged
Difficulty 1/5 Under an hour Newbie friendliness 84/100
cockroachdb/cockroach#176210 · 1 comment ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cockroachdb/cockroach#176150 · 1 comment ·
All issues in cockroachdb/cockroach
Similar issues
-
area/import-export area/projects concern/ux
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
go-vikunja/vikunja#4455 · 1 comment ·
Maintainers usually reply within 2 days
-
Dependabot PRs no longer merge automaticallyPossibly taken @dwisiswant0 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
projectdiscovery/urlfinder#160 ·
Maintainers usually reply within 1 day
-
database
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
chnm/legal-modernism#450 ·
Maintainers usually reply within 1 day
-
tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
bug ready-for-triage ready-to-code
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
RedHatInsights/insights-ingress-go#946 · 4 comments ·
Maintainers usually reply within 1 day