From 0bde48e75d2e9f7c4d8af487476948d0180b4bdb Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Sun, 13 May 2018 10:09:58 -0700 Subject: [PATCH] Make CPython implementation function type-correct, which removes UB from calling a function through a pointer of the wrong type, and also removes a C-style cast. PiperOrigin-RevId: 196428430 --- .../python/lib/core/ndarray_tensor_bridge.cc | 45 +++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/tensorflow/python/lib/core/ndarray_tensor_bridge.cc b/tensorflow/python/lib/core/ndarray_tensor_bridge.cc index 65e2178..0d58385 100644 --- a/tensorflow/python/lib/core/ndarray_tensor_bridge.cc +++ b/tensorflow/python/lib/core/ndarray_tensor_bridge.cc @@ -72,10 +72,11 @@ struct TensorReleaser { extern PyTypeObject TensorReleaserType; -static void TensorReleaser_dealloc(TensorReleaser* self) { +static void TensorReleaser_dealloc(PyObject* pself) { + TensorReleaser* self = reinterpret_cast(pself); (*self->destructor)(); delete self->destructor; - TensorReleaserType.tp_free(self); + TensorReleaserType.tp_free(pself); } PyTypeObject TensorReleaserType = { @@ -84,26 +85,26 @@ PyTypeObject TensorReleaserType = { sizeof(TensorReleaser), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ - (destructor)TensorReleaser_dealloc, /* tp_dealloc */ - nullptr, /* tp_print */ - nullptr, /* tp_getattr */ - nullptr, /* tp_setattr */ - nullptr, /* tp_compare */ - nullptr, /* tp_repr */ - nullptr, /* tp_as_number */ - nullptr, /* tp_as_sequence */ - nullptr, /* tp_as_mapping */ - nullptr, /* tp_hash */ - nullptr, /* tp_call */ - nullptr, /* tp_str */ - nullptr, /* tp_getattro */ - nullptr, /* tp_setattro */ - nullptr, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - "Wrapped TensorFlow Tensor", /* tp_doc */ - nullptr, /* tp_traverse */ - nullptr, /* tp_clear */ - nullptr, /* tp_richcompare */ + TensorReleaser_dealloc, /* tp_dealloc */ + nullptr, /* tp_print */ + nullptr, /* tp_getattr */ + nullptr, /* tp_setattr */ + nullptr, /* tp_compare */ + nullptr, /* tp_repr */ + nullptr, /* tp_as_number */ + nullptr, /* tp_as_sequence */ + nullptr, /* tp_as_mapping */ + nullptr, /* tp_hash */ + nullptr, /* tp_call */ + nullptr, /* tp_str */ + nullptr, /* tp_getattro */ + nullptr, /* tp_setattro */ + nullptr, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + "Wrapped TensorFlow Tensor", /* tp_doc */ + nullptr, /* tp_traverse */ + nullptr, /* tp_clear */ + nullptr, /* tp_richcompare */ }; Status TF_DataType_to_PyArray_TYPE(TF_DataType tf_datatype, -- 2.7.4