change implementation of mi_realpath to be more robust; see issue #660

This commit is contained in:
Daan Leijen 2022-12-19 18:59:33 -08:00
parent 94b8cb870e
commit ef3f651f3f
2 changed files with 19 additions and 4 deletions

View File

@ -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 <unistd.h> // 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

View File

@ -39,6 +39,7 @@ static void heap_thread_free_huge();
static void test_stl_allocators();
int main() {
mi_stats_reset(); // ignore earlier allocations