From 7ea15f3810e0ce51c3acc13553022b1e149e9cca Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Wed, 31 Mar 2021 06:43:27 +0900 Subject: [PATCH] check for wrapping to negative in hash lookup of cell (issue #735) Without this chibi can crash after 129 open file descriptors. Note the bug referenced would also indirectly be fixed if process->string-list properly closed its ports, but we shouldn't rely on that. --- sexp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sexp.c b/sexp.c index 94ffc36e..e5283ab4 100644 --- a/sexp.c +++ b/sexp.c @@ -1809,7 +1809,9 @@ static sexp* sexp_fileno_cell(sexp ctx, sexp vec, int fd) { if (len == 0) return NULL; data = sexp_vector_data(vec); - for (i = 0, cell = (fd * FNV_PRIME) % len; i < len; i++, cell=(cell+1)%len) + cell = (fd * FNV_PRIME) % len; + if (cell < 0) cell += len; + for (i = 0; i < len; i++, cell=(cell+1)%len) if (!sexp_ephemeronp(data[cell]) || (sexp_filenop(sexp_ephemeron_key(data[cell])) && sexp_fileno_fd(sexp_ephemeron_key(data[cell])) == fd))