Skip to content

Commit

Permalink
Fixing segfault. Off-by-one when setting number of dimensions in Stre…
Browse files Browse the repository at this point in the history
…amingSparseFeatures.
  • Loading branch information
Thoralf Klein committed Aug 20, 2013
1 parent 0f20584 commit 4fae70c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/shogun/features/SparseFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ template<class ST> SGSparseVector<ST> CSparseFeatures<ST>::get_sparse_feature_ve
REQUIRE(num>=0 && num<get_num_vectors(), "get_sparse_feature_vector(num=%d): num exceeds [0;%d]\n", num, get_num_vectors()-1);
index_t real_num=m_subset_stack->subset_idx_conversion(num);

SGSparseVector<ST> result;

if (sparse_feature_matrix.sparse_matrix)
{
return sparse_feature_matrix[real_num];
}
else
{
SGSparseVector<ST> result;
if (feature_cache)
{
result.features=feature_cache->lock_entry(num);
Expand Down Expand Up @@ -223,6 +222,14 @@ template<class ST> void CSparseFeatures<ST>::set_sparse_feature_matrix(SGSparseM
SG_ERROR("Not allowed with subset\n");

sparse_feature_matrix=sm;

// TODO: check should be implemented in sparse matrix class
for (int32_t j=0; j<get_num_vectors(); j++) {
SGSparseVector<ST> sv=get_sparse_feature_vector(j);
REQUIRE(get_num_features() >= sv.get_num_dimensions(),
"sparse_matrix[%d] check failed (matrix features %d >= vector dimension %d)",
j, get_num_features(), sv.get_num_dimensions());
}
}

template<class ST> SGMatrix<ST> CSparseFeatures<ST>::get_full_feature_matrix()
Expand Down Expand Up @@ -533,6 +540,15 @@ template<class ST> float64_t CSparseFeatures<ST>::dense_dot(int32_t vec_idx1, co

if (sv.features)
{
// TODO: check should be implemented in sparse matrix class
REQUIRE(get_num_features() >= sv.get_num_dimensions(),
"sparse_matrix[%d] check failed (matrix features %d >= vector dimension %d)",
vec_idx1, get_num_features(), sv.get_num_dimensions());

REQUIRE(vec2_len >= sv.get_num_dimensions(),
"sparse_matrix[%d] check failed (dense vector dimension %d >= vector dimension %d)",
vec_idx1, vec2_len, sv.get_num_dimensions());

for (int32_t i=0; i<sv.num_feat_entries; i++)
result+=vec2[sv.features[i].feat_index]*sv.features[i].entry;
}
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/features/streaming/StreamingSparseFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,13 @@ bool CStreamingSparseFeatures<T>::get_next_example()
// Update number of features based on highest index
for (int32_t i=0; i<current_length; i++)
{
if (current_vector[i].feat_index > current_num_features)
if (current_vector[i].feat_index+1 > current_num_features)
current_num_features = current_vector[i].feat_index+1;
}
current_vec_index++;

ASSERT(current_num_features >= get_vector().get_num_dimensions());
ASSERT(current_num_features == CMath::max(current_num_features, get_vector().get_num_dimensions()));
return true;
}

Expand Down

0 comments on commit 4fae70c

Please sign in to comment.