pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitry Mityugov <d.mityugov@postgrespro.ru>
To: pgsql-hackers@postgresql.org
Subject: [PATCH] Use strchr() to search for a single character
Date: Mon, 21 Jul 2025 01:21:38 +0300
Message-ID: <9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru> (raw)

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

Attachments:

  [text/x-diff] 0001-Use-strchr-to-search-for-a-single-character.patch (1.0K, ../9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru/2-0001-Use-strchr-to-search-for-a-single-character.patch)
  download | inline diff:
From: Dmitry Mityugov <d.mityugov@postgrespro.ru>
Date: Mon, 21 Jul 2025 00:50:35 +0300
Subject: [PATCH] Use strchr() to search for a single character

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.
---
 src/port/pgmkdirp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/port/pgmkdirp.c b/src/port/pgmkdirp.c
index d943559760d..7d7cea4dd0e 100644
--- a/src/port/pgmkdirp.c
+++ b/src/port/pgmkdirp.c
@@ -73,7 +73,7 @@ pg_mkdir_p(char *path, int omode)
 		if (p[0] == '/' && p[1] == '/')
 		{
 			/* network drive */
-			p = strstr(p + 2, "/");
+			p = strchr(p + 2, '/');
 			if (p == NULL)
 			{
 				errno = EINVAL;
-- 
2.50.1



view thread (8+ messages)  latest in thread

Message-ID: <9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru>
Permalink:  ../9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru/
Also on:    postgresql.org/message-id/9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: d.mityugov@postgrespro.ru
  Subject: Re: [PATCH] Use strchr() to search for a single character
  In-Reply-To: <9c1beea6c7a5e9fb6677f26620f1f257@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox