Skip to content

Fix out-of-bounds std::vector access causing R session abort - #27

Open
ssppkenny wants to merge 1 commit into
bnosac:masterfrom
ssppkenny:master
Open

Fix out-of-bounds std::vector access causing R session abort#27
ssppkenny wants to merge 1 commit into
bnosac:masterfrom
ssppkenny:master

Conversation

@ssppkenny

Copy link
Copy Markdown

Summary

Fixes a std::vector out-of-bounds access in src/word2vec/lib/word2vec.cpp:72-74 that causes an R session crash when training word2vec models on Linux distributions with _GLIBCXX_ASSERTIONS enabled (Arch Linux, Fedora, etc.).

Root Cause

std::copy(&_trainMatrix[wordIndex * m_vectorSize],
          &_trainMatrix[(wordIndex + 1) * m_vectorSize],
          &v[0]);

When wordIndex is the last word in the vocabulary, (wordIndex + 1) * m_vectorSize == _trainMatrix.size(). The expression &_trainMatrix[size()] calls std::vector::operator[] with an out-of-bounds index, which triggers the assertion:

/usr/include/c++/16/bits/stl_vector.h:1253: Assertion __n < this->size() failed.

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, while operator[](size()) does not.

Also replaced &v[0] with v.data() for consistency.

Reproduction

library(word2vec)
word2vec(x = c("i saw the queen yesterday"), type = "cbow", dim = 15, iter = 20)

On Arch Linux / Fedora, this crashes the R session immediately with a std::vector assertion 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.

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