]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
xap_helper.h: fix double-free on OpenBSD hdestroy
authorEric Wong <e@80x24.org>
Wed, 30 Aug 2023 05:10:45 +0000 (05:10 +0000)
committerEric Wong <e@80x24.org>
Wed, 30 Aug 2023 05:27:36 +0000 (05:27 +0000)
hdestroy on OpenBSD assumes each key in the table can be freed,
so use strdup to fulfil that requirement.

This behavior differs from tested behavior on glibc and FreeBSD,
as well as what I can see from reading the musl and NetBSD
source code.  OpenBSD may be the only relevant OS which requires
this workaround.

lib/PublicInbox/xap_helper.h

index 922105110c8e0c166408f8363e55084d43c0fbb5..17085adcae55cc755a6d1b072314999d521e2d06 100644 (file)
@@ -460,6 +460,16 @@ static enum exc_iter dump_roots_iter(struct req *req,
        return ITER_OK;
 }
 
+static char *hsearch_enter_key(char *s)
+{
+#if defined(__OpenBSD__) /* hdestroy frees each key */
+       char *ret = strdup(s);
+       if (!ret) perror("strdup");
+       return ret;
+#endif // glibc, musl, FreeBSD, NetBSD do not free keys
+       return s;
+}
+
 static bool cmd_dump_roots(struct req *req)
 {
        CLEANUP_DUMP_ROOTS struct dump_roots_tmp drt = {};
@@ -511,7 +521,8 @@ static bool cmd_dump_roots(struct req *req)
        }
        for (size_t i = 0; i < tot; ) {
                ENTRY e;
-               e.key = drt.entries[i++];
+               e.key = hsearch_enter_key(drt.entries[i++]);
+               if (!e.key) return false;
                e.data = drt.entries[i++];
                if (!hsearch(e, ENTER)) {
                        warn("hsearch(%s => %s, ENTER)", e.key,