Coverage Report

Created: 2025-07-18 06:53

/src/ntpsec/libntp/lib_strbuf.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * lib_strbuf - library string storage
3
 */
4
#include <pthread.h>
5
6
#include "config.h"
7
8
#include "isc_netaddr.h"
9
10
#include "ntp_assert.h"
11
#include "ntp_fp.h"
12
#include "ntp_stdlib.h"
13
#include "lib_strbuf.h"
14
15
/*
16
 * Storage declarations
17
 */
18
static pthread_mutex_t cookie_lock = PTHREAD_MUTEX_INITIALIZER;
19
20
static pthread_t me;
21
22
0
void getbuf_init(void) {
23
0
  me = pthread_self();
24
0
}
25
26
/*
27
 * Function to get a pointer to the next buffer.  Needs to be thread-safe because
28
 * it's used in callers that need to be thread-safe, notably msyslog.  For the
29
 * same reason, we don't try to log a lock-acquisition failure.
30
 *
31
 * ESR: Yes, this is ugly and kludgy. I'm not getting rid of of it
32
 * because I have an eye forward on translation to a garbage-collected
33
 * language, at which point something with this behavior will be
34
 * better than all the contortions we'd have to go through to get rid
35
 * of it in C.
36
 *
37
 * HGM: But I'm not willing to ship a lurking time bomb,
38
 * so I fixed the non-main threads (NTS, DNS) not to call
39
 * lib_getbuf and added a trap to make sure I really fixed them all.
40
 */
41
42
43
char *lib_getbuf(void)
44
0
{
45
0
  static libbufstr  lib_stringbuf[LIB_NUMBUF];
46
0
  static int    lib_nextbuf;
47
0
  char *bufp;
48
49
// FIXME - need this until python tests can call getbuf_init
50
0
  static bool init_done = false;
51
0
  if (!init_done) {
52
0
    getbuf_init();
53
0
    init_done = true;
54
0
  }
55
56
0
  if (pthread_self() != me) {
57
0
    msyslog(LOG_ERR, "ERR: lib_getbuf() called from non-main thread.");
58
0
#ifndef BACKTRACE_DISABLED
59
//        backtrace_log();
60
0
#endif
61
//        exit(1);
62
0
  }
63
64
0
  pthread_mutex_lock(&cookie_lock);
65
0
  ZERO(lib_stringbuf[lib_nextbuf]);
66
0
  (bufp) = &lib_stringbuf[lib_nextbuf++][0];
67
0
  lib_nextbuf %= (int)COUNTOF(lib_stringbuf);
68
0
  pthread_mutex_unlock(&cookie_lock);
69
0
  return bufp;
70
0
}