Repository navigation
Commit 0db103b
authored
Use FlatTensorHeader::kHeaderOffset for the FlatTensor size check (pytorch#22137)
### Summary
`FlatTensorDataMap::load` checks that the buffer is at least as long as
the flatbuffer header before it reads the root table offset out of it.
It computed that length as `sizeof(flatbuffers::uoffset_t) +
flatbuffers::kFileIdentifierLength`.
`kFileIdentifierLength` only exists at namespace scope in newer
flatbuffers releases. In flatbuffers 1.12 the same constant exists only
as a member of `FlatBufferBuilder`, so a build against an older
flatbuffers fails to compile:
```
flat_tensor_data_map.cpp:303:40: error: no member named 'kFileIdentifierLength' in namespace 'flatbuffers'
```
The submodule pinned in this repository is v24.3.25, where the namespace
scope name does exist, so CI here cannot see the problem.
### What this changes
The number being computed is 8: a 4 byte root table offset followed by a
4 byte file identifier. `FlatTensorHeader::kHeaderOffset` already names
exactly that number, because the FlatTensor header begins right after
the flatbuffer header ends. The matching bounds check in `Program::load`
already uses `ExtendedHeader::kHeaderOffset` for the same reason.
So this swaps the expression for `FlatTensorHeader::kHeaderOffset`. The
bound does not move, behavior is unchanged, and no include is added,
`flat_tensor_header.h` was already included by this file.
### Test plan
Compiled `extension/flat_tensor/flat_tensor_data_map.cpp` with
`-std=c++17 -Wall` against the pinned flatbuffers v24.3.25. Clean, no
warnings and no errors.
Checked that the fix is load bearing by compiling both spellings against
both flatbuffers versions:
| | old expression | new expression |
| --- | --- | --- |
| flatbuffers 1.12.0 | fails with the error above | compiles |
| flatbuffers 24.3.25 | compiles | compiles |
The probe also `static_assert`s that the new expression equals the old
one and equals 8, so the two spellings really do produce the same bound.1 parent cbcdfc1 commit 0db103b
1 file changed
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| |||
0 commit comments