MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / VerifyStringTensorBuffer

Function VerifyStringTensorBuffer

tensorflow/lite/tools/verifier.cc:57–111  ·  view source on GitHub ↗

Verifies string tensor has legit buffer contents that follow the schema defined in lite/string_util.h

Source from the content-addressed store, hash-verified

55// Verifies string tensor has legit buffer contents that follow the schema
56// defined in lite/string_util.h
57bool VerifyStringTensorBuffer(const Tensor& tensor, const Buffer& buffer,
58 ErrorReporter* error_reporter) {
59 uint32_t buffer_size = buffer.data()->size();
60 if (buffer_size < sizeof(uint32_t)) {
61 ReportError(error_reporter, "String tensor %s is invalid (empty)",
62 tensor.name()->c_str());
63 return false;
64 }
65 const char* buffer_ptr = reinterpret_cast<const char*>(buffer.data()->data());
66
67 uint32_t num_strings = *GetIntPtr(buffer_ptr);
68 if (num_strings > kMaxNumString) {
69 ReportError(error_reporter,
70 "String tensor %s has invalid num of string set: %d",
71 tensor.name()->c_str(), num_strings);
72 return false;
73 }
74 uint32_t header_offsets =
75 static_cast<uint32_t>(num_strings + 2) * sizeof(int32_t);
76
77 if (buffer_size < header_offsets) {
78 ReportError(error_reporter,
79 "String tensor %s buffer requires at least %d bytes, but is "
80 "allocated with %d bytes",
81 tensor.name()->c_str(), header_offsets, buffer_size);
82 return false;
83 }
84
85 uint32_t prev_ptr = header_offsets;
86 uint32_t offset = sizeof(int32_t);
87
88 if (*GetIntPtr(buffer_ptr + offset) != header_offsets) {
89 ReportError(error_reporter,
90 "String tensor %s buffer initial offset must be: %d",
91 tensor.name()->c_str(), header_offsets);
92 return false;
93 }
94 offset += sizeof(int32_t);
95 for (int i = 1; i <= num_strings; i++, offset += sizeof(int32_t)) {
96 int string_offset = *GetIntPtr(buffer_ptr + offset);
97 if (string_offset < prev_ptr || string_offset > buffer_size) {
98 ReportError(error_reporter,
99 "String tensor %s buffer is invalid: index %d",
100 tensor.name()->c_str(), i);
101 return false;
102 }
103 }
104 if (*GetIntPtr(buffer_ptr + offset - sizeof(int32_t)) != buffer_size) {
105 ReportError(error_reporter,
106 "String tensor %s buffer last offset must be %d",
107 tensor.name()->c_str(), buffer_size);
108 return false;
109 }
110 return true;
111}
112
113// Verifies numeric tensor has legit buffer.
114bool VerifyNumericTensorBuffer(const Tensor& tensor, const Buffer& buffer,

Callers 1

VerifyTensorsFunction · 0.85

Calls 6

c_strMethod · 0.80
ReportErrorFunction · 0.70
GetIntPtrFunction · 0.70
nameMethod · 0.65
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected