From f5ea52868777b63283200c2261e85001999913f5 Mon Sep 17 00:00:00 2001 From: Gregory Chanan Date: Mon, 25 Mar 2019 08:53:42 -0700 Subject: [PATCH] Don't segfault on trying to get data_ptr of sparse tensor. (#18347) Summary: Also asserts in storage_initialized that there is a storage. Pull Request resolved: https://github.com/pytorch/pytorch/pull/18347 Differential Revision: D14582028 Pulled By: gchanan fbshipit-source-id: df3f5d181188f39e361839169fd054539c3b2839 --- c10/core/TensorImpl.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/c10/core/TensorImpl.h b/c10/core/TensorImpl.h index 6a8f408..24ddaac 100644 --- a/c10/core/TensorImpl.h +++ b/c10/core/TensorImpl.h @@ -564,6 +564,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target { template inline T * data() const { AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged + AT_CHECK(has_storage(), + "Cannot access data pointer of Tensor that doesn't have storage"); AT_ASSERTM( storage_initialized(), "The tensor has a non-zero number of elements, but its data is not allocated yet. " @@ -595,7 +597,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target { */ inline void* data() const { AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged - AT_ASSERT(storage_initialized()); + AT_CHECK(has_storage(), + "Cannot access data pointer of Tensor that doesn't have storage"); AT_ASSERT(dtype_initialized()); return static_cast( static_cast(storage_.data()) + @@ -1243,7 +1246,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target { * True if a tensor is storage initialized. A tensor may become * storage UNINITIALIZED after a Resize() or FreeMemory() */ - bool storage_initialized() const noexcept { + bool storage_initialized() const { + AT_ASSERT(has_storage()); return storage_.data() || numel_ == 0; } -- 2.7.4