From 6ab9188d0cec3d887f537894ed669fe803049746 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 04:56:27 +0200 Subject: Implement a tiny mmap wrapper ourselves, replacing libowfat's wrapper --- ot_accesslist.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'ot_accesslist.c') diff --git a/ot_accesslist.c b/ot_accesslist.c index e20f42c..6315bc2 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -10,15 +10,19 @@ #include #include #include -#ifdef WANT_DYNAMIC_ACCESSLIST -#include +#if defined(WANT_ACCESSLIST_BLACK) || defined(WANT_ACCESSLIST_WHITE) +#include +#include +#include #include #include #endif +#ifdef WANT_DYNAMIC_ACCESSLIST +#include +#endif /* Libowfat */ #include "ip6.h" -#include "mmap.h" /* Opentracker */ #include "ot_accesslist.h" @@ -120,12 +124,37 @@ size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, return count; } +/* Map a file read only and report its size. Returns NULL if the file cannot + be opened, is empty, or is too large to map, leaving maplen untouched. */ +static const char *accesslist_map(const char *filename, size_t *maplen) { + struct stat st; + const char *map; + int fd = open(filename, O_RDONLY); + + if (fd < 0) + return NULL; + + if (fstat(fd, &st) < 0 || st.st_size <= 0 || (uintmax_t)st.st_size > (uintmax_t)SIZE_MAX) { + close(fd); + return NULL; + } + + map = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0); + close(fd); + + if (map == MAP_FAILED) + return NULL; + + *maplen = (size_t)st.st_size; + return map; +} + static void accesslist_readfile(void) { ot_accesslist *accesslist_new; const char *map; size_t maplen, capacity, found; - if ((map = mmap_read(g_accesslist_filename, &maplen)) == NULL) { + if ((map = accesslist_map(g_accesslist_filename, &maplen)) == NULL) { char *wd = getcwd(NULL, 0); fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); free(wd); @@ -137,7 +166,7 @@ static void accesslist_readfile(void) { accesslist_new = accesslist_make(g_accesslist, capacity); if (!accesslist_new) { fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash)); - mmap_unmap(map, maplen); + munmap((void *)map, maplen); return; } @@ -146,7 +175,7 @@ static void accesslist_readfile(void) { fprintf(stderr, "Added %zd info_hashes to accesslist\n", found); #endif - mmap_unmap(map, maplen); + munmap((void *)map, maplen); qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); accesslist_new->size = found; -- cgit v1.3