Re: Possible replace of strncpy on xactdesc.c

From: Daniel Gustafsson <daniel(at)yesql(dot)se>
To: Mario González Troncoso <gonzalemario(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Possible replace of strncpy on xactdesc.c
Date: 2026-07-06 14:38:37
Message-ID: 2F655280-ACEB-47C8-9F0A-35709B96373F@yesql.se
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 3 Jul 2026, at 05:47, Mario González Troncoso <gonzalemario(at)gmail(dot)com> wrote:

> I've been doing some research about if we could replace strncpy() in
> some code. It seems I found one and I'd like your opinion on it to
> determine if it's worth the change.
>
> Basically what I found and IMO it's worth the change is on file xactdesc.c
>
> diff --git a/src/backend/access/rmgrdesc/xactdesc.c
> b/src/backend/access/rmgrdesc/xactdesc.c
> index 4f53d3035cc..0f466a4c27c 100644
> --- a/src/backend/access/rmgrdesc/xactdesc.c
> +++ b/src/backend/access/rmgrdesc/xactdesc.c
> @@ -256,7 +256,7 @@ ParsePrepareRecord(uint8 info, xl_xact_prepare
> *xlrec, xl_xact_parsed_prepare *p
> parsed->nabortstats = xlrec->nabortstats;
> parsed->nmsgs = xlrec->ninvalmsgs;
>
> - strncpy(parsed->twophase_gid, bufptr, xlrec->gidlen);
> + strlcpy(parsed->twophase_gid, bufptr, sizeof(parsed->twophase_gid));
> bufptr += MAXALIGN(xlrec->gidlen);

As a general rule it's a good idea to replace strncpy with strlcpy.

> The other functions that are inside the file already use strlcpy() so
> maybe the use of current strncpy() on xactdesc.c is just code that
> comes from the refactor itself.

It was introduced in 1eb6d6527aae in twophase.c and then moved to xaxtdesc.c in
the above mentioned commit.

> I'll send a proper patch once some feedback is received but at least
> it's compiling and passing local tests.

Sounds good, please send a patch.

--
Daniel Gustafsson

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexander Pyhalov 2026-07-06 14:42:42 Re: Asynchronous MergeAppend
Previous Message Amit Langote 2026-07-06 14:29:18 Re: PG19 FK fast path: OOB write and missed FK checks during batched