Browse Source

Fix path_free() not actually NULL-ing the pointer

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

8
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;
}
```

11
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;
}
}

4
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);
char* path_parent(const char* path);

6
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;
}
Loading…
Cancel
Save