From e8b500f4caa3fc308de50e96c7d90bdbe888d224 Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Sat, 11 Nov 2017 17:02:06 +0100
Subject: [PATCH] pgsql: Make salted-password-cache threadsafe.

---
 lib/modules/Sql.pmod/pgsql_util.pmod | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index 0b9ca3433a..fd1071b850 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -93,18 +93,24 @@ private Regexp iregexp(string expr) {
   return Regexp(ret->read());
 }
 
+private Thread.Mutex cachemutex = Thread.Mutex();
 private string cached_pass, cached_salt, cached_SaltedPassword;
 private int cached_iters;
 
 final string get_salted_password(string pass, string salt, int iters) {
-  return cached_pass == pass && cached_salt == salt && cached_iters == iters
-   && cached_SaltedPassword;
+  Thread.MutexKey lock = cachemutex->lock();
+  string ret = cached_pass == pass && cached_salt == salt
+   && cached_iters == iters && cached_SaltedPassword;
+  lock = 0;
+  return ret;
 }
 
 final void set_salted_password(string pass, string salt, int iters,
  string SaltedPassword) {
+  Thread.MutexKey lock = cachemutex->lock();
   cached_pass = pass; cached_salt = salt; cached_iters = iters;
   cached_SaltedPassword = SaltedPassword;
+  lock = 0;
 }
 
 final void closestatement(bufcon|conxsess plugbuffer,string oldprep) {
-- 
GitLab