Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path resolver and add debugging output #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
bun run codegen
bun run meson-setup.clang-release
meson compile -C build/ vs:executable
bun run test
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -115,6 +116,7 @@ jobs:
# Unclear fix to be investigated
rm subprojects/libtcc/VERSION
meson compile -C build/ vs:executable
bun run test
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
26 changes: 14 additions & 12 deletions src/utils/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ std::pair<bool, std::string> resolve_path::normalizer(const char *parent, const
memset(ret+parent_len,0,child_len+1);
memcpy(ret,parent,parent_len);
int ptr = parent_len;

for(int j=0; j<child_len; ){
if(tkn(child+j,"../")){
//Go back to the past /
if(ptr<=1)return {false, ""}; //Early failure, cannot track back.
if (j > 0) {
if (child[j-1] != '/') {
uint8_t i = 0;
for(;i<=sizeof "../";i++) {
Copy link
Contributor Author

@andy5995 andy5995 Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so this you might laugh at this. ;) Something just didn't feel right about using "3" here.

In my own code, I might #define DOUBLE_DOT_DIR_SEP "../" or something at the top, then just use that string... and then using sizeof would seem more appropriate, but that too seems like overkill.

Copy link
Owner

@KaruroChori KaruroChori Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that is totally fine, hardcoded numbers are just bad. That is resolved at compile time so it is not like it reduces performance or anything.

For context, in modern C++ one would define a constexpr value in place of a define if it had to be made more explicit. But it is fine as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Maybe @Jammyjamjamman is right and I'm not a bad developer! :)

ret[ptr++] = child[j++];
}
continue;
}
}
ptr-=2;
for(;ret[ptr]!='/';ptr--);
j+=3;
Expand All @@ -46,24 +55,17 @@ std::pair<bool, std::string> resolve_path::normalizer(const char *parent, const
j+=2; //Just skip this.
continue;
}
else if(child[j]=='/' && child[j+1]=='/'){
j++;
continue;
}
else if(child[j]=='/'){
else if(child[j]=='/' && j!=child_len-1){
j++;
ret[ptr]='/';
ptr++;
}
else{
ret[ptr]=child[j];
ptr++;
j++;
continue;
}

if(j!=child_len){
ret[ptr]='/';
ptr++;
}
}

//TODO: Stress test for potential unsafety
Expand Down Expand Up @@ -157,4 +159,4 @@ std::pair<resolve_path::reason_t::t,scoped_rpath_t> resolve_path::operator()(fro
return {reason_t::MALFORMED,{rpath_type_t::NONE, ""}};
}

}
}
15 changes: 8 additions & 7 deletions test/marginal/resolver-paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ using namespace vs;

int test_normalizer(const char* expected, const char* parent, const char* child, bool allow_exit, bool base_dir){
auto tmp = resolve_path::normalizer(parent,child,allow_exit,base_dir);
std::cout<<"╭<parent> "<<parent<<"\n";
std::cout<<"├<child> "<<child<<"\n";
std::cout<<"├> "<<tmp.second<<"\n";
std::cout<<"╰>> "<<expected<<"\n";
std::cerr<<"╭<parent> "<<parent<<"\n";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we discussed this change. If I manage to fix the resolver, I'll revert these changes before merging.

std::cerr<<"├<child> "<<child<<"\n";
std::cerr<<"├> "<<tmp.second<<"\n";
std::cerr<<"╰>> "<<expected<<"\n";
//if(!tmp.first)return 1;
if(tmp.second==expected){return 0;}
else{
Expand All @@ -26,10 +26,11 @@ int main(){
ret+=test_normalizer("/quick-js/ww/a", "/hello world/banana/", "./../.././quick-js/ww/a", true, false);
ret+=test_normalizer("", "/hello world/banana/", "../../quick-js/ww/a", false, false);
ret+=test_normalizer("", "/hello world/banana/", "./../.././quick-js/ww/a", false, false);
ret+=test_normalizer("/hello world/banana/ww/a", "/hello world/banana/", "quick-js/../quick-js/ww/a", false, false);
ret+=test_normalizer("/hello world/banana/quick-js/ww/a", "/hello world/banana/", "quick-js/../quick-js/ww/a", false, false);
ret+=test_normalizer("/hello world/banana/ww/a", "/hello world/banana/", "quick-js/./.././ww/a", false, false);
ret+=test_normalizer("/a/b../", "/a/", "b../", false, false);

assert(ret=0);
std::cerr<<"ret = "<<ret<<"\n";
assert(ret==0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes where the test fails even when ret is eq to 0 ;)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL in theory my IDE should have warned me, somehow I missed its message.

return 0;
}
}
Loading