Stops getting address of zero-sided matrix, which indeed dereferences null.
PiperOrigin-RevId: 329709701
Change-Id: I62648769c0066d0fa904edfbef82bdcfc414904c
diff --git a/tensorflow/core/util/sparse/sparse_tensor.h b/tensorflow/core/util/sparse/sparse_tensor.h
index bc4e2c8..062226d 100644
--- a/tensorflow/core/util/sparse/sparse_tensor.h
+++ b/tensorflow/core/util/sparse/sparse_tensor.h
@@ -476,12 +476,12 @@
// Fill in indices & values.
if (st_num_entries > 0) {
std::copy_n(&st.vals_.vec<T>()(0), st_num_entries, &vals_t(offset));
- }
- const auto* st_ix = &st.ix_.matrix<int64>()(0, 0);
- auto* ix_out = &ix_t(offset, 0);
- for (std::size_t i = 0; i < st_num_entries * dims; ++i) {
- *ix_out++ = *st_ix++ + ((i % dims == primary_dim) ? shape_offset : 0);
+ const auto* st_ix = &st.ix_.matrix<int64>()(0, 0);
+ auto* ix_out = &ix_t(offset, 0);
+ for (std::size_t i = 0; i < st_num_entries * dims; ++i) {
+ *ix_out++ = *st_ix++ + ((i % dims == primary_dim) ? shape_offset : 0);
+ }
}
offset += st_num_entries;
diff --git a/tensorflow/core/util/sparse/sparse_tensor_test.cc b/tensorflow/core/util/sparse/sparse_tensor_test.cc
index 8a73211..792269f 100644
--- a/tensorflow/core/util/sparse/sparse_tensor_test.cc
+++ b/tensorflow/core/util/sparse/sparse_tensor_test.cc
@@ -592,6 +592,20 @@
EXPECT_EQ(conc_ooo.num_entries(), 4 * N);
}
+TEST(SparseTensorTest, ConcatEmptyN) {
+ constexpr int N = 0;
+ constexpr int NDIM = 2;
+ Tensor ix(DT_INT64, TensorShape({N, NDIM}));
+ Tensor vals(DT_STRING, TensorShape({N}));
+ TensorShape shape({10, 10});
+ SparseTensor st;
+ TF_ASSERT_OK(SparseTensor::Create(ix, vals, shape, {0, 1}, &st));
+
+ SparseTensor concatted = SparseTensor::Concat<tstring>({st, st, st});
+
+ EXPECT_EQ(concatted.num_entries(), 0);
+}
+
// TODO(ebrevdo): ReduceToDense(R={dim1,dim2,...}, reduce_fn, &output)
// reduce_fn sees slices of resorted values based on generator (dim: DDIMS), and
// slices of resorted indices on generator.