diff --git a/src/alloc.c b/src/alloc.c index 6e468c85..554405f1 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -781,7 +781,9 @@ mi_decl_nodiscard mi_decl_restrict char* mi_heap_strdup(mi_heap_t* heap, const c if (s == NULL) return NULL; size_t n = strlen(s); char* t = (char*)mi_heap_malloc(heap,n+1); - if (t != NULL) _mi_memcpy(t, s, n + 1); + if (t == NULL) return NULL; + _mi_memcpy(t, s, n); + t[n] = 0; return t; } @@ -832,6 +834,7 @@ mi_decl_nodiscard mi_decl_restrict char* mi_heap_realpath(mi_heap_t* heap, const } #else #include // pathconf +/* static size_t mi_path_max(void) { static size_t path_max = 0; if (path_max <= 0) { @@ -842,20 +845,31 @@ static size_t mi_path_max(void) { } 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 { - size_t n = mi_path_max(); + char* rname = realpath(fname, NULL); + if (rname == NULL) return NULL; + char* result = mi_heap_strdup(heap, rname); + free(rname); // use regular free! (which may be redirected to our free but that's ok) + return result; + } + /* + const size_t n = mi_path_max(); char* buf = (char*)mi_malloc(n+1); - if (buf==NULL) return NULL; + if (buf == NULL) { + errno = ENOMEM; + 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/main-override.cpp b/test/main-override.cpp index 37d4daae..e63d605a 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -39,6 +39,7 @@ static void heap_thread_free_huge(); static void test_stl_allocators(); + int main() { mi_stats_reset(); // ignore earlier allocations