Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin/varnishd/cache/cache_vpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ vpi_ref_panic(struct vsb *vsb, unsigned n, const struct vcl *vcl)
const struct VCL_conf *conf = NULL;
const struct vpi_ref *ref;
const char *p, *src = NULL;
const int lim = 40;
#define LIM 40
const char *abbstr = "[...]";
char buf[lim + sizeof(abbstr)];
char buf[LIM + sizeof(abbstr)];
int w = 0;

AN(vsb);
Expand Down Expand Up @@ -134,9 +134,9 @@ vpi_ref_panic(struct vsb *vsb, unsigned n, const struct vcl *vcl)
w = p - src;
else
w -= ref->offset;
if (w > lim) {
if (w > LIM) {
w = snprintf(buf, sizeof buf, "%.*s%s",
lim, src, abbstr);
LIM, src, abbstr);
src = buf;
}
}
Expand All @@ -153,7 +153,7 @@ vpi_ref_panic(struct vsb *vsb, unsigned n, const struct vcl *vcl)
}
VSB_indent(vsb, -2);
VSB_cat(vsb, "},\n");

#undef LIM
}
void
VPI_Panic(struct vsb *vsb, const struct wrk_vpi *vpi, const struct vcl *vcl)
Expand Down
17 changes: 17 additions & 0 deletions bin/varnishtest/tests/r03000.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
varnishtest "Test VSUB_closefrom() with sysconf overflow (r03000)"

# This test verifies that VSUB_closefrom() correctly handles systems
# where sysconf(_SC_OPEN_MAX) returns values too large for int.
# On macOS without /proc/pid/fd/, sysconf can return LONG_MAX.
# The bug caused integer overflow to -1, triggering assert(maxfd > 0).

shell {
cat > ${tmpdir}/test.vcl <<EOF
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
EOF
varnishd -C -n ${tmpdir} -f ${tmpdir}/test.vcl > /dev/null
}
17 changes: 12 additions & 5 deletions lib/libvarnish/vsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ VSUB_closefrom(int fd)
return;
# endif
char buf[128];
int i, maxfd = 0;
int i;
long maxfd_l = 0;
int maxfd;
DIR *d;
struct dirent *de;
char *p;
Expand All @@ -89,14 +91,19 @@ VSUB_closefrom(int fd)
i = strtoul(de->d_name, &p, 10);
if (*p != '\0')
continue;
if (i > maxfd)
maxfd = i;
if (i > maxfd_l)
maxfd_l = i;
}
AZ(closedir(d));
}

if (maxfd == 0)
maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd_l == 0) {
maxfd_l = sysconf(_SC_OPEN_MAX);
/* sysconf may return unreasonably large values, cap it */
if (maxfd_l > 65536 || maxfd_l <= 0)
maxfd_l = 1024;
}
maxfd = (int)maxfd_l;
assert(maxfd > 0);
for (; maxfd > fd; maxfd--)
(void)close(maxfd);
Expand Down
7 changes: 4 additions & 3 deletions vmod/vmod_directors_shard_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ shardcfg_hashcircle(struct sharddir *shardd)
unsigned h;
uint32_t i, j, n_points, r, rmax;
const char *ident;
const int len = 12; // log10(UINT32_MAX) + 2;
char s[len];
#define IDENT_LEN 12 // log10(UINT32_MAX) + 2;
char s[IDENT_LEN];

CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
AZ(shardd->hashcircle);
Expand Down Expand Up @@ -294,7 +294,7 @@ shardcfg_hashcircle(struct sharddir *shardd)
r = vmin_t(uint32_t, b->replicas, rmax);

for (j = 0; j < r; j++) {
assert(snprintf(s, len, "%d", j) < len);
assert(snprintf(s, IDENT_LEN, "%d", j) < IDENT_LEN);
assert (i < n_points);
shardd->hashcircle[i].point =
VRT_HashStrands32(TOSTRANDS(2, ident, s));
Expand All @@ -314,6 +314,7 @@ shardcfg_hashcircle(struct sharddir *shardd)
"hashcircle[%5jd] = {point = %8x, host = %2u}\n",
(intmax_t)i, shardd->hashcircle[i].point,
shardd->hashcircle[i].host);
#undef IDENT_LEN
}

/*
Expand Down
11 changes: 6 additions & 5 deletions vmod/vmod_vtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
{
struct ws *ws;
unsigned l;
const unsigned maxlen = 1024;
unsigned char buf[maxlen];
#define MAXLEN 1024
unsigned char buf[MAXLEN];
const char *p, *err;

CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
Expand All @@ -263,14 +263,14 @@ vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
return (NULL);
WS_Assert(ws);

if (len > maxlen) {
if (len > MAXLEN) {
VRT_fail(ctx, "workspace_dump: max length is %jd",
(intmax_t)maxlen);
(intmax_t)MAXLEN);
return (NULL);
}

l = WS_Dump(ws, *where, off, buf, len);
assert(l <= maxlen);
assert(l <= MAXLEN);

if (l == 0) {
switch (errno) {
Expand All @@ -289,6 +289,7 @@ vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
return (NULL);
}
return (VRT_blob(ctx, "workspace_dump", p, l, 0xd000d000));
#undef MAXLEN
}

/*--------------------------------------------------------------------*/
Expand Down