Skip to content

Fix path normalization to handle '.' and '..' components#46

Merged
JohnAmadis merged 3 commits into
developfrom
copilot/fix-listing-current-directory
Dec 9, 2025
Merged

Fix path normalization to handle '.' and '..' components#46
JohnAmadis merged 3 commits into
developfrom
copilot/fix-listing-current-directory

Conversation

Copilot AI commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

ls without arguments (passing "." to opendir) fails because to_absolute_path() concatenates "." with the current directory, producing unnormalized paths like /mnt/. that filesystem implementations reject.

Changes

  • Added normalize_path(): Resolves "." and ".." components in absolute paths using a stack-based approach

    • Manual string parsing (no strtok) for thread safety
    • Exact buffer sizing to prevent overflows
    • Handles edge cases: multiple slashes, root boundaries, excessive ".."
  • Modified to_absolute_path(): Now normalizes all paths before returning

Example

// Before: 
to_absolute_path(".", "/mnt") → "/mnt/."  // ❌ Fails in opendir

// After:
to_absolute_path(".", "/mnt") → "/mnt"    // ✅ Works

// Also handles:
to_absolute_path("..", "/mnt/sub") → "/mnt"
to_absolute_path("./a/../b", "/mnt") → "/mnt/b"

All existing tests pass. CodeQL clean.

Original prompt

This section details on the original issue you should resolve

<issue_title>Listing current directory does not work properly</issue_title>
<issue_description>Currently listing by ls (without an argument) does not work properly:

stm32f746xg@/> ls
[ERROR] Failed to open directory '.'
[ERROR] Failed to open directory '.'

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 9, 2025 11:47
Add normalize_path function to resolve '.' (current directory) and '..' (parent directory) components in paths. This fixes the issue where 'ls' without arguments (which passes '.' to opendir) fails because the path is not properly normalized.

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
- Replace strtok with manual parsing to avoid string modification and improve thread safety
- Calculate exact buffer size for normalized path to prevent potential buffer overflows
- Use memcpy instead of strcat for safer string concatenation
- Reduce memory allocation for components array

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix current directory listing issue with ls command Fix path normalization to handle '.' and '..' components Dec 9, 2025
Copilot AI requested a review from JohnAmadis December 9, 2025 11:53
@JohnAmadis JohnAmadis marked this pull request as ready for review December 9, 2025 12:38
@JohnAmadis JohnAmadis merged commit dbe8bf3 into develop Dec 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Listing current directory does not work properly

2 participants