Skip to content

Fixed: Invalid UTF-8 byte sequence error (0xff) when relaying text parameter followed by null parameter [Extended Query]#5142

Merged
noizu merged 6 commits intov3.0from
v3.0_bind_fmt_bug_fix_5140
Oct 14, 2025
Merged

Fixed: Invalid UTF-8 byte sequence error (0xff) when relaying text parameter followed by null parameter [Extended Query]#5142
noizu merged 6 commits intov3.0from
v3.0_bind_fmt_bug_fix_5140

Conversation

@rahim-kanji
Copy link
Collaborator

Closes #5140

Now correctly handles empty (len=0) parameter length..
For text parameters (format=0), respect paramLengths[i] if provided, otherwise fall back to strlen().
@gemini-code-assist
Copy link

Summary of Changes

Hello @rahim-kanji, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an "Invalid UTF-8 byte sequence error" that occurred when a text parameter was immediately followed by a null parameter during data relay. The core issue stemmed from libpq not consistently using provided parameter lengths for text types and an oversight in handling null parameter lengths within extended query messages. The changes introduce a more precise length determination for text parameters, improve null parameter parsing, and include a dedicated regression test to prevent future regressions.

Highlights

  • PostgreSQL libpq Parameter Handling: Corrected an issue in libpq's PQsendQueryGuts function where text parameters were not always using the provided paramLengths, leading to incorrect byte sequence errors when followed by null parameters. The fix ensures that if paramLengths is provided and non-negative, its value is used for text parameters, otherwise strlen is used as a fallback.
  • Extended Query Message Parsing Robustness: Enhanced the PgSQL_Field_Reader to more robustly handle parameter lengths in extended query messages. This includes explicitly defining PGSQL_PARAM_NULL and adding a check for malformed lengths (values exceeding INT32_MAX), as well as ensuring the pointer is advanced correctly for both non-NULL and NULL parameters.
  • New Regression Test: Introduced a new regression test (pgsql-reg_test_5140_bind_param_fmt_mix-t.cpp) to validate the correct handling of mixed text and binary parameters, specifically covering the scenario where a text parameter is followed by a null parameter, thereby preventing future regressions of this issue.
  • Build System Update: Updated the deps/Makefile to apply the newly introduced bind_fmt_text.patch during the PostgreSQL build process, integrating the libpq parameter binding logic fix into the dependency build.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue with handling PostgreSQL parameters, particularly when mixing text and null types, which could lead to UTF-8 errors. The fix involves two main parts: patching the underlying libpq to correctly use provided parameter lengths for text-formatted data, and correcting a potential overflow and pointer arithmetic bug in ProxySQL's own protocol parser for extended query messages. A regression test is also added to validate the fix.

My review identifies a bug in the new regression test that involves an out-of-bounds column access, and a potential logic issue in the parsing code that could conflate empty strings with NULL values. Both issues come with suggestions for fixes.

Comment on lines +121 to +124
bool isnull_c2 = PQgetisnull(res, 0, 2);

ok(std::string(c1) == "ABCDEFGHIJKLMN", "col1 length 14 parsed correctly");
ok(isnull_c2, "col3 NULL parsed correctly");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There appears to be an out-of-bounds access in the test. The test table reg_test_5140 has two columns, col1 and col2, so valid column indices are 0 and 1. However, PQgetisnull(res, 0, 2) attempts to access the column with index 2, which does not exist. This should be 1 to check col2. Additionally, the test message in the ok() call refers to col3, which is also incorrect.

Suggested change
bool isnull_c2 = PQgetisnull(res, 0, 2);
ok(std::string(c1) == "ABCDEFGHIJKLMN", "col1 length 14 parsed correctly");
ok(isnull_c2, "col3 NULL parsed correctly");
bool isnull_c2 = PQgetisnull(res, 0, 1);
ok(std::string(c1) == "ABCDEFGHIJKLMN", "col1 length 14 parsed correctly");
ok(isnull_c2, "col2 NULL parsed correctly");

@rahim-kanji rahim-kanji force-pushed the v3.0_bind_fmt_bug_fix_5140 branch from 8b9ed25 to c4a9e40 Compare October 3, 2025 10:28
@rahim-kanji rahim-kanji changed the title Fixed: Invalid UTF-8 byte sequence error (0xff) when relaying text parameter followed by null parameter Fixed: Invalid UTF-8 byte sequence error (0xff) when relaying text parameter followed by null parameter [Extended Query] Oct 3, 2025
@rahim-kanji rahim-kanji force-pushed the v3.0_bind_fmt_bug_fix_5140 branch from c4a9e40 to ee4cd98 Compare October 3, 2025 10:48
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 3, 2025

@noizu noizu merged commit aaefac0 into v3.0 Oct 14, 2025
148 of 154 checks passed
@renecannao renecannao deleted the v3.0_bind_fmt_bug_fix_5140 branch March 7, 2026 20:34
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.

Invalid UTF-8 byte sequence error (0xff) when relaying text parameter followed by null parameter

2 participants