CCAN: update after 32-bit and MacOS cleanups.
What changed, and why it matters
This commit updates the CCAN utility library used by Core Lightning. Most changes are test fixes and portability cleanups for 32-bit and macOS builds. The one genuinely security-relevant code change is in the file-descriptor passing helper (fdpass_recv): it now uses a larger ancillary buffer and also checks the MSG_CTRUNC flag, so a malicious or misbehaving peer cannot force the process to leak file descriptors by sending more fds than expected. A second change in the I/O engine fixes a case where a write-side EPIPE could leave a connection's read side permanently disabled. The commit message itself does not frame these as security fixes.
Treat as a routine but worthwhile maintenance update. Review the fdpass and io exclusivity changes in the context of how Core Lightning uses fd passing and duplex I/O; consider running the new regression tests on 32-bit and macOS CI targets. No emergency response is indicated from the commit alone.
Security signals we found
fdpass_recv now closes all file descriptors carried by a malformed or truncated SCM_RIGHTS message, fixing a potential fd leak / resource-exhaustion DoS
fdpass_recv checks MSG_CTRUNC in addition to cmsg_len, hardening validation against ABI-specific truncation behavior
io engine clears exclusivity bit on EPIPE-idle path, fixing a state where one plan direction could be permanently suppressed
Multiple regression tests added or updated for fd leak, exclusive duplex behavior, and listener shutdown handling
Evidence from the diff
The diff is a bulk CCAN import (08af1cab -> 17db5e13) with 34 files touched. Security signals: (1) ccan/fdpass/fdpass.c enlarges the SCM_RIGHTS recv buffer from CMSG_SPACE(sizeof(fd)) to CMSG_SPACE(16*sizeof(fd)) and adds a MSG_CTRUNC check before validating cmsg_len. The old code could accept a multi-fd message that happened to truncate to the size of one fd on some ABIs, leaving extra fds installed by the kernel but never closed. The new code closes all fds in the cmsg when the count is wrong or truncation occurred. (2) ccan/io/io.c clears backend exclusivity when a plan idles on EPIPE, preventing a stale exclusive flag from suppressing polling on the opposite direction forever. Remaining changes are portability/test hygiene: unistd.h includes, /dev/fd vs /proc/self/fd, macOS poll() behavior, timeout margins, /tmp symlink handling, errno preservation in path_cwd, etc. No CVE, advisory, or researcher attribution is present in the commit or supplied references.
Changed components
ccan/ccan/fdpass/fdpass.cccan/ccan/fdpass/test/run-multifd-leak.cccan/ccan/io/io.cccan/ccan/io/poll.cccan/ccan/io/test/run-47-exclusive-duplex.cccan/ccan/io/test/run-48-exclusive-duplex-write.cccan/ccan/io/test/run-22-POLLHUP-on-listening-socket.cccan/ccan/tal/path/path.cInspect captured patch +372 / −71
### ccan/README
@@ -1,3 +1,3 @@
CCAN imported from https://github.com/rustyrussell/ccan.
-CCAN version: 08af1cab
+CCAN version: 17db5e13
### ccan/ccan/breakpoint/_info
@@ -27,6 +27,7 @@ int main(int argc, char *argv[])
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/compiler\n");
+ printf("ccan/mem\n");
return 0;
}
### ccan/ccan/breakpoint/test/run-blocked.c
@@ -11,6 +11,7 @@
* Fails against the current code: the second ok1 fails
* (breakpoint_under_debug == true) and the process is then killed by
* SIGTRAP when the mask is restored. Must pass after repair. */
+#include <unistd.h>
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/breakpoint/breakpoint.c>
#include <ccan/tap/tap.h>
### ccan/ccan/breakpoint/test/run-fork.c
@@ -12,6 +12,7 @@
*
* Fails against the current code (child killed by SIGTRAP). Must pass
* after repair. */
+#include <unistd.h>
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/breakpoint/breakpoint.c>
#include <ccan/tap/tap.h>
### ccan/ccan/breakpoint/test/run-threads.c
@@ -13,8 +13,10 @@
* uninitialized state, simulating many first-use races. Fails against
* the current code (a child is killed by SIGTRAP, usually in round 0).
* Must pass after repair. */
+#include <unistd.h>
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/breakpoint/breakpoint.c>
+#include <ccan/mem/mem.h>
#include <ccan/tap/tap.h>
#include <pthread.h>
#include <stdlib.h>
@@ -39,6 +41,14 @@ int main(void)
{
int round, crashed = 0;
+ /* Deliberately hammers a known, documented, unsynchronized
+ * race (audit F2); valgrind's own instrumentation of it isn't
+ * meaningful signal. */
+ if (mem_under_valgrind()) {
+ plan_skip_all("not meaningful under valgrind");
+ return exit_status();
+ }
+
alarm(60);
plan_tests(1);
### ccan/ccan/cppmagic/test/run-map-empty-arg.c
@@ -5,6 +5,7 @@
* cannot distinguish "expands to nothing" from "absent", so this is
* now the documented behavior (cppmagic.h CPPMAGIC_MAP doc note).
* This test pins it. */
+#include <unistd.h>
#include "config.h"
#include <string.h>
### ccan/ccan/fdpass/fdpass.c
@@ -50,9 +50,17 @@ int fdpass_recv(int sockin)
struct iovec iov;
int fd;
char c;
+ /* Only one fd is ever legitimate here, but size the buffer for
+ * several: some kernels don't cleanly truncate/reject a
+ * SCM_RIGHTS message that overflows a too-small ancillary
+ * buffer (observed: a buffer sized for exactly one fd let a
+ * two-fd message through complete and untruncated, silently
+ * leaking the second). Generous headroom means any extra fds a
+ * malformed/malicious message carries stay visible in cmsg data
+ * below, where the check after recvmsg() closes them all. */
union { /* Ancillary data buffer, wrapped in a union
in order to ensure it is suitably aligned */
- char buf[CMSG_SPACE(sizeof(fd))];
+ char buf[CMSG_SPACE(16 * sizeof(fd))];
struct cmsghdr align;
} u;
@@ -79,9 +87,14 @@ int fdpass_recv(int sockin)
return -1;
}
- if (cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
- /* The kernel already installed any fds the message
- * carried; don't leak them. */
+ if ((msg.msg_flags & MSG_CTRUNC) || cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
+ /* Either our ancillary buffer was too small to hold what
+ * the peer sent (MSG_CTRUNC: cmsg_len alone can't be
+ * trusted to catch this, since on some ABIs the truncated
+ * size happens to equal CMSG_LEN(sizeof(fd)) even though
+ * more was sent), or the peer's message didn't carry
+ * exactly one fd. Either way, the kernel already
+ * installed any fds it managed to fit; don't leak them. */
if (cmsg->cmsg_len >= CMSG_LEN(0)
&& cmsg->cmsg_len <= msg.msg_controllen) {
int *fds = (int *)CMSG_DATA(cmsg);
### ccan/ccan/fdpass/test/run-multifd-leak.c
@@ -1,13 +1,11 @@
-/* Temporary auditor regression test (audit 2026-08-05).
- * Proves: when the peer sends TWO fds in ONE SCM_RIGHTS cmsg
- * (cmsg_len = CMSG_LEN(2*sizeof(int)), which fits the receiver's
- * CMSG_SPACE(sizeof(int)) control buffer), fdpass_recv rejects the
- * message but the kernel has already installed both fds into this
- * process; fdpass_recv never closes them -> fd leak (observed: 32 fds
- * leaked over 16 rejected recvs). Repeatable by any non-fdpass peer;
- * resource-exhaustion DoS.
- * Currently FAILS; must pass after repair (reject path must close any
- * fds carried by a malformed SCM_RIGHTS cmsg). */
+/* Regression test: when the peer sends TWO fds in ONE SCM_RIGHTS cmsg
+ * (cmsg_len = CMSG_LEN(2*sizeof(int))), fdpass_recv must reject the
+ * message without leaking whatever fd(s) the kernel already installed
+ * into this process. On some ABIs (notably 32-bit, where
+ * CMSG_SPACE(sizeof(int)) leaves room for only one fd's worth of
+ * ancillary data) the kernel truncates the message down to what looks
+ * like a legitimate single-fd receive, so the check can't rely on
+ * cmsg_len alone and must also honour MSG_CTRUNC. */
#include <ccan/fdpass/fdpass.h>
/* Include the C files directly. */
#include <ccan/fdpass/fdpass.c>
@@ -23,7 +21,7 @@
static int count_fds(void)
{
- DIR *d = opendir("/proc/self/fd");
+ DIR *d = opendir("/dev/fd");
struct dirent *de;
int n = 0;
### ccan/ccan/htable/test/run-init-sized-alloc-fail.c
@@ -7,6 +7,7 @@
* indexed up to (1<<bits)-1 buckets into a one-bucket "table" and wrote
* out of bounds (ASan: stack/heap-buffer-overflow in ht_add()).
*/
+#include <unistd.h>
#include <ccan/htable/htable.h>
#include <ccan/htable/htable.c>
#include <ccan/tap/tap.h>
### ccan/ccan/htable/test/run-init-sized-huge.c
@@ -10,6 +10,7 @@
* checks the requested size against the overflow-free computation, so it
* fails on 32-bit before the fix and passes everywhere after it.
*/
+#include <unistd.h>
#include <ccan/htable/htable.h>
#include <ccan/htable/htable.c>
#include <ccan/tap/tap.h>
### ccan/ccan/io/io.c
@@ -414,6 +414,14 @@ static enum plan_result do_plan(struct io_conn *conn, struct io_plan *plan,
return EXHAUSTED;
if (errno == EPIPE && idle_on_epipe) {
plan->status = IO_UNSET;
+ /* Clear any exclusivity on this (now-idle) plan:
+ * otherwise it stays counted, permanently
+ * suppressing polling on the *other* direction too
+ * (see exclude_pollfds()), even though nothing will
+ * ever clear it since this plan isn't active to be
+ * un-exclusived through the normal io_conn_*_exclusive()
+ * call the caller would otherwise make. */
+ backend_set_exclusive(plan, false);
backend_new_plan(conn);
return UNINTERESTED;
}
### ccan/ccan/io/io.h
@@ -536,7 +536,7 @@ struct io_plan *io_halfclose(struct io_conn *conn);
*
* Example:
* // Silly example to wait then close.
- * static struct io_plan *wait(struct io_conn *conn, void *b)
+ * static struct io_plan *wait_then_close(struct io_conn *conn, void *b)
* {
* return io_wait(conn, b, io_close_cb, NULL);
* }
### ccan/ccan/io/poll.c
@@ -473,7 +473,19 @@ void *io_loop(struct timers *timers, struct timer **expired)
if (events & POLLIN) {
accept_conn(l);
r--;
- } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
+ } else if (events) {
+ /* We only ever ask for POLLIN here.
+ * POLLHUP/POLLNVAL/POLLERR cover
+ * Linux's hangup-on-shutdown case
+ * (see run-22-POLLHUP-on-listening-
+ * socket.c), but as with the
+ * non-listener case below, macOS's
+ * exact revents combination for
+ * socket errors/shutdown is known to
+ * differ from Linux's; don't risk
+ * falling through both branches (and
+ * thus never closing the listener) on
+ * a combination we didn't predict. */
r--;
errno = EBADF;
io_close_listener(l);
@@ -489,12 +501,43 @@ void *io_loop(struct timers *timers, struct timer **expired)
* ECONNREFUSED. */
r--;
/* Get fd's specific error to find Mac's
- * ECONNREFUSED, among others */
- if(getsockopt(fds[i]->fd, SOL_SOCKET, SO_ERROR,
+ * ECONNREFUSED, among others. getsockopt()
+ * doubles as its own error-value buffer here:
+ * on success &errno receives the socket's
+ * SO_ERROR; on failure, errno gets set as
+ * usual by the failed call itself. */
+ if (getsockopt(fds[i]->fd, SOL_SOCKET, SO_ERROR,
&errno, &errno_len) == -1) {
+ if (errno == ENOTSOCK) {
+ /* Not a socket at all (e.g. a
+ * plain fd or /dev/null added
+ * via io_new_conn()): there's
+ * no SO_ERROR to learn here.
+ * Give the connection's own
+ * read/write a normal chance
+ * to run and discover the real
+ * outcome itself (EOF, a real
+ * error, or nothing), instead
+ * of inventing a misleading
+ * EBADF and closing blind.
+ *
+ * events here is only the
+ * error bits we matched this
+ * branch on (by construction,
+ * neither POLLIN nor POLLOUT);
+ * io_ready() only acts on
+ * those two bits, so pass what
+ * we actually asked poll() for
+ * instead, or it's a same-state
+ * no-op that spins forever. */
+ io_ready(c, pollfds[i].events);
+ goto next_fd;
+ }
errno = EBADF;
}
io_close(c);
+ next_fd:
+ ;
}
}
}
### ccan/ccan/io/test/run-15-timeout.c
@@ -86,9 +86,12 @@ int main(void)
int fd, status;
/* This is how many tests you plan to run */
- plan_tests(21);
+ plan_tests(20);
d->state = 0;
- d->timeout_usec = 100000;
+ /* Wide margin: on a loaded/throttled CI runner, a tight gap here
+ * risks the parent's own scheduling delay eating into it before
+ * the child even gets to check the connection. */
+ d->timeout_usec = 300000;
timers_init(&d->timers, time_mono());
timer_init(&d->timer);
fd = make_listen_fd(PORT, &addrinfo);
@@ -108,7 +111,7 @@ int main(void)
if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
exit(2);
signal(SIGPIPE, SIG_IGN);
- usleep(500000);
+ usleep(2000000);
for (i = 0; i < strlen("hellothere"); i++) {
if (write(fd, "hellothere" + i, 1) != 1)
break;
@@ -136,11 +139,21 @@ int main(void)
/* It should have died. */
ok1(wait(&status));
ok1(WIFEXITED(status));
- ok1(WEXITSTATUS(status) < sizeof(d->buf));
-
- /* This one shouldn't time out. */
+ /* Not asserted: how many bytes the child got out before its write
+ * failed depends on TCP half-close timing, not just wall-clock
+ * margins -- our close() above only stops us from reading, it
+ * doesn't forcibly reject writes already in flight from the
+ * child's side, so some/all of them can legitimately still
+ * succeed depending on how fast the kernel gets around to it.
+ * The timeout firing correctly (already checked above: state==1,
+ * expired==&d->timer) is the actual thing under test. */
+ diag("child wrote %d bytes before its write failed (or didn't)",
+ WEXITSTATUS(status));
+
+ /* This one shouldn't time out. Same wide-margin reasoning as
+ * above, mirrored. */
d->state = 0;
- d->timeout_usec = 500000;
+ d->timeout_usec = 2000000;
fflush(stdout);
if (!fork()) {
@@ -154,7 +167,7 @@ int main(void)
if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
exit(2);
signal(SIGPIPE, SIG_IGN);
- usleep(100000);
+ usleep(300000);
for (i = 0; i < strlen("hellothere"); i++) {
if (write(fd, "hellothere" + i, 1) != 1)
break;
### ccan/ccan/io/test/run-22-POLLHUP-on-listening-socket.c
@@ -3,6 +3,8 @@
#include <ccan/io/poll.c>
#include <ccan/io/io.c>
#include <ccan/tap/tap.h>
+#include <ccan/time/time.h>
+#include <ccan/timer/timer.h>
#include <sys/wait.h>
#include <stdio.h>
@@ -43,18 +45,37 @@ static int make_listen_fd(const char *port, struct addrinfo **info)
int main(void)
{
struct addrinfo *addrinfo = NULL;
+ struct io_listener *l;
+ struct timers timers;
+ struct timer timer;
+ struct timer *expired;
int fd;
/* This is how many tests you plan to run */
plan_tests(1);
fd = make_listen_fd(PORT, &addrinfo);
freeaddrinfo(addrinfo);
- io_new_listener(NULL, fd, io_never, NULL);
+ l = io_new_listener(NULL, fd, io_never, NULL);
- /* Anyone could do this; a child doing it will cause poll to return
- * POLLHUP only! */
+ /* Anyone could do this; on Linux, a child doing it causes poll()
+ * to return POLLHUP -- but that's not universal: on macOS, poll()
+ * doesn't report *any* event for an unconnected listening socket
+ * that's been shut down, so io_loop() would otherwise never
+ * return. Bound the wait so this can't hang either way, and
+ * treat the platforms where the event never arrives as a known,
+ * documented gap rather than a failure. */
shutdown(fd, SHUT_RDWR);
- ok1(io_loop(NULL, NULL) == NULL);
+ timers_init(&timers, time_mono());
+ timer_init(&timer);
+ timer_addrel(&timers, &timer, time_from_sec(2));
+ if (io_loop(&timers, &expired) == NULL && !expired) {
+ ok1(true);
+ } else {
+ skip(1, "poll() did not report the shutdown listening"
+ " socket on this platform");
+ io_close_listener(l);
+ }
+ timers_cleanup(&timers);
/* This exits depending on whether all tests passed */
return exit_status();
### ccan/ccan/io/test/run-47-exclusive-duplex.c
@@ -46,6 +46,37 @@ static struct io_plan *write_more(struct io_conn *conn, struct data *d)
write_done, d);
}
+/* Read-exclusive suppresses writes for the connection's whole life here
+ * (unlike write-exclusive's EPIPE-idle case, read's EOF closes the
+ * conn outright rather than idling, so there's no point where write
+ * could resume): the pattern should be nothing but "<...>"-marked
+ * reads, whose concatenated content -- across however many chunks the
+ * peer's two writes happened to arrive in, which is TCP/kernel
+ * delivery detail, not something to assert an exact split for --
+ * equals what was sent. */
+static bool exclusive_read_pattern_ok(const char *pattern, const char *expect)
+{
+ const char *p;
+ char *data;
+ size_t len = 0;
+ bool ok;
+
+ for (p = pattern; *p; p++) {
+ if (*p == '>')
+ return false; /* a write happened: exclusivity lied */
+ }
+
+ data = malloc(strlen(pattern) + 1);
+ for (p = pattern; *p; p++) {
+ if (*p != '<')
+ data[len++] = *p;
+ }
+ data[len] = '\0';
+ ok = (strcmp(data, expect) == 0);
+ free(data);
+ return ok;
+}
+
static struct io_plan *read_priority_init(struct io_conn *conn, struct data *d)
{
/* This should suppress the write */
@@ -131,8 +162,9 @@ int main(void)
d.pattern = tal_arrz(NULL, char, 1);
ok1(io_loop(NULL, NULL) == NULL);
- /* No trace of writes */
- ok1(strcmp(d.pattern, "<1hellothere<1helloagain") == 0);
+ /* No trace of writes; all of the peer's data read, however it
+ * got chunked. */
+ ok1(exclusive_read_pattern_ok(d.pattern, "1hellothere1helloagain"));
tal_free(d.pattern);
ok1(wait(&status));
### ccan/ccan/io/test/run-48-exclusive-duplex-write.c
@@ -4,6 +4,7 @@
#include <ccan/io/io.c>
#include <ccan/tap/tap.h>
#include <sys/wait.h>
+#include <sys/socket.h>
#include <stdio.h>
#define PORT "65048"
@@ -46,6 +47,46 @@ static struct io_plan *write_more(struct io_conn *conn, struct data *d)
write_done, d);
}
+/* Once the write side idles, the read side correctly resumes: some
+ * prefix of pure writes (">"), then whatever the peer wrote arrives in
+ * "<...>"-marked chunks. Two things vary across platforms, so neither
+ * can be asserted as one fixed outcome:
+ * - Exact chunk boundaries depend on TCP/kernel delivery granularity.
+ * - Whether the write side idles at all before the whole connection
+ * closes depends on how the kernel reports the peer's close: EPIPE
+ * (graceful; write idles, buffered peer data is then read) vs.
+ * ECONNRESET (hard reset; io_close()s the whole conn directly, any
+ * unread peer data is legitimately lost, same as Linux's TCP
+ * generally does for this in-flight-data-on-close shape).
+ * So: writes must never follow a read (exclusivity actually held), and
+ * whatever *did* get read, concatenated across however many chunks,
+ * must be a prefix of what was sent -- empty (nothing read: hard
+ * reset) and the full string (graceful: all of it read) both count. */
+static bool exclusive_write_pattern_ok(const char *pattern, const char *expect)
+{
+ const char *p;
+ char *data;
+ size_t len = 0;
+ bool ok, seen_read = false;
+
+ for (p = pattern; *p; p++) {
+ if (*p == '<')
+ seen_read = true;
+ else if (*p == '>' && seen_read)
+ return false; /* write after a read: exclusivity lied */
+ }
+
+ data = malloc(strlen(pattern) + 1);
+ for (p = pattern; *p; p++) {
+ if (*p != '<' && *p != '>')
+ data[len++] = *p;
+ }
+ data[len] = '\0';
+ ok = (strncmp(data, expect, len) == 0);
+ free(data);
+ return ok;
+}
+
static struct io_plan *write_priority_init(struct io_conn *conn, struct data *d)
{
/* This should suppress the read */
@@ -55,9 +96,22 @@ static struct io_plan *write_priority_init(struct io_conn *conn, struct data *d)
static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
{
+ int sndbuf = 1024;
+
/* Free listener so when conns close we exit io_loop */
io_close_listener(d->l);
+ /* We write 1 byte at a time in an exclusive (uninterruptible) loop
+ * until the peer's close is noticed -- the peer here never reads,
+ * so that only happens once the send buffer fills and write()
+ * returns EAGAIN/EPIPE. Default buffer sizes vary a lot across
+ * platforms/kernel versions (observed to be large enough on some
+ * macOS configurations that this loop takes an unpredictably long
+ * time -- not blocked, just very slow -- to naturally get there).
+ * Force it small so this is fast and deterministic everywhere. */
+ setsockopt(io_conn_fd(conn), SOL_SOCKET, SO_SNDBUF,
+ &sndbuf, sizeof(sndbuf));
+
return io_duplex(conn, read_more(conn, d), write_priority_init(conn, d));
}
@@ -131,8 +185,9 @@ int main(void)
d.pattern = tal_arrz(NULL, char, 1);
ok1(io_loop(NULL, NULL) == NULL);
- /* No trace of reads */
- ok1(strspn(d.pattern, ">") == strlen(d.pattern));
+ /* Writes only until the write side idles; all of the peer's data
+ * read after that, however it got chunked. */
+ ok1(exclusive_write_pattern_ok(d.pattern, "1hellothere1helloagain"));
tal_free(d.pattern);
ok1(wait(&status));
### ccan/ccan/io/test/run-50-listener-blocking-accept.c
@@ -90,7 +90,11 @@ static int make_listen_fd(const char *port)
int main(void)
{
- struct sockaddr_in sa;
+ /* make_listen_fd() uses AF_UNSPEC, and getaddrinfo() commonly
+ * resolves that to IPv6 on macOS (vs. IPv4 here) -- a fixed
+ * sockaddr_in and a hardcoded AF_INET client socket below would
+ * mismatch whichever family the listener actually got. */
+ struct sockaddr_storage sa;
socklen_t salen = sizeof(sa);
int flags, status;
@@ -110,9 +114,11 @@ int main(void)
fflush(stdout);
if (fork() == 0) {
alarm(5);
- client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (getsockname(listen_fd, (struct sockaddr *)&sa, &salen) != 0)
exit(1);
+ client_fd = socket(sa.ss_family, SOCK_STREAM, 0);
+ if (client_fd < 0)
+ exit(1);
if (connect(client_fd, (struct sockaddr *)&sa, salen) != 0)
exit(1);
io_poll_override(rst_poll);
### ccan/ccan/likely/test/run-stats-empty-debug.c
@@ -1,4 +1,5 @@
#define CCAN_LIKELY_DEBUG 1
+#include <unistd.h>
#include <ccan/likely/likely.c>
#include <ccan/likely/likely.h>
#include <ccan/tap/tap.h>
### ccan/ccan/mem/mem.c
@@ -3,9 +3,22 @@
#include "config.h"
#include <assert.h>
+#include <stdlib.h>
#include <string.h>
#include <ccan/mem/mem.h>
+bool mem_under_valgrind(void)
+{
+ const char *e = getenv("RUNNING_ON_VALGRIND");
+ if (e && strcmp(e, "1") == 0)
+ return true;
+#if HAVE_VALGRIND_MEMCHECK_H
+ return RUNNING_ON_VALGRIND;
+#else
+ return false;
+#endif
+}
+
#if !HAVE_MEMMEM
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen)
### ccan/ccan/mem/mem.h
@@ -258,6 +258,15 @@ static inline void *memcheck_(const void *data, size_t len)
}
#endif
+/**
+ * mem_under_valgrind - true if we're running under valgrind.
+ *
+ * Checks RUNNING_ON_VALGRIND() if available at compile time, and
+ * RUNNING_ON_VALGRIND=1 in the environment (set by ccanlint) either
+ * way.
+ */
+bool mem_under_valgrind(void);
+
#if HAVE_TYPEOF
/**
* memcheck - check that a memory region is initialized
### ccan/ccan/order/test/api.c
@@ -121,11 +121,23 @@ int main(void)
TEST_SCALAR(unsigned, uint, 0, 1, 10, INT_MAX, (unsigned)INT_MAX+1,
-10, -1);
+ /* These sequences must be strictly increasing. On platforms
+ * where long is the same width as int (e.g. 32-bit), INT_MIN ==
+ * LONG_MIN and INT_MAX == LONG_MAX, so including both would
+ * produce equal-valued (non-strictly-increasing) entries. */
+#if LONG_MAX > INT_MAX
TEST_SCALAR(long, long, LONG_MIN, INT_MIN, -10, -1, 0, 1, 10, INT_MAX,
LONG_MAX);
+#else
+ TEST_SCALAR(long, long, LONG_MIN, -10, -1, 0, 1, 10, LONG_MAX);
+#endif
+#if ULONG_MAX > UINT_MAX
TEST_SCALAR(unsigned long, ulong, 0, 1, 10, INT_MAX,
(unsigned long)INT_MAX+1, LONG_MAX,
(unsigned long)LONG_MAX+1, -10, -1);
+#else
+ TEST_SCALAR(unsigned long, ulong, 0, 1, 10, LONG_MAX, -10, -1);
+#endif
TEST_SCALAR(float, float, -INFINITY, -FLT_MAX, -1.0, 0.0, FLT_MIN,
0.1, M_E, M_PI, 5.79, FLT_MAX, INFINITY);
### ccan/ccan/pipecmd/test/run-eintr.c
@@ -2,6 +2,7 @@
* SA_RESTART interrupts the parent's blocking read(execfail[0]);
* pipecmdarr must not then report success for a command which does
* not exist. */
+#include <unistd.h>
#include <ccan/pipecmd/pipecmd.h>
/* Include the C files directly. */
#include <ccan/pipecmd/pipecmd.c>
### ccan/ccan/pipecmd/test/run-execfail-fdleak.c
@@ -3,19 +3,21 @@
* (b) pipe()/fcntl()/fork() fails after the command pipes were created.
* Both paths only close par_close[] and forget tochild[1], fromchild[0]
* and errfromchild[0]. */
+#include <unistd.h>
#include <ccan/pipecmd/pipecmd.h>
/* Include the C files directly. */
#include <ccan/pipecmd/pipecmd.c>
#include <ccan/tap/tap.h>
#include <string.h>
#include <dirent.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
static int count_fds(void)
{
- DIR *d = opendir("/proc/self/fd");
+ DIR *d = opendir("/dev/fd");
struct dirent *de;
int n = 0;
@@ -70,8 +72,23 @@ int main(void)
getrlimit(RLIMIT_NOFILE, &saved);
before = count_fds();
rl = saved;
- /* Room for exactly the three command pipes (6 fds). */
- rl.rlim_cur = before + 6;
+ {
+ /* A count of open fds isn't a safe basis for the limit:
+ * if our fd table has gaps (e.g. the test harness holds
+ * some higher-numbered fds open), pipecmdarr()'s opens
+ * fill those gaps first and the count undercounts how
+ * many low-numbered slots are actually free. Probe the
+ * fd number the kernel would hand out next instead, so
+ * the limit leaves room for exactly the three command
+ * pipes (6 fds). */
+ int next = open("/dev/null", O_RDONLY);
+ if (next < 0) {
+ ok(0, "probe open failed");
+ exit(1);
+ }
+ rl.rlim_cur = next + 6;
+ close(next);
+ }
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
ok(0, "setrlimit failed");
exit(1);
### ccan/ccan/pipecmd/test/run-execfail-flush.c
@@ -1,6 +1,7 @@
/* Regression test: on exec failure the child calls exit(127) instead of
* _exit(127), so it flushes the parent's inherited stdio buffers a second
* time (and runs atexit handlers) when an output stream is preserved. */
+#include <unistd.h>
#include <ccan/pipecmd/pipecmd.h>
/* Include the C files directly. */
#include <ccan/pipecmd/pipecmd.c>
### ccan/ccan/ptr_valid/test/run-batch-cache.c
@@ -3,6 +3,7 @@
* ptr_valid_batch_write() claim the page is writable (and a cached
* write failure makes ptr_valid_batch_read() claim it is unreadable).
* Currently fails: not ok 2 and not ok 4. */
+#include <unistd.h>
#include <ccan/ptr_valid/ptr_valid.h>
/* Include the C files directly. */
#include <ccan/ptr_valid/ptr_valid.c>
### ccan/ccan/ptr_valid/test/run-child-flush.c
@@ -4,6 +4,7 @@
* a second time. We force the fallback by undefining
* HAVE_PROC_SELF_MAPS (same code path as /proc being unavailable).
* Currently fails: not ok 2. */
+#include <unistd.h>
#include <ccan/ptr_valid/ptr_valid.h>
#undef HAVE_PROC_SELF_MAPS
#define HAVE_PROC_SELF_MAPS 0
### ccan/ccan/ptr_valid/test/run-errno.c
@@ -1,6 +1,7 @@
/* Regression test: ptr_valid.h documents "Sets errno to EFAULT on
* failure", but the /proc/self/maps path and the alignment check
* return false without setting errno. Currently fails all 3 tests. */
+#include <unistd.h>
#include <ccan/ptr_valid/ptr_valid.h>
/* Include the C files directly. */
#include <ccan/ptr_valid/ptr_valid.c>
### ccan/ccan/ptr_valid/test/run-realloc-leak.c
@@ -3,6 +3,7 @@
* We interpose malloc/realloc/free by macro (this file includes
* ptr_valid.c directly) and inject a failure at each growth call.
* Currently fails: not ok 1 and not ok 2. */
+#include <unistd.h>
#include <ccan/ptr_valid/ptr_valid.h>
#include <stdint.h>
@@ -83,7 +84,6 @@ int main(void)
int i;
struct ptr_valid_batch batch;
- plan_tests(2);
alarm(20);
/* Silence "defined but not used" when ptr_valid.c's
@@ -94,14 +94,23 @@ int main(void)
/* Split a 1000-page mapping into alternating RO/RW VMAs so
* /proc/self/maps exceeds grab()'s initial 16k buffer and has
- * more entries than add_map()'s initial 16 slots. */
+ * more entries than add_map()'s initial 16 slots.
+ *
+ * Must resolve this (and plan accordingly) before the single
+ * plan_tests()/plan_skip_all() call tap allows. */
region = mmap(NULL, 1000 * 4096, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
- if (region == MAP_FAILED)
+ if (region == MAP_FAILED) {
plan_skip_all("mmap failed");
+ return exit_status();
+ }
for (i = 0; i < 1000; i += 2)
- if (mprotect(region + i * 4096, 4096, PROT_READ) != 0)
+ if (mprotect(region + i * 4096, 4096, PROT_READ) != 0) {
plan_skip_all("mprotect failed");
+ return exit_status();
+ }
+
+ plan_tests(2);
/* grab(): fail the 16384 -> 32768 buffer growth. */
fail_realloc_size = 32768 + 1;
### ccan/ccan/tal/path/path.c
@@ -15,13 +15,20 @@ char *path_cwd(const tal_t *ctx)
{
size_t len = 64;
char *cwd;
+ int saved_errno = errno;
/* *This* is why people hate C. */
cwd = tal_arr(ctx, char, len);
while (cwd && !getcwd(cwd, len)) {
if (errno != ERANGE || !tal_resize(&cwd, len *= 2))
cwd = tal_free(cwd);
}
+ /* No guarantee callers get errno untouched on success, but no
+ * reason to leave our own retry's stale ERANGE sitting there
+ * either (cwd longer than our initial 64-byte guess is routine
+ * under macOS's deep default $TMPDIR). */
+ if (cwd)
+ errno = saved_errno;
return cwd;
}
@@ -113,11 +120,15 @@ struct path_pushd *path_pushd(const tal_t *ctx, const char *dir)
if (!old)
return NULL;
+ /* Check this before path_cwd(): we shouldn't be calling getcwd()
+ * (and thus touching errno) at all when we're not going to do
+ * anything. */
+ if (unlikely(!dir) && taken(dir))
+ return tal_free(old);
+
old->olddir = path_cwd(old);
if (unlikely(!old->olddir))
old = tal_free(old);
- else if (unlikely(!dir) && is_taken(dir))
- old = tal_free(old);
else if (chdir(dir) != 0)
old = tal_free(old);
### ccan/ccan/tal/path/test/run-pushd.c
@@ -6,6 +6,10 @@ int main(void)
{
struct path_pushd *pd;
char path1[1024], path2[1024], *ctx = tal_strdup(NULL, "ctx");
+ /* /tmp can be a symlink (e.g. to /private/tmp on macOS): resolve it
+ * so our getcwd() comparisons match. NULL context: mustn't be a
+ * child of ctx, since we check ctx has no leftover children below. */
+ const char *realtmp = path_canon(NULL, "/tmp");
/* This is how many tests you plan to run */
plan_tests(19);
@@ -37,7 +41,7 @@ int main(void)
if (!getcwd(path2, sizeof(path2)))
abort();
- ok1(streq(path2, "/tmp"));
+ ok1(streq(path2, realtmp));
path_popd(pd);
if (!getcwd(path2, sizeof(path2)))
@@ -71,5 +75,6 @@ int main(void)
#endif
ok1(!tal_first(ctx));
tal_free(ctx);
+ tal_free(realtmp);
return exit_status();
}
### ccan/ccan/tal/path/test/run-simplify.c
@@ -4,7 +4,8 @@
int main(void)
{
- char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx");
+ char cwd[1024], *path, *parent, *tmpbase, *ctx = tal_strdup(NULL, "ctx");
+ const char *realtmp;
plan_tests(95);
@@ -18,6 +19,14 @@ int main(void)
if (symlink("run-simplify-foo", "run-simplify-link") != 0)
abort();
+ /* /tmp can itself be a symlink (e.g. to /private/tmp on macOS),
+ * which would make path_simplify() keep ".." literally instead of
+ * collapsing it. Resolve it, so the tests below always exercise a
+ * real directory. */
+ realtmp = path_canon(ctx, "/tmp");
+ parent = path_dirname(ctx, realtmp);
+ tmpbase = path_basename(ctx, realtmp);
+
/* Handling of . and .. */
path = path_simplify(ctx, ".");
ok1(streq(path, "."));
@@ -177,53 +186,53 @@ int main(void)
tal_free(path);
/* This is expected to be a real directory. */
- path = path_simplify(ctx, "/tmp");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, realtmp);
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/", realtmp)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/.");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/.", realtmp)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/./tmp/.");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "/.%s/.", realtmp)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/../tmp/.");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "/..%s/.", realtmp)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/..");
- ok1(streq(path, "/"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/..", realtmp)));
+ ok1(streq(path, parent));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/../");
- ok1(streq(path, "/"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/../", realtmp)));
+ ok1(streq(path, parent));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/../tmp");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/../%s", realtmp, tmpbase)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/../tmp/");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/../%s/", realtmp, tmpbase)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
- path = path_simplify(ctx, "/tmp/../tmp/.");
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/../%s/.", realtmp, tmpbase)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
@@ -255,10 +264,13 @@ int main(void)
tal_free(path);
/* take tests */
- path = path_simplify(ctx, take(tal_strdup(ctx, "/tmp/../tmp/.")));
- ok1(streq(path, "/tmp"));
+ path = path_simplify(ctx, take(tal_fmt(ctx, "%s/../%s/.", realtmp, tmpbase)));
+ ok1(streq(path, realtmp));
ok1(tal_parent(path) == ctx);
tal_free(path);
+ tal_free(parent);
+ tal_free(tmpbase);
+ tal_free(realtmp);
ok1(tal_first(ctx) == NULL);
tal_free(ctx);
### ccan/ccan/time/test/run-divide-roundup.c
@@ -10,6 +10,7 @@
* After repair it must pass: result well-formed and equal to either
* {0, 999999999} (truncation) or {1, 0} (correct round-up).
*/
+#include <unistd.h>
#include <ccan/time/time.h>
#include <ccan/time/time.c>
#include <ccan/tap/tap.h>
### ccan/ccan/time/test/run-multiply-overflow.c
@@ -9,6 +9,7 @@
* repair (document the overflow precondition and/or clamp), it must
* run UBSan-clean.
*/
+#include <unistd.h>
#include <ccan/time/time.h>
#include <ccan/time/time.c>
#include <ccan/tap/tap.h>Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.