Fix out-of-bounds std::vector access causing R session abort - #27
Open
ssppkenny wants to merge 1 commit into
Open
Fix out-of-bounds std::vector access causing R session abort#27ssppkenny wants to merge 1 commit into
ssppkenny wants to merge 1 commit into
Conversation
Use data() + offset instead of &snip operator[] to avoid triggering _GLIBCXX_ASSERTIONS when computing past-the-end pointers. The old code called operator[] with index == size(), which is undefined behavior and causes a crash on Arch Linux, Fedora, and other distros with _GLIBCXX_ASSERTIONS enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a
std::vectorout-of-bounds access insrc/word2vec/lib/word2vec.cpp:72-74that causes an R session crash when training word2vec models on Linux distributions with_GLIBCXX_ASSERTIONSenabled (Arch Linux, Fedora, etc.).Root Cause
When
wordIndexis the last word in the vocabulary,(wordIndex + 1) * m_vectorSize == _trainMatrix.size(). The expression&_trainMatrix[size()]callsstd::vector::operator[]with an out-of-bounds index, which triggers the assertion:Using
&_trainMatrix[index]to get a past-the-end pointer is undefined behavior in C++.Fix
Replaced
&_trainMatrix[index]with_trainMatrix.data() + index, which is standard-conforming —data() + size()yields a valid past-the-end pointer, whileoperator[](size())does not.Also replaced
&v[0]withv.data()for consistency.Reproduction
On Arch Linux / Fedora, this crashes the R session immediately with a
std::vectorassertion failure.Testing
Verified the fix by rebuilding the package from source and running word2vec training and prediction — no crash, correct embeddings returned.
Related
See #(issue-number) for the original bug report.