Skip to content

Fix aiGetMaterialProperty in the presence of material keys that are prefixes of other keys#6603

Open
Valakor wants to merge 2 commits into
assimp:masterfrom
Valakor:fix-aiGetMaterialProperty
Open

Fix aiGetMaterialProperty in the presence of material keys that are prefixes of other keys#6603
Valakor wants to merge 2 commits into
assimp:masterfrom
Valakor:fix-aiGetMaterialProperty

Conversation

@Valakor
Copy link
Copy Markdown
Contributor

@Valakor Valakor commented Apr 20, 2026

The existing code compares each property key in a material to the input via strncmp(prop->mKey.data, pKey, strlen(pKey)). This will incorrectly return longer keys whose prefix matches the searched-for key. For example: searching for "$tex.mapping" when the material contains "$tex.mappingname" will return the "mappingname" property despite that not being the requested key.

I haven't done an exhaustive search of all material keys to find common prefixes, but I know this affects at least these two glTF properties that contain the common prefix AI_MATKEY_MAPPING: AI_MATKEY_GLTF_MAPPINGNAME and AI_MATKEY_GLTF_MAPPINGID.

Summary by CodeRabbit

  • Bug Fixes
    • Material property lookup now uses a full-name comparison to prevent partial/prefix matches, ensuring precise property selection and correct application.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 20, 2026

📝 Walkthrough

Walkthrough

Updated the key comparison in aiGetMaterialProperty from using a length-limited prefix comparison to using a fixed-size strncmp with AI_MAXLEN; type/index filtering and loop/control flow remain unchanged.

Changes

Cohort / File(s) Summary
Material property matching
code/Material/MaterialSystem.cpp
Replaced strncmp(..., strlen(pKey)) usage with strncmp(..., AI_MAXLEN) when comparing prop->mKey.data to pKey, altering the key-match comparison length used during lookup. No other logic changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇 A tiny tweak within the code,
One bound adjusted on the road,
Keys now checked by fixed-size span,
The rabbit hops, applauds the plan,
Quiet change — a precise little ode.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main fix: preventing incorrect prefix-based matching in aiGetMaterialProperty when material keys share prefixes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@kimkulling
Copy link
Copy Markdown
Member

Good catch, thanks for your input. I guess using strcpy will create some security flaws.

@Valakor
Copy link
Copy Markdown
Contributor Author

Valakor commented Apr 21, 2026

The codebase seems to use strcmp fairly ubiquitously which is why I changed it to that, but it would be easy to instead do something like strncmp(prop->mKey.data, pKey, std::max(prop->mKey.length, strlen(pKey))) if that is preferred.

…String can never be longer than this we'll either:

1. Finish comparing before AI_MAXLEN
2. Stop comparing after AI_MAXLEN and consider the strings "equal" even if "pKey" is longer

This feels like a reasonable compromise to avoid malformed (non zero terminated) strings since I wouldn't expect a property name to be 1024 characters long.
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@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.

🧹 Nitpick comments (1)
code/Material/MaterialSystem.cpp (1)

76-76: Fix correctly addresses the prefix-match bug.

prop->mKey.data is a fixed char[AI_MAXLEN] buffer that is always NUL-terminated within AI_MAXLEN per the aiString invariants, and strncmp halts at the first NUL in either operand. The comparison therefore (a) never reads past either string's NUL, and (b) correctly rejects prefix-only matches such as "$tex.mapping" vs "$tex.mappingname" because the differing byte at the shorter key's NUL position causes a mismatch. Even a hypothetical pKey longer than AI_MAXLEN is safely rejected, since the comparison terminates at prop->mKey.data's NUL.

One optional consistency note: the other key comparisons in this same file (lines 354, 457, 494) use strcmp, which is functionally equivalent here given the aiString invariants. Given the PR discussion already settled on strncmp(..., AI_MAXLEN) as a defensive compromise, this is fine to leave as-is; just flagging for awareness.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/Material/MaterialSystem.cpp` at line 76, The change using
strncmp(prop->mKey.data, pKey, AI_MAXLEN) is correct and safe because aiString
guarantees NUL-termination within AI_MAXLEN, so keep this comparison as-is (it
prevents prefix-only matches like "$tex.mapping" vs "$tex.mappingname"). Ensure
you retain the check that also validates type (UINT_MAX wildcard) in the same
condition (refer to prop->mKey.data, pKey, AI_MAXLEN and the type/check for
UINT_MAX). Optionally, for consistency you may change the other key comparisons
that use strcmp (seen elsewhere in this file) to the same strncmp(...,
AI_MAXLEN) pattern, but this is not required.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/Material/MaterialSystem.cpp`:
- Line 76: The change using strncmp(prop->mKey.data, pKey, AI_MAXLEN) is correct
and safe because aiString guarantees NUL-termination within AI_MAXLEN, so keep
this comparison as-is (it prevents prefix-only matches like "$tex.mapping" vs
"$tex.mappingname"). Ensure you retain the check that also validates type
(UINT_MAX wildcard) in the same condition (refer to prop->mKey.data, pKey,
AI_MAXLEN and the type/check for UINT_MAX). Optionally, for consistency you may
change the other key comparisons that use strcmp (seen elsewhere in this file)
to the same strncmp(..., AI_MAXLEN) pattern, but this is not required.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 49f4431d-7699-42cf-b465-6264ded8fb92

📥 Commits

Reviewing files that changed from the base of the PR and between 6484dce and 12968ed.

📒 Files selected for processing (1)
  • code/Material/MaterialSystem.cpp

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.

2 participants