Change return type of GetIntPtr() and correct endainness when needed

This commit is contained in:
Sidong-Wei 2020-11-16 10:50:10 -05:00
parent b63cb8466b
commit d267b0106b

View File

@ -50,8 +50,12 @@ void ReportError(ErrorReporter* error_reporter, const char* format, ...) {
}
}
// Returns the int32_t value pointed by ptr.
const uint32_t* GetIntPtr(const char* ptr) {
return reinterpret_cast<const uint32_t*>(ptr);
const uint32_t GetIntPtr(const char* ptr) {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return return flatbuffers::EndianScalar(*reinterpret_cast<const uint32_t*>(ptr));
#else
return *reinterpret_cast<const uint32_t*>(ptr);
#endif
}
// Verifies flatbuffer format of the model contents and returns the in-memory
@ -79,7 +83,7 @@ bool VerifyStringTensorBuffer(const Tensor& tensor, const Buffer& buffer,
}
const char* buffer_ptr = reinterpret_cast<const char*>(buffer.data()->data());
uint32_t num_strings = *GetIntPtr(buffer_ptr);
uint32_t num_strings = GetIntPtr(buffer_ptr);
if (num_strings > kMaxNumString) {
ReportError(error_reporter,
"String tensor %s has invalid num of string set: %d",
@ -100,7 +104,7 @@ bool VerifyStringTensorBuffer(const Tensor& tensor, const Buffer& buffer,
uint32_t prev_ptr = header_offsets;
uint32_t offset = sizeof(int32_t);
if (*GetIntPtr(buffer_ptr + offset) != header_offsets) {
if (GetIntPtr(buffer_ptr + offset) != header_offsets) {
ReportError(error_reporter,
"String tensor %s buffer initial offset must be: %d",
NameOrEmptyString(tensor.name()), header_offsets);
@ -108,7 +112,7 @@ bool VerifyStringTensorBuffer(const Tensor& tensor, const Buffer& buffer,
}
offset += sizeof(int32_t);
for (int i = 1, end = num_strings; i <= end; i++, offset += sizeof(int32_t)) {
int string_offset = *GetIntPtr(buffer_ptr + offset);
int string_offset = GetIntPtr(buffer_ptr + offset);
if (string_offset < static_cast<int>(prev_ptr) ||
string_offset > static_cast<int>(buffer_size)) {
ReportError(error_reporter,
@ -117,7 +121,7 @@ bool VerifyStringTensorBuffer(const Tensor& tensor, const Buffer& buffer,
return false;
}
}
if (*GetIntPtr(buffer_ptr + offset - sizeof(int32_t)) != buffer_size) {
if (GetIntPtr(buffer_ptr + offset - sizeof(int32_t)) != buffer_size) {
ReportError(error_reporter,
"String tensor %s buffer last offset must be %d",
NameOrEmptyString(tensor.name()), buffer_size);