From 4a7b57e93d0e21e93cc718920f6e8a6f2ab1cb27 Mon Sep 17 00:00:00 2001 From: Akshay Modi Date: Mon, 22 Oct 2018 11:11:15 -0700 Subject: [PATCH] Py_BuildValue might fail if the const char* cannot be interpreted as utf-8 PiperOrigin-RevId: 218198785 --- tensorflow/python/eager/pywrap_tfe_src.cc | 8 ++++++++ tensorflow/python/eager/tensor_test.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/tensorflow/python/eager/pywrap_tfe_src.cc b/tensorflow/python/eager/pywrap_tfe_src.cc index de37f7b9ca2..9cee561b32c 100644 --- a/tensorflow/python/eager/pywrap_tfe_src.cc +++ b/tensorflow/python/eager/pywrap_tfe_src.cc @@ -250,6 +250,7 @@ bool ParseTypeValue(const string& key, PyObject* py_value, TF_Status* status, tensorflow::Safe_PyObjectPtr py_type_enum( PyObject_GetAttrString(py_value, "_type_enum")); if (py_type_enum == nullptr) { + PyErr_Clear(); TF_SetStatus( status, TF_INVALID_ARGUMENT, tensorflow::strings::StrCat("Expecting a DType.dtype for attr ", key, @@ -795,6 +796,13 @@ int MaybeRaiseExceptionFromTFStatus(TF_Status* status, PyObject* exception) { if (exception_class != nullptr) { tensorflow::Safe_PyObjectPtr val( Py_BuildValue("si", msg, TF_GetCode(status))); + if (PyErr_Occurred()) { + // NOTE: This hides the actual error (i.e. the reason `status` was not + // TF_OK), but there is nothing we can do at this point since we can't + // generate a reasonable error from the status. + // Consider adding a message explaining this. + return -1; + } PyErr_SetObject(exception_class, val.get()); return -1; } else { diff --git a/tensorflow/python/eager/tensor_test.py b/tensorflow/python/eager/tensor_test.py index cc79f55d3bf..cd3d05d05e3 100644 --- a/tensorflow/python/eager/tensor_test.py +++ b/tensorflow/python/eager/tensor_test.py @@ -20,6 +20,7 @@ from __future__ import print_function import copy import re +import sys import numpy as np @@ -32,6 +33,7 @@ from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.framework import test_util from tensorflow.python.ops import array_ops +from tensorflow.python.ops import io_ops def _create_tensor(value, device=None, dtype=None): @@ -242,6 +244,12 @@ class TFETensorTest(test_util.TensorFlowTestCase): RuntimeError, "Can't copy Tensor with type string to device"): _create_tensor("test string") + def testInvalidUTF8ProducesReasonableError(self): + if sys.version_info[0] < 3: + self.skipTest("Test is only valid in python3.") + with self.assertRaises(UnicodeDecodeError): + io_ops.read_file(b"\xff") + class TFETensorUtilTest(test_util.TensorFlowTestCase):