Re: Avoid incomplete copy string (src/backend/access/transam/xlog.c)

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: fabriziomello(at)gmail(dot)com
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Avoid incomplete copy string (src/backend/access/transam/xlog.c)
Date: 2024-06-24 00:31:51
Message-ID: CAEudQAqqRYns_JVGJeCg=wUxozitCDrh=GNvGJdcN=4yKpLD_Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em dom., 23 de jun. de 2024 às 21:08, Fabrízio de Royes Mello <
fabriziomello(at)gmail(dot)com> escreveu:

>
> On Sun, 23 Jun 2024 at 20:51 Ranier Vilela <ranier(dot)vf(at)gmail(dot)com> wrote:
>
>> Hi.
>>
>> In src/include/access/xlogbackup.h, the field *name*
>> has one byte extra to store null-termination.
>>
>> But, in the function *do_pg_backup_start*,
>> I think that is a mistake in the line (8736):
>>
>> memcpy(state->name, backupidstr, strlen(backupidstr));
>>
>> memcpy with strlen does not copy the whole string.
>> strlen returns the exact length of the string, without
>> the null-termination.
>>
>> So, I think this can result in errors,
>> like in the function *build_backup_content*
>> (src/backend/access/transam/xlogbackup.c)
>> Where *appendStringInfo* expects a string with null-termination.
>>
>> appendStringInfo(result, "LABEL: %s\n", state->name);
>>
>> To fix, copy strlen size plus one byte, to include the null-termination.
>>
>
>>
> Doesn’t “sizeof” solve the problem? It take in account the
> null-termination character.

sizeof is is preferable when dealing with constants such as:
memcpy(name, "string test1", sizeof( "string test1");

Using sizeof in this case will always copy MAXPGPATH + 1.
Modern compilers will optimize strlen,
copying fewer bytes.

best regards,
Ranier Vilela

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2024-06-24 00:34:05 Re: replace strtok()
Previous Message Michael Paquier 2024-06-24 00:23:50 Re: Avoid incomplete copy string (src/backend/access/transam/xlog.c)