Skip to content

Commit e0b6e58

Browse files
Fix segfault/heap buffer overflow in {Experimental,}DatasetToTFRecord where dataset is numeric.
Code assumes only strings inputs and then interprets numbers as valid `tstring`s. Then, when trying to compute the CRC of the record this results in heap buffer overflow. PiperOrigin-RevId: 387675909 Change-Id: I7396b9b8afc1ac744112af7c0b1cd7bb41e0f556
1 parent b5b9ae9 commit e0b6e58

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tensorflow/core/kernels/data/experimental/to_tf_record_op.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#include "tensorflow/core/framework/function_handle_cache.h"
1919
#include "tensorflow/core/framework/op_kernel.h"
2020
#include "tensorflow/core/framework/resource_mgr.h"
21+
#include "tensorflow/core/framework/types.h"
2122
#include "tensorflow/core/kernels/ops_util.h"
2223
#include "tensorflow/core/lib/core/threadpool.h"
2324
#include "tensorflow/core/lib/io/record_writer.h"
@@ -91,8 +92,20 @@ class ToTFRecordOp : public AsyncOpKernel {
9192
TF_RETURN_IF_ERROR(finalized_dataset->MakeIterator(
9293
&iter_ctx, /*parent=*/nullptr, "ToTFRecordOpIterator", &iterator));
9394

95+
const int num_output_dtypes = finalized_dataset->output_dtypes().size();
96+
if (num_output_dtypes != 1) {
97+
return errors::InvalidArgument(
98+
"ToTFRecordOp currently only support datasets of 1 single column, ",
99+
"but got ", num_output_dtypes);
100+
}
101+
const DataType dt = finalized_dataset->output_dtypes()[0];
102+
if (dt != DT_STRING) {
103+
return errors::InvalidArgument(
104+
"ToTFRecordOp currently only supports DT_STRING dataypes, but got ",
105+
DataTypeString(dt));
106+
}
94107
std::vector<Tensor> components;
95-
components.reserve(finalized_dataset->output_dtypes().size());
108+
components.reserve(num_output_dtypes);
96109
bool end_of_sequence;
97110
do {
98111
TF_RETURN_IF_ERROR(

0 commit comments

Comments
 (0)