Обсуждение: replace strtok()
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$<iterations>:<salt>$<storedkey>:<serverkey> with strtok(), then SCRAM-SHA-256$$<iterations>::<salt>$$<storedkey>::<serverkey> 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. [0]: https://www.postgresql.org/message-id/856e5ec3-879f-42ee-8258-8bcc6ec9bdea@eisentraut.org
Вложения
Em ter., 18 de jun. de 2024 às 04:18, Peter Eisentraut <peter@eisentraut.org> 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$<iterations>:<salt>$<storedkey>:<serverkey>
with strtok(), then
SCRAM-SHA-256$$<iterations>::<salt>$$<storedkey>::<serverkey>
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.
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
Вложения
At Tue, 18 Jun 2024 09:18:28 +0200, Peter Eisentraut <peter@eisentraut.org> wrote in > 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$<iterations>:<salt>$<storedkey>:<serverkey> > > with strtok(), then > > SCRAM-SHA-256$$<iterations>::<salt>$$<storedkey>::<serverkey> > > 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(). I agree with the distinction. > A reviewer job here would be to check whether I made that distinction > correctly in each case. 0001 and 0002 look correct to me regarding that distinction. They applied correctly to the master HEAD and all tests passed on Linux. > 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. 0003 looks fine and successfully built and seems working on an MSVC build. About 0004, Cygwin seems to have its own strtok_r, but I haven't checked how that fact affects the build. > [0]: > https://www.postgresql.org/message-id/856e5ec3-879f-42ee-8258-8bcc6ec9bdea@eisentraut.org regards. -- Kyotaro Horiguchi NTT Open Source Software Center
On 18.06.24 13:43, Ranier Vilela wrote: > 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. This would not be future-proof. In C23, if you pass a const char * into strchr(), you also get a const char * as a result. And in this case, we do write into the area pointed to by the result. So with a const char *token, this whole thing would not compile cleanly under C23. > I found another implementation of strsep, it seems lighter to me. > I will attach it for consideration, however, I have not done any testing. Yeah, surely there are many possible implementations. I'm thinking, since we already took other str*() functions from OpenBSD, it makes sense to do this here as well, so we have only one source to deal with.
Peter Eisentraut <peter@eisentraut.org> writes: > On 18.06.24 13:43, Ranier Vilela wrote: >> I found another implementation of strsep, it seems lighter to me. >> I will attach it for consideration, however, I have not done any testing. > Yeah, surely there are many possible implementations. I'm thinking, > since we already took other str*() functions from OpenBSD, it makes > sense to do this here as well, so we have only one source to deal with. Why not use strpbrk? That's equally thread-safe, it's been there since C89, and it doesn't have the problem that you can't find out which of the delimiter characters was found. regards, tom lane
On Sat, Jun 22, 2024 at 11:48:21AM -0400, Tom Lane wrote: > Peter Eisentraut <peter@eisentraut.org> writes: > > On 18.06.24 13:43, Ranier Vilela wrote: > >> I found another implementation of strsep, it seems lighter to me. > >> I will attach it for consideration, however, I have not done any testing. > > > Yeah, surely there are many possible implementations. I'm thinking, > > since we already took other str*() functions from OpenBSD, it makes > > sense to do this here as well, so we have only one source to deal with. > > Why not use strpbrk? That's equally thread-safe, it's been there > since C89, and it doesn't have the problem that you can't find out > which of the delimiter characters was found. Yeah, strpbrk() has been used in the tree as far as 2003 without any port/ implementation. -- Michael
Вложения
On 24.06.24 02:34, Michael Paquier wrote: > On Sat, Jun 22, 2024 at 11:48:21AM -0400, Tom Lane wrote: >> Peter Eisentraut <peter@eisentraut.org> writes: >>> On 18.06.24 13:43, Ranier Vilela wrote: >>>> I found another implementation of strsep, it seems lighter to me. >>>> I will attach it for consideration, however, I have not done any testing. >> >>> Yeah, surely there are many possible implementations. I'm thinking, >>> since we already took other str*() functions from OpenBSD, it makes >>> sense to do this here as well, so we have only one source to deal with. >> >> Why not use strpbrk? That's equally thread-safe, it's been there >> since C89, and it doesn't have the problem that you can't find out >> which of the delimiter characters was found. > > Yeah, strpbrk() has been used in the tree as far as 2003 without any > port/ implementation. The existing uses of strpbrk() are really just checking whether some characters exist in a string, more like an enhanced strchr(). I don't see any uses for tokenizing a string like strtok() or strsep() would do. I think that would look quite cumbersome. So I think a simpler and more convenient abstraction like strsep() would still be worthwhile.