Skip to content

Commit 758ce9b

Browse files
committed
Incorporate strerror_r() into src/port/snprintf.c, too.
This provides the features that used to exist in useful_strerror() for users of strerror_r(), too. Also, standardize on the GNU convention that strerror_r returns a char pointer that may not be NULL. I notice that libpq's win32.c contains a variant version of strerror_r that probably ought to be folded into strerror.c. But lacking a Windows environment, I should leave that to somebody else. Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
1 parent 26e9d4d commit 758ce9b

File tree

11 files changed

+99
-83
lines changed

11 files changed

+99
-83
lines changed

src/include/port.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
193193
extern char *pg_strerror(int errnum);
194194
#define strerror pg_strerror
195195

196+
/* Likewise for strerror_r(); note we prefer the GNU API for that */
197+
extern char *pg_strerror_r(int errnum, char *buf, size_t buflen);
198+
#define strerror_r pg_strerror_r
199+
#define PG_STRERROR_R_BUFLEN 256 /* Recommended buffer size for strerror_r */
200+
196201
/* Portable prompt handling */
197202
extern void simple_prompt(const char *prompt, char *destination, size_t destlen,
198203
bool echo);
@@ -428,8 +433,6 @@ extern char *dlerror(void);
428433
#endif
429434

430435
/* thread.h */
431-
extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
432-
433436
#ifndef WIN32
434437
extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
435438
size_t buflen, struct passwd **result);

src/interfaces/libpq/fe-auth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,11 @@ pg_local_sendauth(PGconn *conn)
756756

757757
if (sendmsg(conn->sock, &msg, 0) == -1)
758758
{
759-
char sebuf[256];
759+
char sebuf[PG_STRERROR_R_BUFLEN];
760760

761761
printfPQExpBuffer(&conn->errorMessage,
762762
"pg_local_sendauth: sendmsg: %s\n",
763-
pqStrerror(errno, sebuf, sizeof(sebuf)));
763+
strerror_r(errno, sebuf, sizeof(sebuf)));
764764
return STATUS_ERROR;
765765
}
766766
return STATUS_OK;
@@ -1098,7 +1098,7 @@ pg_fe_getauthname(PQExpBuffer errorMessage)
10981098
printfPQExpBuffer(errorMessage,
10991099
libpq_gettext("could not look up local user ID %d: %s\n"),
11001100
(int) user_id,
1101-
pqStrerror(pwerr, pwdbuf, sizeof(pwdbuf)));
1101+
strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
11021102
else
11031103
printfPQExpBuffer(errorMessage,
11041104
libpq_gettext("local user with ID %d does not exist\n"),

src/interfaces/libpq/fe-connect.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ connectNoDelay(PGconn *conn)
14591459
(char *) &on,
14601460
sizeof(on)) < 0)
14611461
{
1462-
char sebuf[256];
1462+
char sebuf[PG_STRERROR_R_BUFLEN];
14631463

14641464
appendPQExpBuffer(&conn->errorMessage,
14651465
libpq_gettext("could not set socket to TCP no delay mode: %s\n"),
@@ -1480,7 +1480,7 @@ connectNoDelay(PGconn *conn)
14801480
static void
14811481
connectFailureMessage(PGconn *conn, int errorno)
14821482
{
1483-
char sebuf[256];
1483+
char sebuf[PG_STRERROR_R_BUFLEN];
14841484

14851485
#ifdef HAVE_UNIX_SOCKETS
14861486
if (IS_AF_UNIX(conn->raddr.addr.ss_family))
@@ -1637,7 +1637,7 @@ setKeepalivesIdle(PGconn *conn)
16371637
if (setsockopt(conn->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
16381638
(char *) &idle, sizeof(idle)) < 0)
16391639
{
1640-
char sebuf[256];
1640+
char sebuf[PG_STRERROR_R_BUFLEN];
16411641

16421642
appendPQExpBuffer(&conn->errorMessage,
16431643
libpq_gettext("setsockopt(%s) failed: %s\n"),
@@ -1671,7 +1671,7 @@ setKeepalivesInterval(PGconn *conn)
16711671
if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPINTVL,
16721672
(char *) &interval, sizeof(interval)) < 0)
16731673
{
1674-
char sebuf[256];
1674+
char sebuf[PG_STRERROR_R_BUFLEN];
16751675

16761676
appendPQExpBuffer(&conn->errorMessage,
16771677
libpq_gettext("setsockopt(%s) failed: %s\n"),
@@ -1706,7 +1706,7 @@ setKeepalivesCount(PGconn *conn)
17061706
if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPCNT,
17071707
(char *) &count, sizeof(count)) < 0)
17081708
{
1709-
char sebuf[256];
1709+
char sebuf[PG_STRERROR_R_BUFLEN];
17101710

17111711
appendPQExpBuffer(&conn->errorMessage,
17121712
libpq_gettext("setsockopt(%s) failed: %s\n"),
@@ -2036,7 +2036,7 @@ PQconnectPoll(PGconn *conn)
20362036
bool reset_connection_state_machine = false;
20372037
bool need_new_connection = false;
20382038
PGresult *res;
2039-
char sebuf[256];
2039+
char sebuf[PG_STRERROR_R_BUFLEN];
20402040
int optval;
20412041
PQExpBufferData savedMessage;
20422042

@@ -2580,7 +2580,7 @@ PQconnectPoll(PGconn *conn)
25802580
else
25812581
appendPQExpBuffer(&conn->errorMessage,
25822582
libpq_gettext("could not get peer credentials: %s\n"),
2583-
pqStrerror(errno, sebuf, sizeof(sebuf)));
2583+
strerror_r(errno, sebuf, sizeof(sebuf)));
25842584
goto error_return;
25852585
}
25862586

@@ -2591,7 +2591,7 @@ PQconnectPoll(PGconn *conn)
25912591
appendPQExpBuffer(&conn->errorMessage,
25922592
libpq_gettext("could not look up local user ID %d: %s\n"),
25932593
(int) uid,
2594-
pqStrerror(passerr, sebuf, sizeof(sebuf)));
2594+
strerror_r(passerr, sebuf, sizeof(sebuf)));
25952595
else
25962596
appendPQExpBuffer(&conn->errorMessage,
25972597
libpq_gettext("local user with ID %d does not exist\n"),
@@ -3953,7 +3953,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
39533953
{
39543954
int save_errno = SOCK_ERRNO;
39553955
pgsocket tmpsock = PGINVALID_SOCKET;
3956-
char sebuf[256];
3956+
char sebuf[PG_STRERROR_R_BUFLEN];
39573957
int maxlen;
39583958
struct
39593959
{

src/interfaces/libpq/fe-lobj.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ lo_import_internal(PGconn *conn, const char *filename, Oid oid)
694694
char buf[LO_BUFSIZE];
695695
Oid lobjOid;
696696
int lobj;
697-
char sebuf[256];
697+
char sebuf[PG_STRERROR_R_BUFLEN];
698698

699699
/*
700700
* open the file to be read in
@@ -704,7 +704,7 @@ lo_import_internal(PGconn *conn, const char *filename, Oid oid)
704704
{ /* error */
705705
printfPQExpBuffer(&conn->errorMessage,
706706
libpq_gettext("could not open file \"%s\": %s\n"),
707-
filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
707+
filename, strerror_r(errno, sebuf, sizeof(sebuf)));
708708
return InvalidOid;
709709
}
710710

@@ -760,7 +760,7 @@ lo_import_internal(PGconn *conn, const char *filename, Oid oid)
760760
printfPQExpBuffer(&conn->errorMessage,
761761
libpq_gettext("could not read from file \"%s\": %s\n"),
762762
filename,
763-
pqStrerror(save_errno, sebuf, sizeof(sebuf)));
763+
strerror_r(save_errno, sebuf, sizeof(sebuf)));
764764
return InvalidOid;
765765
}
766766

@@ -789,7 +789,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
789789
tmp;
790790
char buf[LO_BUFSIZE];
791791
int lobj;
792-
char sebuf[256];
792+
char sebuf[PG_STRERROR_R_BUFLEN];
793793

794794
/*
795795
* open the large object.
@@ -814,7 +814,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
814814
printfPQExpBuffer(&conn->errorMessage,
815815
libpq_gettext("could not open file \"%s\": %s\n"),
816816
filename,
817-
pqStrerror(save_errno, sebuf, sizeof(sebuf)));
817+
strerror_r(save_errno, sebuf, sizeof(sebuf)));
818818
return -1;
819819
}
820820

@@ -834,7 +834,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
834834
printfPQExpBuffer(&conn->errorMessage,
835835
libpq_gettext("could not write to file \"%s\": %s\n"),
836836
filename,
837-
pqStrerror(save_errno, sebuf, sizeof(sebuf)));
837+
strerror_r(save_errno, sebuf, sizeof(sebuf)));
838838
return -1;
839839
}
840840
}
@@ -857,7 +857,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
857857
{
858858
printfPQExpBuffer(&conn->errorMessage,
859859
libpq_gettext("could not write to file \"%s\": %s\n"),
860-
filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
860+
filename, strerror_r(errno, sebuf, sizeof(sebuf)));
861861
result = -1;
862862
}
863863

src/interfaces/libpq/fe-misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
10711071

10721072
if (result < 0)
10731073
{
1074-
char sebuf[256];
1074+
char sebuf[PG_STRERROR_R_BUFLEN];
10751075

10761076
printfPQExpBuffer(&conn->errorMessage,
10771077
libpq_gettext("select() failed: %s\n"),

src/interfaces/libpq/fe-secure-openssl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pgtls_read(PGconn *conn, void *ptr, size_t len)
142142
{
143143
ssize_t n;
144144
int result_errno = 0;
145-
char sebuf[256];
145+
char sebuf[PG_STRERROR_R_BUFLEN];
146146
int err;
147147
unsigned long ecode;
148148

@@ -272,7 +272,7 @@ pgtls_write(PGconn *conn, const void *ptr, size_t len)
272272
{
273273
ssize_t n;
274274
int result_errno = 0;
275-
char sebuf[256];
275+
char sebuf[PG_STRERROR_R_BUFLEN];
276276
int err;
277277
unsigned long ecode;
278278

@@ -443,7 +443,7 @@ pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
443443

444444
return cert_hash;
445445
}
446-
#endif /* HAVE_X509_GET_SIGNATURE_NID */
446+
#endif /* HAVE_X509_GET_SIGNATURE_NID */
447447

448448
/* ------------------------------------------------------------ */
449449
/* OpenSSL specific code */
@@ -780,7 +780,7 @@ initialize_SSL(PGconn *conn)
780780
struct stat buf;
781781
char homedir[MAXPGPATH];
782782
char fnbuf[MAXPGPATH];
783-
char sebuf[256];
783+
char sebuf[PG_STRERROR_R_BUFLEN];
784784
bool have_homedir;
785785
bool have_cert;
786786
bool have_rootcert;
@@ -941,7 +941,7 @@ initialize_SSL(PGconn *conn)
941941
{
942942
printfPQExpBuffer(&conn->errorMessage,
943943
libpq_gettext("could not open certificate file \"%s\": %s\n"),
944-
fnbuf, pqStrerror(errno, sebuf, sizeof(sebuf)));
944+
fnbuf, strerror_r(errno, sebuf, sizeof(sebuf)));
945945
SSL_CTX_free(SSL_context);
946946
return -1;
947947
}
@@ -1212,7 +1212,7 @@ open_client_SSL(PGconn *conn)
12121212

12131213
case SSL_ERROR_SYSCALL:
12141214
{
1215-
char sebuf[256];
1215+
char sebuf[PG_STRERROR_R_BUFLEN];
12161216

12171217
if (r == -1)
12181218
printfPQExpBuffer(&conn->errorMessage,

src/interfaces/libpq/fe-secure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
233233
{
234234
ssize_t n;
235235
int result_errno = 0;
236-
char sebuf[256];
236+
char sebuf[PG_STRERROR_R_BUFLEN];
237237

238238
n = recv(conn->sock, ptr, len, 0);
239239

@@ -311,7 +311,7 @@ pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len)
311311
ssize_t n;
312312
int flags = 0;
313313
int result_errno = 0;
314-
char sebuf[256];
314+
char sebuf[PG_STRERROR_R_BUFLEN];
315315

316316
DECLARE_SIGPIPE_INFO(spinfo);
317317

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ extern char *libpq_ngettext(const char *msgid, const char *msgid_plural, unsigne
773773
#define SOCK_ERRNO_SET(e) WSASetLastError(e)
774774
#else
775775
#define SOCK_ERRNO errno
776-
#define SOCK_STRERROR pqStrerror
776+
#define SOCK_STRERROR strerror_r
777777
#define SOCK_ERRNO_SET(e) (errno = (e))
778778
#endif
779779

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy