Browse Source

Fix path_free() not actually NULL-ing the pointer

master
parent
commit
f17e096eaa
  1. 6
      README.md
  2. 9
      libpath.c
  3. 2
      libpath.h
  4. 6
      test.c

6
README.md

@ -24,13 +24,13 @@ int main() {
char* parent = path_parent(path); char* parent = path_parent(path);
printf("Parent: %s\n", parent); printf("Parent: %s\n", parent);
path_free(path); path_free(&path);
char* parent_of_parent = path_parent(parent); char* parent_of_parent = path_parent(parent);
printf("Parent of parent: %s\n", parent_of_parent); printf("Parent of parent: %s\n", parent_of_parent);
path_free(parent); path_free(&parent);
path_free(parent_of_parent); path_free(&parent_of_parent);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

9
libpath.c

@ -124,9 +124,9 @@ char* path_join(unsigned int count, ...) {
} }
// Frees path and sets pointer to NULL // Frees path and sets pointer to NULL
void path_free(char* path) { void path_free(char** path) {
free(path); free(*path);
path = NULL; *path = NULL;
} }
/* /*
@ -171,7 +171,8 @@ char* path_parent(const char* path) {
base_path[last_separator_index] = '\0'; base_path[last_separator_index] = '\0';
// free temporary cleaned path // free temporary cleaned path
path_free(clean_path); path_free(&clean_path);
return base_path; return base_path;
} }

2
libpath.h

@ -32,7 +32,7 @@ Be sure to path_free() after use.
char* path_join(unsigned int count, ...); char* path_join(unsigned int count, ...);
// Frees path and sets pointer to NULL // 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. Creates a new path pointing to the parent.

6
test.c

@ -22,13 +22,13 @@ int main() {
char* parent = path_parent(path); char* parent = path_parent(path);
printf("Parent: %s\n", parent); printf("Parent: %s\n", parent);
path_free(path); path_free(&path);
char* parent_of_parent = path_parent(parent); char* parent_of_parent = path_parent(parent);
printf("Parent of parent: %s\n", parent_of_parent); printf("Parent of parent: %s\n", parent_of_parent);
path_free(parent); path_free(&parent);
path_free(parent_of_parent); path_free(&parent_of_parent);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Loading…
Cancel
Save