[Git][NTPsec/ntpsec][master] Make lib_getbuf() thread-safe.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Mar 26 11:55:04 UTC 2019



Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
2bb7f8fb by Eric S. Raymond at 2019-03-26T11:54:28Z
Make lib_getbuf() thread-safe.

- - - - -


1 changed file:

- libntp/lib_strbuf.c


Changes:

=====================================
libntp/lib_strbuf.c
=====================================
@@ -1,6 +1,8 @@
 /*
  * lib_strbuf - library string storage
  */
+#include <pthread.h>
+
 #include "config.h"
 
 #include "isc_netaddr.h"
@@ -14,8 +16,18 @@
  */
 int		debug;
 
+static pthread_mutex_t cookie_lock = PTHREAD_MUTEX_INITIALIZER;
+
 /*
- * Macro to get a pointer to the next buffer
+ * Function to get a pointer to the next buffer.  Needs to be thread-safe because
+ * it's used in callers that need to be thread-safe, notably msyslog.  For the 
+ * same reason, we don't try to log a log-acquisition failure.
+ *
+ * ESR: Yes, this is ugly and kludgy. I'm not getting rid of of it
+ * because I have an eye forward on translation to a garbage-collected
+ * language, at which point something with this behavior will be
+ * better than all the conttorions we'd have to go through to get rid
+ * of it in C.
  */
 char *lib_getbuf(void)
 {
@@ -23,8 +35,10 @@ char *lib_getbuf(void)
     static int		lib_nextbuf;
     char *bufp;
 
+    pthread_mutex_lock(&cookie_lock);
     ZERO(lib_stringbuf[lib_nextbuf]);
     (bufp) = &lib_stringbuf[lib_nextbuf++][0];
     lib_nextbuf %= (int)COUNTOF(lib_stringbuf);
+    pthread_mutex_unlock(&cookie_lock);
     return bufp;
 }



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/2bb7f8fb92a6b059e5e51c6b7f5c16346dc0056b

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/2bb7f8fb92a6b059e5e51c6b7f5c16346dc0056b
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190326/c8dfaa67/attachment-0001.html>


More information about the vc mailing list