From 76023ca45f38ef961be23658055a3a0afa22321b Mon Sep 17 00:00:00 2001 From: daan Date: Mon, 8 Jul 2019 17:43:10 -0700 Subject: [PATCH] fix path max in realpath on unix (pr #69) --- src/alloc.c | 25 ++++++++++++++++++------- test/test-api.c | 9 +++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index f55ac9a6..8ae29723 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -454,19 +454,30 @@ char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) } } #else -#include -#ifndef PATH_MAX -#define PATH_MAX 260 -#endif +#include +static size_t mi_path_max() { + static size_t path_max = 0; + if (path_max <= 0) { + long m = pathconf("/",_PC_PATH_MAX); + if (m <= 0) path_max = 4096; // guess + else if (m < 256) path_max = 256; // at least 256 + else path_max = m; + } + return path_max; +} char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept { if (resolved_name != NULL) { return realpath(fname,resolved_name); } else { - char buf[PATH_MAX+1]; - char* rname = realpath(fname,buf); - return mi_heap_strndup(heap,rname,PATH_MAX); // ok if `rname==NULL` + size_t n = mi_path_max(); + char* buf = (char*)mi_malloc(n+1); + if (buf==NULL) return NULL; + char* rname = realpath(fname,buf); + char* result = mi_heap_strndup(heap,rname,n); // ok if `rname==NULL` + mi_free(buf); + return result; } } #endif diff --git a/test/test-api.c b/test/test-api.c index f7d9d80c..818ba011 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -139,6 +139,15 @@ int main() { CHECK("heap_destroy", test_heap1()); CHECK("heap_delete", test_heap2()); + // --------------------------------------------------- + // various + // --------------------------------------------------- + CHECK_BODY("realpath", { + char* s = mi_realpath( ".", NULL ); + // printf("realpath: %s\n",s); + mi_free(s); + }); + // --------------------------------------------------- // Done // ---------------------------------------------------[]