likle / cwalk

Path library for C/C++. Cross-Platform for Linux, FreeBSD, Windows and MacOS. Supports UNIX and Windows path styles on those platforms.
https://likle.github.io/cwalk/
MIT License
253 stars 40 forks source link

[Bug] cwk_path_get_absolute can produce incorrect results when reusing base as buffer #11

Closed nitz closed 4 years ago

nitz commented 4 years ago

Hello! First real quick, I've been using libcwalk for a few days so far and it's done basically everything I need with no fuss. I love it!

Okay, so in an application I'm working on now on an embedded platform, I'm using libcwalk to traverse my folder hierarchy as the user navigates. I have a single buffer that I'm using and have been trying to use cwk_path_get_absolute to build the active path as the user pushes and pops directories. The issue I've run into is that attempting to append a path after a relative path has been appended results in an incorrect result. I've created a small example here to demonstrate the behavior and my expected outcome:

#include <stdio.h>
#include <string.h>

#include "cwalk.h"

#define PATHSIZE 256

void path_push(char* path, const char* add, const char* expected)
{
    cwk_path_get_absolute(path, add, path, PATHSIZE);

    const char* result = strcmp(path, expected) == 0 ? "PASS" : "FAIL";
    printf("%s: '%s', expected: '%s'\r\n", result, path, expected);
}

int main(int argc, char** argv)
{
    char path[PATHSIZE] = { 0 };
    cwk_path_set_style(CWK_STYLE_UNIX);
    path_push(path, "/", "/");
    path_push(path, "see", "/see");
    path_push(path, "dog", "/see/dog");
    path_push(path, "run", "/see/dog/run");
    path_push(path, "..", "/see/dog");
    path_push(path, "..", "/see");
    path_push(path, "cat", "/see/cat");
    path_push(path, "run", "/see/cat/run");
}

Using the relative ".." to navigate up seems to somehow cause the next time a new fragment is added, append to the end of the original, pre-"up'd" directory.

Is this behavior expected when reusing the base path as the result like that?

Thanks for taking a look!

likle commented 4 years ago

Hi @nitz , thanks for submitting this bug! I can reproduce it and will fix it as soon as possible.

likle commented 4 years ago

The issue should be resolved with the latest changes available in the master branch. Please let me know if you have any questions or still encounter any problems!

nitz commented 4 years ago

Woah that was lightning quick! Thank you! I appreciate that you adopted the dog and cat for your test case too 😂