Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sJXFX-005Pyi-51 for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jun 2024 11:43:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sJXFU-00BqMz-Ce for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jun 2024 11:43:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sJXFU-00BqMr-1K for pgsql-hackers@lists.postgresql.org; Tue, 18 Jun 2024 11:43:40 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sJXFS-002HJl-6C for pgsql-hackers@postgresql.org; Tue, 18 Jun 2024 11:43:40 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a6efe62f583so533170666b.3 for ; Tue, 18 Jun 2024 04:43:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718711016; x=1719315816; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uX/hp3bad+vXnAUp09kvbh/Wmm/M7qnZqzA0ULg7OaY=; b=eXPuNBL2tRan+KW/me+9vhs2Q1kKMuipzF5DfTydrxis+phzvFcIqYrhe5whsYhTx2 ynhgMlD+D7SHCTt60DdXkfhGrN2JQ7AMC5aFcPTkzHydLnb6HnS/bdm7h/Gd5J0gMMW6 ABcEYZlHyFlkEa5wONRD5RvsW4FB4X2/2+LgD2tNX7R4w+tOvsL11caFxLN2elpverx4 4XQiURNEHMyNoOH2xcCJaWiaQknQLkkBIMQ4dquu9EyzaAWCyAHqkqsqEEdNrhlAQHHK g32+5WAs/YW6Y7htaLOPGo821tV8LpBIusrP5HP/Z/P11ELPgjwwkWtiTr2btXWFpkAd juDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718711016; x=1719315816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uX/hp3bad+vXnAUp09kvbh/Wmm/M7qnZqzA0ULg7OaY=; b=TuB7gnO6xkXtTEVIfkhLb1NLDuXWL8FkRauq8/kPZ9wmS2gqJDNcTunFw0z9J/woQQ iWful7BZBZrhwk3tdCg6RWdsXmjy1G0Fylrsiki5094y8XmRBHHevCILGqDFsBwuwi0I WwFdMGksatBsbNHJExLzRF4P5bP8SupQ0WHVPZitPBRK/EDOVsBETQPuSU9ss801JB1q 1BjONE+y759pEeh594caVG1VsiyOstYHXUh/Pxq2u5OxlY1LasJoCkt5er7huxzVED38 EtvmsUQnsJda6NiPF2vperACdBlPJThJUp/xlpHab4lOpvvizhwOy9OlC97p6li2Abek 5+Vw== X-Gm-Message-State: AOJu0Ywr3LfSYmLmPElhndTZDzLj62yYt8DjFhHKA3NfiiDTc81Qu6Dc oRO+cDhWbXBmbIfY7n2bsQ0/eBmxuGHWlp6vTCa07l6ePWZog0eVl3Oyu7hiQBHogC26Qng0450 Ipll5l/5Lgv1+Qb68g97o0zQ9iH0iYUiH X-Google-Smtp-Source: AGHT+IHL48vs6Nzx/Uh/7o76Ue9X6PtXGNUFM8igTPAaqdl9HTeSm2Fn0ZNbiCj1/OZRseZkp4yo5TIrXzRRgsmc4Ys= X-Received: by 2002:a17:906:4748:b0:a6f:10d0:f3e0 with SMTP id a640c23a62f3a-a6f60d27cccmr726933166b.21.1718711015637; Tue, 18 Jun 2024 04:43:35 -0700 (PDT) MIME-Version: 1.0 References: <79692bf9-17d3-41e6-b9c9-fc8c3944222a@eisentraut.org> In-Reply-To: <79692bf9-17d3-41e6-b9c9-fc8c3944222a@eisentraut.org> From: Ranier Vilela Date: Tue, 18 Jun 2024 08:43:23 -0300 Message-ID: Subject: Re: replace strtok() To: Peter Eisentraut Cc: pgsql-hackers Content-Type: multipart/mixed; boundary="0000000000008f2269061b289796" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000008f2269061b289796 Content-Type: multipart/alternative; boundary="0000000000008f2268061b289794" --0000000000008f2268061b289794 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Em ter., 18 de jun. de 2024 =C3=A0s 04:18, Peter Eisentraut escreveu: > Under the topic of getting rid of thread-unsafe functions in the backend > [0], here is a patch series to deal with strtok(). > > Of course, strtok() is famously not thread-safe and can be replaced by > strtok_r(). But it also has the wrong semantics in some cases, because > it considers adjacent delimiters to be one delimiter. So if you parse > > SCRAM-SHA-256$:$: > > with strtok(), then > > SCRAM-SHA-256$$::$$:: > > parses just the same. In many cases, this is arguably wrong and could > hide mistakes. > > So I'm suggesting to use strsep() in those places. strsep() is > nonstandard but widely available. > > There are a few places where strtok() has the right semantics, such as > parsing tokens separated by whitespace. For those, I'm using strtok_r(). > > A reviewer job here would be to check whether I made that distinction > correctly in each case. > > On the portability side, I'm including a port/ replacement for strsep() > and some workaround to get strtok_r() for Windows. I have included > these here as separate patches for clarity. > +1 For making the code thread-safe. But I would like to see more const char * where this is possible. For example, in pg_locale.c IMO, the token variable can be const char *. At least strchr expects a const char * as the first parameter. I found another implementation of strsep, it seems lighter to me. I will attach it for consideration, however, I have not done any testing. best regards, Ranier Vilela --0000000000008f2268061b289794 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Em ter., 18 de jun. de 2024 =C3=A0s 04:18, Peter Eisentraut <peter@eisentraut.org> escreveu:<= br>
Under the topic = of getting rid of thread-unsafe functions in the backend
[0], here is a patch series to deal with strtok().

Of course, strtok() is famously not thread-safe and can be replaced by
strtok_r().=C2=A0 But it also has the wrong semantics in some cases, becaus= e
it considers adjacent delimiters to be one delimiter.=C2=A0 So if you parse=

=C2=A0 =C2=A0 =C2=A0SCRAM-SHA-256$<iterations>:<salt>$<store= dkey>:<serverkey>

with strtok(), then

=C2=A0 =C2=A0 =C2=A0SCRAM-SHA-256$$<iterations>::<salt>$$<st= oredkey>::<serverkey>

parses just the same.=C2=A0 In many cases, this is arguably wrong and could=
hide mistakes.

So I'm suggesting to use strsep() in those places.=C2=A0 strsep() is nonstandard but widely available.

There are a few places where strtok() has the right semantics, such as
parsing tokens separated by whitespace.=C2=A0 For those, I'm using strt= ok_r().

A reviewer job here would be to check whether I made that distinction
correctly in each case.

On the portability side, I'm including a port/ replacement for strsep()=
and some workaround to get strtok_r() for Windows.=C2=A0 I have included these here as separate patches for clarity.
+1 For mak= ing the code thread-safe.
But I would like to see more const char * wher= e this is possible.

For example, in pg_locale.c
IMO, the token va= riable can be const char *.

At least strchr expects a const char * a= s the first parameter.

I found another implementation of strsep, it seems li= ghter to me.
I will attach it for consideration, however, I have n= ot done any testing.=C2=A0

best regards,
Ranier Vilela
--0000000000008f2268061b289794-- --0000000000008f2269061b289796 Content-Type: text/plain; charset="US-ASCII"; name="strsep.c" Content-Disposition: attachment; filename="strsep.c" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lxkc4h4w0 Lyogc3Ryc2VwLmgNCiAqDQogKiAgUHJvdmlkZXMgdGhlIDQuNEJTRCBzdHJzZXAoMykgZnVuY3Rp b24gZm9yIHRob3NlIHRoYXQgZG9uJ3QgaGF2ZSBpdC4NCiAqDQogKiAgQ29weXJpZ2h0IDIwMTEg TWljaGFlbCBUaG9tYXMgR3JlZXINCiAqICBEaXN0cmlidXRlZCB1bmRlciB0aGUgQm9vc3QgU29m dHdhcmUgTGljZW5zZ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