Skip to content

Fix STR() truncating concatenated strings#4647

Draft
zheliu2 wants to merge 1 commit intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
zheliu2:zheliu/fix-str-concatenation
Draft

Fix STR() truncating concatenated strings#4647
zheliu2 wants to merge 1 commit intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
zheliu2:zheliu/fix-str-concatenation

Conversation

@zheliu2
Copy link
Contributor

@zheliu2 zheliu2 commented Mar 12, 2026

Summary

Fixes #4297

The STR() function was passing length + 1 (buffer size including null terminator) as the string length to tsql_varchar_input(), causing the resulting varchar to include an extra byte. This extra byte caused any strings concatenated after STR() to be silently lost.

Root cause: In contrib/babelfishpg_tsql/src/string.c, the float_str() function allocated size = length + 1 bytes for the buffer (to accommodate the null terminator), but then passed size (instead of the actual string length) to return_varchar_pointer().

Fix: Changed return_varchar_pointer(buf, size) to return_varchar_pointer(buf, strlen(buf)) so the actual string length is used.

Example:

-- Before fix: returns 'distance: 123' (suffix lost)
-- After fix:  returns 'distance: 123 mi' (correct)
PRINT 'distance:' + STR(123, 4, 0) + ' mi'

Test plan

  • Added test cases for string concatenation with STR() result in test/JDBC/input/functions/string_functions/str.sql
  • Updated expected output in test/JDBC/expected/str.out

)

The STR() function was passing `length + 1` (including null terminator
space) as the string size to tsql_varchar_input, causing the resulting
varchar to include an extra byte. This extra byte caused any strings
concatenated after STR() to be lost.

Fix: use strlen(buf) to pass the actual string length instead of the
buffer size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: string appended after STR() gets lost

1 participant