Skip to content

Remove unsafe strpos() helper - #70

Merged
darold merged 1 commit into
darold:masterfrom
virendercse:remove-unsafe-strpos
Nov 5, 2026
Merged

darold merged 1 commit into
darold:masterfrom
virendercse:remove-unsafe-strpos

Conversation

@virendercse

Copy link
Copy Markdown
Contributor

strpos() was only used to print the position of the GLOBAL keyword in two DEBUG1 messages, but it was unsafe:

  • malloc(strlen(hay)) leaves no room for the terminating NUL, and the following strncpy() copies exactly strlen(hay) - offset bytes, so the buffer passed to strstr() is not NUL-terminated and strstr() reads past the end of the allocation (heap buffer over-read, confirmed with AddressSanitizer);
  • the malloc'ed buffer was never freed, and neither was the palloc'ed copy returned by asc_toupper(), so every logged CREATE GLOBAL TEMPORARY TABLE leaked the size of the query string;
  • on allocation failure it called exit() from inside a backend, bypassing PostgreSQL's error handling.

The arguments of elog() are only evaluated when the message is going to be emitted, so this code ran whenever DEBUG1 was enabled through client_min_messages or log_min_messages.

The position of GLOBAL in the query string has no diagnostic value (the statement is known to start with CREATE GLOBAL at this point), so remove the helper and the extra argument from the two debug messages instead of rewriting it. The utils/formatting.h include, only needed for asc_toupper(), is removed as well.

(LLM identification and fix with human in the loop)

strpos() was only used to print the position of the GLOBAL keyword in
two DEBUG1 messages, but it was unsafe:

  * malloc(strlen(hay)) leaves no room for the terminating NUL, and the
    following strncpy() copies exactly strlen(hay) - offset bytes, so
    the buffer passed to strstr() is not NUL-terminated and strstr()
    reads past the end of the allocation (heap buffer over-read,
    confirmed with AddressSanitizer);
  * the malloc'ed buffer was never freed, and neither was the palloc'ed
    copy returned by asc_toupper(), so every logged CREATE GLOBAL
    TEMPORARY TABLE leaked the size of the query string;
  * on allocation failure it called exit() from inside a backend,
    bypassing PostgreSQL's error handling.

The arguments of elog() are only evaluated when the message is going
to be emitted, so this code ran whenever DEBUG1 was enabled through
client_min_messages or log_min_messages.

The position of GLOBAL in the query string has no diagnostic value
(the statement is known to start with CREATE GLOBAL at this point), so
remove the helper and the extra argument from the two debug messages
instead of rewriting it. The utils/formatting.h include, only needed
for asc_toupper(), is removed as well.
@darold
darold merged commit 3ccab10 into darold:master Nov 5, 2026
7 checks passed
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.

2 participants