diff --git a/src/system/libroot/posix/stdlib/realpath.cpp b/src/system/libroot/posix/stdlib/realpath.cpp index 575198ec80..9724c39d7e 100644 --- a/src/system/libroot/posix/stdlib/realpath.cpp +++ b/src/system/libroot/posix/stdlib/realpath.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2013, Olivier Coursière, olivier.coursiere@laposte.net. * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -16,16 +17,31 @@ char* realpath(const char* path, char* resolved) { - status_t status = _kern_normalize_path(path, true, resolved); + char* resolvedPath = resolved; + + if (resolvedPath == NULL) { + resolvedPath = (char*)malloc(PATH_MAX + 1); + if (resolvedPath == NULL) { + __set_errno(B_NO_MEMORY); + return NULL; + } + } + + status_t status = _kern_normalize_path(path, true, resolvedPath); if (status != B_OK) { __set_errno(status); + if (resolved == NULL) + free(resolvedPath); return NULL; } // The path must actually exist, not just its parent directories struct stat stat; - if (lstat(resolved, &stat) != 0) + if (lstat(resolvedPath, &stat) != 0) { + if (resolved == NULL) + free(resolvedPath); return NULL; + } - return resolved; + return resolvedPath; }