From be6d2f97cda1ac74412dbde1663ccdc4aab77c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Coursi=C3=A8re?= Date: Sat, 17 Aug 2013 17:40:46 +0000 Subject: [PATCH] libroot: Accept NULL for the resolved_name argument of realpath() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as specified in IEEE Std 1003.1, 2013 Edition, see http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html In this case, the returned buffer is allocated with realpath() and can be deallocated by the caller with free(). The behavior was only "implementation defined" in previous revisions like IEEE Std 1003.1, 2004 Edition, see http://pubs.opengroup.org/onlinepubs/000095399/functions/realpath.html Signed-off-by: Jérôme Duval --- src/system/libroot/posix/stdlib/realpath.cpp | 22 +++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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; }