From f17e096eaa6b13c080148a384dc8a5058b5ab5d2 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Sat, 25 Mar 2023 17:33:35 +0300 Subject: [PATCH] Fix path_free() not actually NULL-ing the pointer --- README.md | 8 ++++---- libpath.c | 11 ++++++----- libpath.h | 4 ++-- test.c | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 806eba2..1ed66bc 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,14 @@ int main() { char* parent = path_parent(path); printf("Parent: %s\n", parent); - path_free(path); + path_free(&path); char* parent_of_parent = path_parent(parent); printf("Parent of parent: %s\n", parent_of_parent); - path_free(parent); - path_free(parent_of_parent); - + path_free(&parent); + path_free(&parent_of_parent); + return EXIT_SUCCESS; } ``` diff --git a/libpath.c b/libpath.c index 855a642..a4cb2a9 100644 --- a/libpath.c +++ b/libpath.c @@ -124,9 +124,9 @@ char* path_join(unsigned int count, ...) { } // Frees path and sets pointer to NULL -void path_free(char* path) { - free(path); - path = NULL; +void path_free(char** path) { + free(*path); + *path = NULL; } /* @@ -171,7 +171,8 @@ char* path_parent(const char* path) { base_path[last_separator_index] = '\0'; // free temporary cleaned path - path_free(clean_path); + path_free(&clean_path); return base_path; -} \ No newline at end of file +} + diff --git a/libpath.h b/libpath.h index 02dc943..2bc5adc 100644 --- a/libpath.h +++ b/libpath.h @@ -32,10 +32,10 @@ Be sure to path_free() after use. char* path_join(unsigned int count, ...); // Frees path and sets pointer to NULL -void path_free(char* path); +void path_free(char** path); /* Creates a new path pointing to the parent. Be sure to path_free() on the base as well after use. */ -char* path_parent(const char* path); \ No newline at end of file +char* path_parent(const char* path); diff --git a/test.c b/test.c index 76b55e8..887df01 100644 --- a/test.c +++ b/test.c @@ -22,13 +22,13 @@ int main() { char* parent = path_parent(path); printf("Parent: %s\n", parent); - path_free(path); + path_free(&path); char* parent_of_parent = path_parent(parent); printf("Parent of parent: %s\n", parent_of_parent); - path_free(parent); - path_free(parent_of_parent); + path_free(&parent); + path_free(&parent_of_parent); return EXIT_SUCCESS; } \ No newline at end of file