Skip to content

Conversation

@kimkulling
Copy link
Owner

@kimkulling kimkulling commented Aug 25, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved stability of file parsing by adding safeguards to avoid out-of-bounds reads when handling primitive types, reducing crashes or errors with malformed or truncated inputs. No change to behavior for valid files.
  • Style

    • Minor formatting cleanup with no user-visible impact.

@coderabbitai
Copy link

coderabbitai bot commented Aug 25, 2025

Walkthrough

Adds a bounds check in parsePrimitiveDataType to avoid comparing beyond input length; also includes a whitespace-only variable declaration change in parseHeader. No public API changes.

Changes

Cohort / File(s) Summary of Changes
Primitive type parsing guard
code/OpenDDLParser.cpp
In parsePrimitiveDataType, before strncmp against Grammar::PrimitiveTypeToken[i], added a check to skip when remaining input length is less than prim_len, preventing out-of-bounds read.
Whitespace adjustment
code/OpenDDLParser.cpp
Minor whitespace change in parseHeader local variable declaration; no functional impact.

Sequence Diagram(s)

sequenceDiagram
  participant Caller as Caller
  participant Parser as OpenDDLParser
  participant Grammar as Grammar::PrimitiveTypeToken

  Caller->>Parser: parsePrimitiveDataType(in, end)
  loop For each primitive type i
    Parser->>Parser: prim_len = strlen(Grammar[i])
    alt Remaining < prim_len
      Note over Parser: New guard: skip comparison
      Parser-->>Parser: continue
    else Remaining >= prim_len
      Parser->>Grammar: Compare with strncmp
      alt Match
        Parser-->>Caller: Return matched type
      else No match
        Parser-->>Parser: continue
      end
    end
  end
  Parser-->>Caller: Return "unknown" if none matched
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix heap-buffer-overflow in parsePrimitiveDataType (#92)

Poem

A twitch of whiskers, code made tight,
I nibbled bounds into the parser’s bite.
No more buffers overflown at night—
Just tidy hops from left to right.
Thump-thump! The bug takes flight. 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/fix_datatype_parsing_issue_92

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
code/OpenDDLParser.cpp (1)

838-845: Critical: hex literal validation condition is inverted (accepts non-hex) and loop order risks deref at end.

The current condition uses < and > with &&, which can never be true for a single character range check; it effectively treats any character as valid hex. Also, the loop condition calls isSeparator(*in) before checking in != end, risking dereference at end.

Apply this safe and correct diff:

-    while (!isSeparator(*in) && in != end) {
-        if ((*in < '0' && *in > '9') || (*in < 'a' && *in > 'f') || (*in < 'A' && *in > 'F')) {
-            ok = false;
-            break;
-        }
-        ++pos;
-        ++in;
-    }
+    while (in != end && !isSeparator(*in)) {
+        const char c = *in;
+        if (!((c >= '0' && c <= '9') ||
+              (c >= 'a' && c <= 'f') ||
+              (c >= 'A' && c <= 'F'))) {
+            ok = false;
+            break;
+        }
+        ++pos;
+        ++in;
+    }

This is orthogonal to the PR’s objective but should be fixed promptly. I’m happy to submit a separate PR if you prefer.

🧹 Nitpick comments (2)
code/OpenDDLParser.cpp (2)

590-602: Optional hardening: ensure token-boundary after a successful match.

Today, a prefix match like "doubleX" would still be accepted as "double". If the grammar forbids trailing identifier chars after the primitive type, add a boundary check. Also avoid recomputing (end - in) and reusing prim_len for clarity.

Apply this diff to tighten matching:

-    for (size_t i = 0; i < (size_t) Value::ValueType::ddl_types_max; i++) {
-        prim_len = strlen(Grammar::PrimitiveTypeToken[i]);
-        if (static_cast<size_t>(end - in) < prim_len) {
-            continue;
-        }
-        if (0 == strncmp(in, Grammar::PrimitiveTypeToken[i], prim_len)) {
-            type = static_cast<Value::ValueType>(i);
-            break;
-        }
-    }
+    for (size_t i = 0; i < (size_t)Value::ValueType::ddl_types_max; ++i) {
+        prim_len = ::strlen(Grammar::PrimitiveTypeToken[i]);
+        const size_t remaining = static_cast<size_t>(end - in);
+        if (remaining < prim_len) {
+            continue;
+        }
+        if (0 == ::strncmp(in, Grammar::PrimitiveTypeToken[i], prim_len)) {
+            // Ensure we don't accept partial identifiers like "doubleX".
+            if (remaining > prim_len) {
+                const char next = *(in + prim_len);
+                if (isCharacter(next) || isNumeric(next) || next == '_') {
+                    continue;
+                }
+            }
+            type = static_cast<Value::ValueType>(i);
+            break;
+        }
+    }

If the spec does allow immediately-adjacent punctuation (e.g., '[' or '{'), the above still permits it.


577-596: Add a regression test for truncated/partial primitive type tokens.

To prevent future regressions of the heap-buffer-overflow, add tests that feed inputs where the buffer ends mid-token (e.g., "unsign", "doubl", "ref" without enough trailing bytes) and verify parsePrimitiveDataType returns ddl_none without touching memory past end. Also add a positive test per token to ensure behavior is unchanged when fully present.

I can draft a minimal unit test that iterates over Grammar::PrimitiveTypeToken, truncates each by 1..N, and asserts no match/crash. Want me to open a follow-up PR with those tests?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ac2896b and 738fa97.

📒 Files selected for processing (1)
  • code/OpenDDLParser.cpp (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: PR (address)
🔇 Additional comments (2)
code/OpenDDLParser.cpp (2)

301-301: LGTM: whitespace-only change in local declaration.

No functional impact; consistent with surrounding style.


587-589: Correct and sufficient OOB read fix before strncmp.

The length guard ensures we never read past end when comparing against PrimitiveTypeToken, addressing the heap-buffer-overflow reported in issue #92.

@sonarqubecloud
Copy link

@kimkulling kimkulling merged commit 0ffc8c2 into master Aug 25, 2025
3 of 4 checks passed
@kimkulling kimkulling deleted the bugfix/fix_datatype_parsing_issue_92 branch August 25, 2025 13:01
@kimkulling kimkulling added bug fuzzer Bugs related to fuzzer tests labels Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fuzzer Bugs related to fuzzer tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: heap-buffer-overflow in ODDLParser::OpenDDLParser::parsePrimitiveDataType

3 participants