Skip to content

Commit 1d89026

Browse files
committed
Implement error checking for pthreads calls in thread-safe mode. They really
should always succeed, but in the likely event of a failure we would previously fall through *without locking* - the new code will exit(1). Printing the error message on stderr will not work for all applications, but it's better than nothing at all - and our API doesn't provide a way to return the error to the caller.
1 parent 0ff81a5 commit 1d89026

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.357 2008/03/31 02:43:14 tgl Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.358 2008/05/16 18:30:53 mha Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3835,14 +3835,23 @@ default_threadlock(int acquire)
38353835
while (InterlockedExchange(&mutex_initlock, 1) == 1)
38363836
/* loop, another thread own the lock */ ;
38373837
if (singlethread_lock == NULL)
3838-
pthread_mutex_init(&singlethread_lock, NULL);
3838+
{
3839+
if (pthread_mutex_init(&singlethread_lock, NULL))
3840+
PGTHREAD_ERROR("failed to initialize mutex");
3841+
}
38393842
InterlockedExchange(&mutex_initlock, 0);
38403843
}
38413844
#endif
38423845
if (acquire)
3843-
pthread_mutex_lock(&singlethread_lock);
3846+
{
3847+
if (pthread_mutex_lock(&singlethread_lock))
3848+
PGTHREAD_ERROR("failed to lock mutex");
3849+
}
38443850
else
3845-
pthread_mutex_unlock(&singlethread_lock);
3851+
{
3852+
if (pthread_mutex_unlock(&singlethread_lock))
3853+
PGTHREAD_ERROR("failed to unlock mutex");
3854+
}
38463855
#endif
38473856
}
38483857

src/interfaces/libpq/fe-secure.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.104 2008/03/31 02:43:14 tgl Exp $
14+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.105 2008/05/16 18:30:53 mha Exp $
1515
*
1616
* NOTES
1717
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -796,12 +796,21 @@ static void
796796
pq_lockingcallback(int mode, int n, const char *file, int line)
797797
{
798798
if (mode & CRYPTO_LOCK)
799-
pthread_mutex_lock(&pq_lockarray[n]);
799+
{
800+
if (pthread_mutex_lock(&pq_lockarray[n]))
801+
PGTHREAD_ERROR("failed to lock mutex");
802+
}
800803
else
801-
pthread_mutex_unlock(&pq_lockarray[n]);
804+
{
805+
if (pthread_mutex_unlock(&pq_lockarray[n]))
806+
PGTHREAD_ERROR("failed to unlock mutex");
807+
}
802808
}
803809
#endif /* ENABLE_THREAD_SAFETY */
804810

811+
/*
812+
* Also see similar code in fe-connect.c, default_threadlock()
813+
*/
805814
static int
806815
init_ssl_system(PGconn *conn)
807816
{
@@ -817,11 +826,15 @@ init_ssl_system(PGconn *conn)
817826
while (InterlockedExchange(&mutex_initlock, 1) == 1)
818827
/* loop, another thread own the lock */ ;
819828
if (init_mutex == NULL)
820-
pthread_mutex_init(&init_mutex, NULL);
829+
{
830+
if (pthread_mutex_init(&init_mutex, NULL))
831+
return -1;
832+
}
821833
InterlockedExchange(&mutex_initlock, 0);
822834
}
823835
#endif
824-
pthread_mutex_lock(&init_mutex);
836+
if (pthread_mutex_lock(&init_mutex))
837+
return -1;
825838

826839
if (pq_initssllib && pq_lockarray == NULL)
827840
{
@@ -836,7 +849,10 @@ init_ssl_system(PGconn *conn)
836849
return -1;
837850
}
838851
for (i = 0; i < CRYPTO_num_locks(); i++)
839-
pthread_mutex_init(&pq_lockarray[i], NULL);
852+
{
853+
if (pthread_mutex_init(&pq_lockarray[i], NULL))
854+
return -1;
855+
}
840856

841857
CRYPTO_set_locking_callback(pq_lockingcallback);
842858
}

src/interfaces/libpq/libpq-int.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.129 2008/01/01 19:46:00 momjian Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.130 2008/05/16 18:30:53 mha Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -439,6 +439,13 @@ extern bool pqGetHomeDirectory(char *buf, int bufsize);
439439
#ifdef ENABLE_THREAD_SAFETY
440440
extern pgthreadlock_t pg_g_threadlock;
441441

442+
#define PGTHREAD_ERROR(msg) \
443+
do { \
444+
fprintf(stderr, "%s\n", msg); \
445+
exit(1); \
446+
} while (0)
447+
448+
442449
#define pglock_thread() pg_g_threadlock(true)
443450
#define pgunlock_thread() pg_g_threadlock(false)
444451
#else

src/interfaces/libpq/pthread-win32.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.15 2008/01/01 19:46:00 momjian Exp $
8+
* $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.16 2008/05/16 18:30:53 mha Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -32,20 +32,27 @@ pthread_getspecific(pthread_key_t key)
3232
return NULL;
3333
}
3434

35-
void
35+
int
3636
pthread_mutex_init(pthread_mutex_t *mp, void *attr)
3737
{
3838
*mp = CreateMutex(0, 0, 0);
39+
if (*mp == NULL)
40+
return 1;
41+
return 0;
3942
}
4043

41-
void
44+
int
4245
pthread_mutex_lock(pthread_mutex_t *mp)
4346
{
44-
WaitForSingleObject(*mp, INFINITE);
47+
if (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0)
48+
return 1;
49+
return 0;
4550
}
4651

47-
void
52+
int
4853
pthread_mutex_unlock(pthread_mutex_t *mp)
4954
{
50-
ReleaseMutex(*mp);
55+
if (!ReleaseMutex(*mp))
56+
return 1;
57+
return 0;
5158
}

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