Skip to content

SkillRanks: fix UB in GetAreaModifier dereferencing empty std::optional#230

Open
plenarius wants to merge 1 commit into
masterfrom
fix_skillrank_areamod_optional
Open

SkillRanks: fix UB in GetAreaModifier dereferencing empty std::optional#230
plenarius wants to merge 1 commit into
masterfrom
fix_skillrank_areamod_optional

Conversation

@plenarius

Copy link
Copy Markdown
Owner

SkillRanks::GetAreaModifier dereferenced the std::optional<int> returned by pArea->nwnxGet<int>(...) without checking whether it had a value. When SetAreaModifier had never been called for that (area, skillId) pair, the optional was empty and * on it was undefined behaviour.

Fixed by using .value_or(0) to return the natural identity for an unset modifier. This matches how the plugin's internal skill-resolution path already treats an empty optional (it skips the += *areaMod accumulation), so observable behaviour for callers is consistent.

Tracks upstream issue nwnxee#1855.

`pArea->nwnxGet<int>(...)` returns `std::optional<int>` and is empty when
the area has no modifier set for the requested skill (i.e. SetAreaModifier
was never called for that combination). Dereferencing it via `*` was
undefined behaviour.

Use `.value_or(0)` to return the natural identity for an unset modifier,
matching how the internal skill resolution at line 675-677 already treats
an empty optional (it skips the `+= *areaMod` accumulation).

Fixes nwnxee#1855
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.

1 participant