From 412b581c51b135722bebdfd458b27f7d268ef85d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kowalczyk?= Date: Fri, 17 Apr 2020 03:15:50 +0200 Subject: [PATCH] [Pal] Add an assert to get_norm_path() This property was already implicitly assumed in a few callsites. We'd be better to document & assert it. --- Pal/lib/graphene/path.c | 10 ++++++---- Pal/regression/normalize_path.c | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Pal/lib/graphene/path.c b/Pal/lib/graphene/path.c index 08bb3220..60ccecd2 100644 --- a/Pal/lib/graphene/path.c +++ b/Pal/lib/graphene/path.c @@ -65,12 +65,13 @@ static inline bool find_prev_slash_offset(const char* path, size_t* offset) { /* * Before calling this function *size_ptr should hold the size of buf. - * After returning it holds number of bytes actually written to it (excluding the ending '\0'). + * After returning it holds number of bytes actually written to it (excluding the ending '\0'). This + * number is never greater than the size of the input path. */ int get_norm_path(const char* path, char* buf, size_t* size_ptr) { - if (!path || !buf || !size_ptr) { - return -PAL_ERROR_INVAL; - } + assert(path && buf && size_ptr); + size_t path_size = strlen(path) + 1; + __UNUSED(path_size); // used only for an assert at the end size_t size = *size_ptr; if (!size) { @@ -145,6 +146,7 @@ int get_norm_path(const char* path, char* buf, size_t* size_ptr) { buf[offset] = '\0'; *size_ptr = ret_size + offset; + assert(*size_ptr <= path_size); return 0; } diff --git a/Pal/regression/normalize_path.c b/Pal/regression/normalize_path.c index de8f9767..cc773021 100644 --- a/Pal/regression/normalize_path.c +++ b/Pal/regression/normalize_path.c @@ -3,6 +3,12 @@ #include "pal_defs.h" #include "pal_error.h" +// Required for asserts inside get_norm_path(). +noreturn void __abort(void) { + warn("ABORTED\n"); + DkProcessExit(1); +} + static const char* get_norm_path_cases[][2] = { {"/", "/"}, {"/a/b/c", "/a/b/c"},