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

ACE_UNIX_Addr supports abstract path in Linux. #870

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

likema
Copy link
Contributor

@likema likema commented Mar 12, 2019

In Linux, UNIX domain socket supports abstract path like @/tmp/foobar

Summary by CodeRabbit

  • New Features

    • Enhanced support for abstract UNIX domain socket addresses on Linux, ensuring accurate address conversion and representation.
    • Improved address comparison handling for cases involving empty or abstract paths.
  • Tests

    • Extended test coverage to validate the processing of abstract socket paths, ensuring robust and reliable address operations.

@likema likema force-pushed the linux-unix-addr-abstract-path branch from 87ccca6 to 294c373 Compare December 11, 2019 02:55
@likema likema force-pushed the linux-unix-addr-abstract-path branch from 294c373 to cf8f8f2 Compare December 29, 2019 18:11
@jwillemsen jwillemsen added the needs review Needs to be reviewed label May 7, 2020
@jwillemsen
Copy link
Member

Any test?

@likema
Copy link
Contributor Author

likema commented Aug 27, 2020

Any test?

It is already in ACE/tests/UNIX_Addr_Test.cpp

@jwillemsen
Copy link
Member

But does it contain a test for an abstract path?

@likema
Copy link
Contributor Author

likema commented Aug 27, 2020

But does it contain a test for an abstract path?

I mean that I committed a test for it:

#if defined(ACE_LINUX)
// Set abstract path by set.
path = "@/tmp/ace.test";
addr.set (path);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (*addr.get_path_name () == '\0');
ACE_TEST_ASSERT (strcmp (path + 1, addr.get_path_name () + 1) == 0);
// Set abstract path by string_to_addr.
path = "@/tmp/unix_addr_test";
addr.string_to_addr (path);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (*addr.get_path_name () == '\0');
ACE_TEST_ASSERT (strcmp (path + 1, addr.get_path_name () + 1) == 0);
#endif // ACE_LINUX

@jwillemsen
Copy link
Member

Ok, missed that addition. Can you merge master into your branch so that all recent CI builds run?

@likema likema force-pushed the linux-unix-addr-abstract-path branch from cf8f8f2 to 9ae9996 Compare August 27, 2020 11:46
@likema
Copy link
Contributor Author

likema commented Aug 27, 2020

Ok, missed that addition. Can you merge master into your branch so that all recent CI builds run?

Done

Copy link

coderabbitai bot commented Feb 1, 2025

Walkthrough

The changes update the handling of UNIX domain socket addresses in ACE for Linux abstract socket paths. New conditional logic verifies whether a path is abstract (prefixed with '@'), adjusts the character and length accordingly, and uses the ACE_MIN macro for safe copying. The operator== is modified for proper comparison, while the tests are enhanced with assertions to validate these behaviors.

Changes

File(s) Change Summary
ACE/.../UNIX_Addr.cpp Added conditional logic for Linux abstract UNIX socket paths in string_to_addr, addr_to_string, and both set methods; replaces leading '@' with a null character; uses ACE_MIN for safe copying.
ACE/.../UNIX_Addr.inl Modified the operator== to adjust comparison for empty sun_path in Linux, skipping the first character when both paths are empty.
ACE/.../UNIX_Addr_Test.cpp Introduced new assertions to test abstract path handling on Linux and improved formatting consistency of existing test cases.

Sequence Diagram(s)

sequenceDiagram
    participant C as Caller
    participant S as string_to_addr
    participant L as Linux Check
    C->>S: Call string_to_addr(input)
    S->>L: Check if ACE_LINUX defined
    alt Linux system
        L->>S: Verify if sun_path[0] == '@'
        alt Abstract path detected
            S->>S: Replace '@' with '\0'
            S->>S: Calculate length (len)
        end
    else
        S->>S: Use standard path processing
    end
    S->>S: Call set_size(len)
    S-->>C: Return address configuration
Loading
sequenceDiagram
    participant C as Caller
    participant E as operator== (ACE_UNIX_Addr)
    participant D as Decision Block
    C->>E: Compare two UNIX_Addr objects
    E->>D: Check if both sun_paths are empty (Linux)
    alt Both empty
        D->>E: Set offset to skip first character
        E->>E: Perform comparison using adjusted index
    else
        D->>E: Use full-length comparison
    end
    E-->>C: Return comparison result
Loading

Poem

I hop through code with nimble feet,
Adjusting abstract paths so neat.
A '@' becomes a silent start,
Logic refined—a work of art!
In tests and files, I celebrate the day,
A happy bunny leads the way! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jwillemsen
Copy link
Member

@coderabbitai review

Copy link

coderabbitai bot commented Feb 2, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (2)
ACE/ace/UNIX_Addr.cpp (2)

38-56: Add input validation in string_to_addr.

The current implementation lacks validation of the input path length, which could lead to buffer overflow.

Add length validation:

 int
 ACE_UNIX_Addr::string_to_addr (const char addr[])
 {
+  if (!addr)
+    return -1;
+
+  size_t const input_len = ACE_OS::strlen(addr);
+  if (input_len >= sizeof(this->unix_addr_.sun_path))
+    return -1;
+
   ACE_OS::strsncpy (this->unix_addr_.sun_path, addr,
                     sizeof this->unix_addr_.sun_path);

   size_t const len = ACE_OS::strlen (this->unix_addr_.sun_path);
 #if defined (ACE_LINUX)
   if (*this->unix_addr_.sun_path == '@') // abstract path
     {
       *this->unix_addr_.sun_path = 0;
     }
 #endif /* ACE_LINUX */

128-147: Improve error handling in set method.

The current implementation doesn't validate input parameters and might access invalid memory.

Add parameter validation:

 int
 ACE_UNIX_Addr::set (const sockaddr_un *un, int len)
 {
+  if (!un || len <= 0)
+    return -1;
+
   (void) ACE_OS::memset ((void *) &this->unix_addr_, 0,
                    sizeof this->unix_addr_);
   this->unix_addr_.sun_family = AF_UNIX;
 #if defined (ACE_LINUX)
   int const n = ACE_MIN (len - int (sizeof this->unix_addr_ -
                                     sizeof (this->unix_addr_.sun_path)),
                          int (sizeof (this->unix_addr_.sun_path)));
   if (n > 0)
     {
       memcpy (this->unix_addr_.sun_path, un->sun_path, n);
     }
 #else
   ACE_OS::strcpy (this->unix_addr_.sun_path, un->sun_path);
 #endif /* ACE_LINUX */
   this->base_set (AF_UNIX, len);
   return 0;
 }
🧹 Nitpick comments (1)
ACE/tests/UNIX_Addr_Test.cpp (1)

58-74: Add more test cases for abstract paths.

While the current tests cover basic functionality, consider adding these test cases:

  1. Negative tests with invalid abstract paths
  2. Edge cases with maximum path length
  3. Tests for comparison operator with abstract paths

Add these test cases:

 #if defined(ACE_LINUX)
+  // Test maximum length abstract path
+  char max_path[108] = "@";  // sizeof(sun_path) is typically 108
+  ACE_OS::memset(max_path + 1, 'a', sizeof(max_path) - 2);
+  max_path[sizeof(max_path) - 1] = '\0';
+  addr.set(max_path);
+  ACE_TEST_ASSERT(addr.addr_to_string(buf, sizeof(buf)) == 0);
+  ACE_TEST_ASSERT(strcmp(max_path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
+
+  // Test comparison of abstract paths
+  ACE_UNIX_Addr addr2("@/tmp/ace.test");
+  ACE_TEST_ASSERT(addr2 == addr2);
+  ACE_TEST_ASSERT(!(addr2 != addr2));
+
+  // Test invalid abstract paths
+  ACE_TEST_ASSERT(addr.set("@") == 0);  // Empty abstract path
+  ACE_TEST_ASSERT(addr.set("@@/tmp/test") == 0);  // Multiple @ prefixes
 #endif // ACE_LINUX
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3ca3f and 100faa3.

📒 Files selected for processing (3)
  • ACE/ace/UNIX_Addr.cpp (5 hunks)
  • ACE/ace/UNIX_Addr.inl (1 hunks)
  • ACE/tests/UNIX_Addr_Test.cpp (1 hunks)
🔇 Additional comments (1)
ACE/ace/UNIX_Addr.inl (1)

27-40: ⚠️ Potential issue

Verify comparison length and add bounds checking.

The current implementation has potential issues:

  1. The length calculation sizeof (this->unix_addr_.sun_path) - 1 might be incorrect for abstract paths where the full length should be used.
  2. No validation of array bounds before accessing sun_path[0].

Consider this safer implementation:

 ACE_INLINE bool
 ACE_UNIX_Addr::operator == (const ACE_UNIX_Addr &sap) const
 {
   size_t i = 0;
+  size_t len = sizeof(this->unix_addr_.sun_path);
 #if defined (ACE_LINUX)
-  if (!*this->unix_addr_.sun_path && !*sap.unix_addr_.sun_path)
+  if (len > 0 && !this->unix_addr_.sun_path[0] && !sap.unix_addr_.sun_path[0])
   {
     i = 1;
+    // For abstract paths, compare the full length
+    len = sizeof(this->unix_addr_.sun_path);
   }
 #endif /* ACE_LINUX */
   return ACE_OS::strncmp (this->unix_addr_.sun_path + i,
                         sap.unix_addr_.sun_path + i,
-                         sizeof (this->unix_addr_.sun_path) - 1) == 0;
+                         len - i) == 0;
 }

Comment on lines 60 to 75
int
ACE_UNIX_Addr::addr_to_string (ACE_TCHAR s[], size_t len) const
{
ACE_OS::strsncpy (s,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path),
len);
size_t i = 0;
#if defined (ACE_LINUX)
if (len && !*this->unix_addr_.sun_path && this->unix_addr_.sun_path[1])
{
s[0] = '@';
i = 1;
}
#endif /* ACE_LINUX */
ACE_OS::strsncpy (s + i,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path + i),
len - i);
return 0;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add bounds checking in addr_to_string.

The current implementation lacks validation of the output buffer size and index bounds.

Add bounds checking:

 int
 ACE_UNIX_Addr::addr_to_string (ACE_TCHAR s[], size_t len) const
 {
+  if (!s || !len)
+    return -1;
+
   size_t i = 0;
 #if defined (ACE_LINUX)
   if (len && !*this->unix_addr_.sun_path && this->unix_addr_.sun_path[1])
     {
       s[0] = '@';
       i = 1;
+      if (i >= len)
+        return -1;
     }
 #endif /* ACE_LINUX */
   ACE_OS::strsncpy (s + i,
                     ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path + i),
                     len - i);
   return 0;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
int
ACE_UNIX_Addr::addr_to_string (ACE_TCHAR s[], size_t len) const
{
ACE_OS::strsncpy (s,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path),
len);
size_t i = 0;
#if defined (ACE_LINUX)
if (len && !*this->unix_addr_.sun_path && this->unix_addr_.sun_path[1])
{
s[0] = '@';
i = 1;
}
#endif /* ACE_LINUX */
ACE_OS::strsncpy (s + i,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path + i),
len - i);
return 0;
}
int
ACE_UNIX_Addr::addr_to_string (ACE_TCHAR s[], size_t len) const
{
if (!s || !len)
return -1;
size_t i = 0;
#if defined (ACE_LINUX)
if (len && !*this->unix_addr_.sun_path && this->unix_addr_.sun_path[1])
{
s[0] = '@';
i = 1;
if (i >= len)
return -1;
}
#endif /* ACE_LINUX */
ACE_OS::strsncpy (s + i,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path + i),
len - i);
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Needs to be reviewed
Development

Successfully merging this pull request may close these issues.

2 participants