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 1ueKcl-007vn0-P6 for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 21:34:13 +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 1ueKcj-00EDxt-SA for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 21:34:10 +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 1ueKcj-00EDwP-DD for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 21:34:09 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ueKcg-000IJy-0h for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 21:34:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1753220045; bh=QnyojROsi/FrdHYRCQZIidinhosx8qpjyUzt/lcp10o=; h=Date:From:To:Cc:Subject:In-Reply-To:References:Message-ID:From; b=VX86YwKAEg4liUyU8YuJeLQvRQFypCtGXvd7giuJuht58z1INJpcRzLnXUppDTBsz 3mSA3EH0j/3knIPkoOh7IiG8TeXSL0Ix4xabKn5Ls12oBPlx8UtLqSkMsKKU1AUsIl Xm0c+cEYATVY3PVQQ8tpteHe4YOElInpx8NN0oP+yopr7zteW+dc+0ZXA7rKqClAdk jBvE+LaBF/BoLJtEJOHFrSJ48vgzdChQl67xDBQJU1bBK1AiZECBdDMDz9sXFateM2 woNjPadMNyYkQT00sXSnbp7OzYmhKa9X5UBN+Q+OZQJ6s2CBCfMQgqvYosgJEhufSR u+vQB6BFv9chg== Received: from mail.postgrespro.ru (webmail-slave-mstn.l.postgrespro.ru [192.168.2.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: d.mityugov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 6F28A6029B; Wed, 23 Jul 2025 00:34:05 +0300 (MSK) MIME-Version: 1.0 Date: Wed, 23 Jul 2025 00:34:05 +0300 From: Dmitry Mityugov To: Corey Huinker Cc: pgsql-hackers@postgresql.org Subject: Re: [PATCH] Use strchr() to search for a single character In-Reply-To: References: <9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru> Message-ID: <8d327aa3460fa78d0d1bdcaa46f5df78@postgrespro.ru> X-Sender: d.mityugov@postgrespro.ru Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/07/22 19:07:00 #27635956 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Corey Huinker писал(а) 2025-07-22 22:42: > On Sun, Jul 20, 2025 at 6:21 PM Dmitry Mityugov > wrote: > >> Code in src/port/pgmkdirp.c uses strstr() to find a single character >> in >> a string, but strstr() seems to be too generic for this job. Another >> >> function, strchr(), might be better suited for this purpose, because >> it >> is optimized to search for exactly one character in a string. In >> addition, if strchr() is used, the compiler doesn't have to generate >> a >> terminating \0 byte for the substring, producing slightly smaller >> code. >> I'm attaching the patch. >> >> Regards, >> Dmitry > > Seems like a simple-enough change, not a huge win but probably worth > doing. > > Using ripgrep to search for 'strstr(.*".")' turns up two similar > situations in contrib/fuzzystrmatch/dmetaphone.c, so perhaps we > include those. > > There's also a match in src/bin/pg_rewind/filemap.c, but that one is a > false positive. Thank you for your attention to this problem. The code in contrib/fuzzystrmatch/dmetaphone.c indeed uses several calls to strstr() to search for a single character, but it also uses strstr() to search for strings that consist of more than a single character on adjacent lines, and replacing half of those strstr()s with strchr()s would make the code less consistent in my opinion. What's more important, it seems that this code in contrib/fuzzystrmatch/dmetaphone.c contains a bug. Statement `else if (strstr(s->str, "WITZ"))` at line 317 will never be executed, because if the string contains substring “W”, it will be handled at line 311, `if (strstr(s->str, "W"))`. Probably this bug should be fixed in a separate commit.