Skip to content

Commit

Permalink
Fix platform
Browse files Browse the repository at this point in the history
  • Loading branch information
tomocy committed Feb 11, 2024
1 parent 8c6e7c4 commit 342baec
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ const char* get_user_cache_dir(void)

const char* get_dir(const char* path)
{
// According to dirname(3)(https://man7.org/linux/man-pages/man3/dirname.3p.html),
// dirname can modify both the given path and the return value.
// Especially, the return value may be overwritten by subsequent calls, which is error-prune.
// To avois such behaviour, copy values before use.

char* const path2 = copy_str(path);
const char* const dir = dirname(path2);
if ((void*)dir == (void*)path2) {
return path2;
}

// Some platform such as MacOS (https://opensource.apple.com/source/Libc/Libc-391/gen/FreeBSD/dirname.c)
// returns the directory name using the same address instead of the given one.
// This causes the next dirname call to overwrite the previous result, which is error-prune.
// To avoid this, copy the result to the different address and return it.

const char* const dir2 = copy_str(dir);
free((void*)path2);

Expand Down

0 comments on commit 342baec

Please sign in to comment.