pg.ddx.io  GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
[pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
5+ messages / 2 participants
[nested] [flat]

* [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
@ 2026-11-05 07:20  "vlsi (@vlsi)" <noreply+vlsi@github.com>
  0 siblings, 0 replies; 5+ messages in thread

From: vlsi (@vlsi) @ 2026-11-05 07:20 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

**Breaking changes.** Inside a `{call ...}` escape, a `}` after `--` on the same line now belongs to the comment, as it does for PostgreSQL. `{call f(?) -- note}` worked before and now fails with `Malformed function or procedure escape syntax at offset 19.` (SQLState `2F003`). End the comment with a line break before the `}`, or use `/* note */`.

`Parser.modifyJdbcCall` flipped an in-string flag on every `'` in the escape body, including one inside a comment, a quoted identifier, or a dollar quote. A valid call was therefore rejected for either value of `standard_conforming_strings`:

```java
con.prepareCall("{call f() /* it's */}");
// PSQLException: Malformed function or procedure escape syntax at offset 21.
```

With `standard_conforming_strings=off`, a backslash at the end of such a text threw `StringIndexOutOfBoundsException` instead ([#4369](https://github.com/pgjdbc/pgjdbc/pull/4369)).

The escape body now skips string constants, quoted identifiers, dollar quotes, and comments as whole tokens, with the same `Parser` helpers that `parseJdbcSql` and `replaceProcessing` use. A token left open runs to the end of the text, and the call fails with `2F003` at that offset. `parseSingleQuotes` used to stop at the first quote of a doubled `''`, and the second call lost the `E` prefix. Because of this, `{call f(E'\'a''b\'c')}`, which the old flag accepted by accident, failed with `standard_conforming_strings=on`. A doubled quote is now part of the literal, as in PostgreSQL's lexer ([scan.l](https://github.com/postgres/postgres/blob/76e9e44b03a3028d3ef79bc334bc45722a96ce4e/src/backend/parse...)). This also fixes `parseJdbcSql`, which took the `?` in `E'it''s \'?'` for a placeholder, and `replaceProcessing`, which rewrote a `{fn ...}` inside such a string.

Ending a `--` comment at `}` would keep `{call f(?) -- note}` working, and it was rejected. MariaDB Connector/J and H2 reject that input too. MySQL Connector/J accepts it only because the rest of the text becomes a single escape token.

`{call f(E'\'a''b\'c')}` with `standard_conforming_strings=on` passes on master as well. It guards the regression described above.

**Scope.** This covers the `StringIndexOutOfBoundsException` that #4369 fixes, and its inputs are among the tests here. #4369 is no longer needed if this merges.


inline patch 1: CHANGELOG.md, pgjdbc/src/main/java/org/postgresql/core/Parser.java, pgjdbc/src/test/java/org/postgresql/core/ParserTest.java (+142/-14 lines) download inline-1.patchdiff --git a/CHANGELOG.md b/CHANGELOG.md index a85ff9edcf..4c7127137e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,12 +5,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## [Unreleased] ### Fixed +* fix: `prepareCall` accepts a `{call ...}` or `{? = call ...}` escape with a quote inside a comment, a quoted identifier, or a dollar-quoted string, such as `{call f() /* it's */}`. Such a call failed with `Malformed function or procedure escape syntax at offset N.` (SQLState `2F003`) for either value of `standard_conforming_strings`, and with `standard_conforming_strings=off` a backslash at the end of the text threw `StringIndexOutOfBoundsException` instead [PR #4521](https://github.com/pgjdbc/pgjdbc/pull/4521) +* fix: a `?` inside an `E'...'` string that also contains a doubled quote, such as `E'it''s \'?'` with `standard_conforming_strings=on`, is no longer taken for a parameter placeholder, and a `{fn ...}` escape inside such a string is no longer rewritten [PR #4521](https://github.com/pgjdbc/pgjdbc/pull/4521) * fix: `GSSInputStream.available()` now returns the number of decrypted bytes the stream holds and has not returned yet, instead of 0. Under GSS encryption, `PGConnection.getNotifications()` and non-blocking `COPY` reads no longer set a 1 ms socket timeout to probe for data while the stream still holds decrypted bytes [PR #4383](https://github.com/pgjdbc/pgjdbc/pull/4383) * fix: `PGStream.skip` no longer loops forever when the connection ends while the driver is discarding bytes, and no longer mistakes a stream that skips nothing for one that has ended. `InputStream.skip` may return zero with more data still coming, which a stream supplied through the `socketFactory` property is free to do; the driver now reads a byte in that case and treats only `-1` as the end [PR #4358](https://github.com/pgjdbc/pgjdbc/pull/4358) * fix: `LargeObjectManager` operations now work inside an active XA transaction. Since 42.7.13 an XA branch keeps the caller's `autoCommit=true`, and the guard rejected large objects as if the connection were idle. It now checks the server transaction state (`getTransactionState()`) instead of `autoCommit`, so a genuine auto-commit connection with no open transaction is still refused [Issue #4309](https://github.com/pgjdbc/pgjdbc/issues/4309) [PR #4310](https://github.com/pgjdbc/pgjdbc/pull/4310) * fix: batch update counts are no longer secured as committed inside an XA transaction. `BatchResultHandler` treated per-statement progress as durable whenever `autoCommit=true`, but an XA branch keeps `autoCommit=true` while the transaction is open, so a batch that flushed mid-way and then failed could report rolled-back statements as succeeded. Progress is now secured only when no server transaction is open [Issue #4309](https://github.com/pgjdbc/pgjdbc/issues/4309) [PR #4311](https://github.com/pgjdbc/pgjdbc/pull/4311) ### Changed +* **Breaking:** inside a `{call ...}` or `{? = call ...}` escape, a `}` after `--` on the same line is now part of the comment, as it is for PostgreSQL, so `{call f(?) -- note}` fails with `Malformed function or procedure escape syntax at offset N.` (SQLState `2F003`). End the comment with a line break before the `}`, or use `/* note */` [PR #4521](https://github.com/pgjdbc/pgjdbc/pull/4521) * fix: backend message lengths are checked before anything is sized from them. Each length is checked against the message layout and a limit for its type, the lengths inside `DataRow` and `FunctionCallResponse` are checked against the message, and the `NegotiateProtocolVersion` option count is bounded. A hostile or out of sync backend could previously force an allocation of up to two gigabytes with a five byte header [Issue #4015](https://github.com/pgjdbc/pgjdbc/issues/4015) * fix: the read buffer is limited to 32 MiB and grows once per request rather than once per socket read. It previously doubled on every read while a large request was outstanding, so a peer that declared a large length and sent it slowly grew the buffer until the doubling past 1 GiB overflowed to a negative size [Issue #4015](https://github.com/pgjdbc/pgjdbc/issues/4015) * fix: a zero-length `CopyData` body is accepted. It is valid on the wire and was previously rejected by an assertion, so it failed only on JVMs started with `-ea` [Issue #4015](https://github.com/pgjdbc/pgjdbc/issues/4015) diff --git a/pgjdbc/src/main/java/org/postgresql/core/Parser.java b/pgjdbc/src/main/java/org/postgresql/core/Parser.java index d39684910b..e3b7c79717 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/Parser.java +++ b/pgjdbc/src/main/java/org/postgresql/core/Parser.java @@ -419,14 +419,13 @@ private static boolean addReturning(StringBuilder nativeSql, SqlCommandType curr /** * Find the end of the single-quoted string starting at the given offset. * - * <p>Note: for {@code 'single '' quote in string'}, this method currently returns the offset of - * first {@code '} character after the initial one. The caller must call the method a second time - * for the second part of the quoted string.</p> + * <p>A doubled quote, as in {@code 'single '' quote in string'}, is part of the string, so an + * {@code E'...'} string keeps its backslash escapes up to the quote that ends it.</p> * * @param query query * @param offset start offset * @param standardConformingStrings standard conforming strings - * @return position of the end of the single-quoted string + * @return position of the quote that ends the string, or {@code query.length} if no quote ends it */ public static int parseSingleQuotes(final char[] query, int offset, boolean standardConformingStrings) { @@ -442,7 +441,11 @@ && charTerminatesIdentifier(query[offset - 2])) { // do NOT treat backslashes as escape characters while (++offset < query.length) { if (query[offset] == '\'') { - return offset; + if (offset + 1 < query.length && query[offset + 1] == '\'') { + ++offset; + } else { + return offset; + } } } } else { @@ -453,6 +456,10 @@ && charTerminatesIdentifier(query[offset - 2])) { ++offset; break; case '\'': + if (offset + 1 < query.length && query[offset + 1] == '\'') { + ++offset; + break; + } return offset; default: break; @@ -1073,7 +1080,6 @@ public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdString char[] jdbcSqlChars = jdbcSql.toCharArray(); int len = jdbcSql.length(); int state = 1; - boolean inQuotes = false; boolean inEscape = false; int startIndex = -1; int endIndex = -1; @@ -1162,16 +1168,23 @@ public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdString break; case 7: // In "body" of the query (after "{ [? =] call ") + // A literal, a quoted identifier, or a comment is skipped as a whole, so a quote or a + // brace inside it does not count. One left open runs to the end of the text, which then + // fails for the missing closing brace. if (ch == '\'') { - inQuotes = !inQuotes; - ++i; - } else if (inQuotes && ch == '\\' && !stdStrings) { - // Backslash in string constant, skip next character. - i += 2; - } else if (!inQuotes && ch == '{') { + i = Math.min(parseSingleQuotes(jdbcSqlChars, i, stdStrings) + 1, len); + } else if (ch == '"') { + i = Math.min(parseDoubleQuotes(jdbcSqlChars, i) + 1, len); + } else if (ch == '$') { + i = Math.min(parseDollarQuotes(jdbcSqlChars, i) + 1, len); + } else if (ch == '-') { + i = Math.min(parseLineComment(jdbcSqlChars, i) + 1, len); + } else if (ch == '/') { + i = Math.min(parseBlockComment(jdbcSqlChars, i) + 1, len); + } else if (ch == '{') { inEscape = !inEscape; ++i; - } else if (!inQuotes && ch == '}') { + } else if (ch == '}') { if (!inEscape) { // Should be end of string. endIndex = i; @@ -1180,7 +1193,7 @@ public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdString } else { inEscape = false; } - } else if (!inQuotes && ch == ';') { + } else if (ch == ';') { syntaxError = true; } else { // Everything else is ok. diff --git a/pgjdbc/src/test/java/org/postgresql/core/ParserTest.java b/pgjdbc/src/test/java/org/postgresql/core/ParserTest.java index fdca32526a..356c84def2 100644 --- a/pgjdbc/src/test/java/org/postgresql/core/ParserTest.java +++ b/pgjdbc/src/test/java/org/postgresql/core/ParserTest.java @@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; import org.postgresql.jdbc.EscapeSyntaxCallMode; import org.postgresql.util.PSQLException; @@ -18,10 +19,13 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import java.sql.SQLException; import java.util.List; +import java.util.stream.Stream; /** * Test cases for the Parser. @@ -230,6 +234,114 @@ void modifyJdbcCallToleratesTrailingComment() throws SQLException { ServerVersion.v11.getVersionNum(), EscapeSyntaxCallMode.CALL)); } + static Stream<Arguments> callBodiesWithQuotesAndBracesInsideTokens() { + return Stream.of( + arguments("{call f() /* it's */}", true, "call f() /* it's */"), + arguments("{call f() /* it's */}", false, "call f() /* it's */"), + arguments("{call f() -- it's\n}", true, "call f() -- it's\n"), + arguments("{call f() -- it's\n}", false, "call f() -- it's\n"), + arguments("{call f(/* } */)}", true, "call f(/* } */)"), + arguments("{call f(/* } */)}", false, "call f(/* } */)"), + arguments("{call \"it's\"()}", true, "call \"it's\"()"), + arguments("{call \"it's\"()}", false, "call \"it's\"()"), + arguments("{call f($$it's$$)}", true, "call f($$it's$$)"), + arguments("{call f($$it's$$)}", false, "call f($$it's$$)"), + arguments("{call f(E'\\'')}", true, "call f(E'\\'')"), + arguments("{call f(E'\\'')}", false, "call f(E'\\'')"), + arguments("{call f(E'\\'a''b\\'c')}", true, "call f(E'\\'a''b\\'c')"), + arguments("{call f(E'\\'a''b\\'c')}", false, "call f(E'\\'a''b\\'c')"), + arguments("{call f('it''s /* -- }')}", true, "call f('it''s /* -- }')"), + arguments("{call f('it''s /* -- }')}", false, "call f('it''s /* -- }')"), + arguments("{call f('a' '}')}", true, "call f('a' '}')"), + arguments("{call f('a' '}')}", false, "call f('a' '}')"), + arguments("{call f('a\\')}", true, "call f('a\\')"), + arguments("{call f('\\'')}", false, "call f('\\'')"), + arguments("{call f(1-2, 4/2, $1, a$b)}", true, "call f(1-2, 4/2, $1, a$b)"), + arguments("{call f(1-2, 4/2, $1, a$b)}", false, "call f(1-2, 4/2, $1, a$b)"), + arguments("{? = call f(/* it's */ ?)}", true, "call f(?,/* it's */ ?)"), + arguments("{? = call f(/* it's */ ?)}", false, "call f(?,/* it's */ ?)") + ); + } + + @ParameterizedTest + @MethodSource("callBodiesWithQuotesAndBracesInsideTokens") + void callBodyIgnoresQuotesAndBracesInsideCommentsAndQuotedText(String sql, boolean stdStrings, + String expected) throws SQLException { + assertEquals(expected, Parser.modifyJdbcCall(sql, stdStrings, ServerVersion.v11.getVersionNum(), + EscapeSyntaxCallMode.CALL).getSql()); + } + + static Stream<Arguments> callBodiesWithoutClosingBrace() { + return Stream.of( + arguments("{call f() /* }", true), + arguments("{call f() /* }", false), + arguments("{call f() -- }", true), + arguments("{call f() -- }", false), + arguments("{call f() -- '\\", true), + arguments("{call f() -- '\\", false), + arguments("{call f('it''s}", true), + arguments("{call f('it''s}", false), + arguments("{call f('a\\", true), + arguments("{call f('a\\", false), + arguments("{?=call f('a\\", true), + arguments("{?=call f('a\\", false), + arguments("{call f('\\", true), + arguments("{call f('\\", false), + arguments("{call f('a\\')}", false), + arguments("{call \"it's}", true), + arguments("{call \"it's}", false), + arguments("{call \"it's\"() \\", true), + arguments("{call \"it's\"() \\", false), + arguments("{call f($$it's})", true), + arguments("{call f($$it's})", false), + arguments("{call f($$it's$$) \\", true), + arguments("{call f($$it's$$) \\", false) + ); + } + + /** + * A call escape is rejected at the offset of the end of the text when no closing brace stands + * outside a comment, string constant, quoted identifier, or dollar quote. A {@code --} comment runs + * to the end of the line, so a closing brace on that line belongs to the comment. + */ + @ParameterizedTest + @MethodSource("callBodiesWithoutClosingBrace") + void callBodyWithoutClosingBraceIsRejected(String sql, boolean stdStrings) { + PSQLException e = assertThrows(PSQLException.class, () -> Parser.modifyJdbcCall(sql, stdStrings, + ServerVersion.v11.getVersionNum(), EscapeSyntaxCallMode.CALL)); + assertEquals(PSQLState.STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL.getState(), e.getSQLState()); + assertTrue(e.getMessage().contains(Integer.toString(sql.length())), + () -> "the message should report offset " + sql.length() + ": " + e.getMessage()); + } + + static Stream<Arguments> placeholdersAroundEscapeStrings() { + return Stream.of( + arguments("select E'a''b\\'?'", 0), + arguments("select 'a''b', ?", 1) + ); + } + + @ParameterizedTest + @MethodSource("placeholdersAroundEscapeStrings") + void questionMarkInsideAnEscapeStringIsNotAPlaceholder(String sql, int bindCount) + throws SQLException { + List<NativeQuery> queries = Parser.parseJdbcSql(sql, true, true, true, false, true); + assertEquals(bindCount, queries.get(0).bindPositions.length); + } + + static Stream<Arguments> escapesAroundEscapeStrings() { + return Stream.of( + arguments("select E'a''b\\' {fn now()} '", "select E'a''b\\' {fn now()} '"), + arguments("select 'a''b' || {fn now()}", "select 'a''b' || now()") + ); + } + + @ParameterizedTest + @MethodSource("escapesAroundEscapeStrings") + void escapeInsideAnEscapeStringIsNotReplaced(String sql, String expected) throws SQLException { + assertEquals(expected, Parser.replaceProcessing(sql, true, true)); + } + /** * A {@code CALL} (or {@code { ? = call ... }} escape) preceded by a comment must still be * recognised as a function call, otherwise OUT parameter registration fails. See issue #2538.
^ permalink raw [nested|flat] 5+ messages in thread

* Re: [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
@ 2026-11-05 07:20  "vlsi (@vlsi)" <noreply+vlsi@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: vlsi (@vlsi) @ 2026-11-05 07:20 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

@vlsi applied label "backward-incompatible".

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
@ 2026-11-05 07:20  "vlsi (@vlsi)" <noreply+vlsi@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: vlsi (@vlsi) @ 2026-11-05 07:20 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

@vlsi applied label "bug".

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
@ 2026-11-05 07:20  "vlsi (@vlsi)" <noreply+vlsi@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: vlsi (@vlsi) @ 2026-11-05 07:20 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

@vlsi applied label "release/breaking-change".

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body
@ 2026-11-05 07:30  "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: codecov[bot] (bot) (@codecov[bot]) @ 2026-11-05 07:30 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

## [Codecov](https://app.codecov.io/gh/pgjdbc/pgjdbc/pull/4521?dropdown=coverage&src=pr&el=h1&utm_med...) Report
:x: Patch coverage is `88.88889%` with `2 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 72.40%. Comparing base ([`dca5b05`](https://app.codecov.io/gh/pgjdbc/pgjdbc/commit/dca5b058748b0428ee048749ea759476c584152b?dropdown=cov...)) to head ([`ede4510`](https://app.codecov.io/gh/pgjdbc/pgjdbc/commit/ede4510a6a8482ed8e302cebd276d3c5a92402c3?dropdown=cov...)).
:warning: Report is 1 commits behind head on master.

<details><summary>Additional details and impacted files</summary>



```diff
@@             Coverage Diff              @@
##             master    #4521      +/-   ##
============================================
+ Coverage     72.34%   72.40%   +0.06%     
- Complexity     5485     5499      +14     
============================================
  Files           244      244              
  Lines         22195    22204       +9     
  Branches       3731     3735       +4     
============================================
+ Hits          16056    16076      +20     
+ Misses         4470     4462       -8     
+ Partials       1669     1666       -3     
```
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

^ permalink  raw  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-11-05 07:30 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 07:20 [pgjdbc/pgjdbc] PR #4521: fix(core): skip comments and quoted text in the {call} escape body "vlsi (@vlsi)" <noreply+vlsi@github.com>
2026-11-05 07:20 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
2026-11-05 07:20 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
2026-11-05 07:20 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
2026-11-05 07:30 ` "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox