]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Fix memory leak on macOS by auto-enabling NEWLOCALE_NEEDS_FREELOCALE 915/head
authorJaisonZheng <1254186821@qq.com>
Wed, 21 Jan 2026 09:28:20 +0000 (17:28 +0800)
committerJaisonZheng <1254186821@qq.com>
Wed, 21 Jan 2026 09:28:20 +0000 (17:28 +0800)
macOS's newlocale() does not follow POSIX specification - it doesn't
reuse the passed locale_t, causing ~1,472 bytes leak per json_tokener_parse() call.

This is the same issue as #668 (FreeBSD). The existing workaround works,
but macOS was not auto-detected.

Tested with AddressSanitizer: leak drops from 1,472,000 bytes to ~120 bytes
(system init only).

CMakeLists.txt

index 6b8fb49115f8f5b18107b0e850eafbd14052c7d8..58ee9ede6da4361586264755977a543a7c52b500 100644 (file)
@@ -66,9 +66,17 @@ option(OVERRIDE_GET_RANDOM_SEED       "Override json_c_get_random_seed() with cu
 option(DISABLE_EXTRA_LIBS             "Avoid linking against extra libraries, such as libbsd." OFF)
 option(DISABLE_JSON_POINTER           "Disable JSON pointer (RFC6901) and JSON patch support." OFF)
 option(DISABLE_JSON_PATCH             "Disable JSON patch (RFC6902) support."                 OFF)
-option(NEWLOCALE_NEEDS_FREELOCALE     "Work around newlocale bugs in old FreeBSD by calling freelocale"  OFF)
+option(NEWLOCALE_NEEDS_FREELOCALE     "Work around newlocale bugs in old FreeBSD and macOS by calling freelocale"  OFF)
 option(BUILD_APPS                     "Default to building apps" ON)
 
+# macOS (Darwin) has the same newlocale() bug as older FreeBSD versions:
+# newlocale() does not free the locale object passed to it, causing memory leaks.
+# See: https://github.com/json-c/json-c/issues/668
+if(APPLE AND NOT NEWLOCALE_NEEDS_FREELOCALE)
+    message(STATUS "macOS detected: enabling NEWLOCALE_NEEDS_FREELOCALE to prevent memory leaks")
+    set(NEWLOCALE_NEEDS_FREELOCALE ON CACHE BOOL "macOS needs freelocale workaround" FORCE)
+endif()
+
 if (AMIGA)
     set(DISABLE_THREAD_LOCAL_STORAGE ON)
     set(ENABLE_THREADING OFF)