Skip to content

Commit b67c9c1

Browse files
committed
Fix poor errno handling in libpq's version of our custom OpenSSL BIO.
Thom Brown reported that SSL connections didn't seem to work on Windows in 9.5. Asif Naeem figured out that the cause was my_sock_read() looking at "errno" when it needs to look at "SOCK_ERRNO". This mistake was introduced in commit 680513a, which cloned the backend's custom SSL BIO code into libpq, and didn't translate the errno handling properly. Moreover, it introduced unnecessary errno save/restore logic, which was particularly confusing because it was incomplete; and it failed to check for all three of EINTR, EAGAIN, and EWOULDBLOCK in my_sock_write. (That might not be necessary; but since we're copying well-tested backend code that does do that, it seems prudent to copy it faithfully.)
1 parent ce58502 commit b67c9c1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -1598,14 +1598,13 @@ static int
15981598
my_sock_read(BIO *h, char *buf, int size)
15991599
{
16001600
int res;
1601-
int save_errno;
16021601

16031602
res = pqsecure_raw_read((PGconn *) h->ptr, buf, size);
1604-
save_errno = errno;
16051603
BIO_clear_retry_flags(h);
16061604
if (res < 0)
16071605
{
1608-
switch (save_errno)
1606+
/* If we were interrupted, tell caller to retry */
1607+
switch (SOCK_ERRNO)
16091608
{
16101609
#ifdef EAGAIN
16111610
case EAGAIN:
@@ -1622,24 +1621,33 @@ my_sock_read(BIO *h, char *buf, int size)
16221621
}
16231622
}
16241623

1625-
errno = save_errno;
16261624
return res;
16271625
}
16281626

16291627
static int
16301628
my_sock_write(BIO *h, const char *buf, int size)
16311629
{
16321630
int res;
1633-
int save_errno;
16341631

16351632
res = pqsecure_raw_write((PGconn *) h->ptr, buf, size);
1636-
save_errno = errno;
16371633
BIO_clear_retry_flags(h);
16381634
if (res <= 0)
16391635
{
1640-
if (save_errno == EINTR)
1636+
/* If we were interrupted, tell caller to retry */
1637+
switch (SOCK_ERRNO)
16411638
{
1642-
BIO_set_retry_write(h);
1639+
#ifdef EAGAIN
1640+
case EAGAIN:
1641+
#endif
1642+
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1643+
case EWOULDBLOCK:
1644+
#endif
1645+
case EINTR:
1646+
BIO_set_retry_write(h);
1647+
break;
1648+
1649+
default:
1650+
break;
16431651
}
16441652
}
16451653

0 commit comments

Comments
 (0)