Skip to content

Conversation

@are-you-tilted-already
Copy link

@are-you-tilted-already are-you-tilted-already commented Oct 25, 2025

The goal is to fix the problem with a confusing behaviour of flatbuffers::make_span with flatbuffers::Vector<(const)? T*>.
Consider the following flatbuffers schema:

// example.fbs
struct ExampleStruct {
  id: uint64;
}

table ExampleStructs {
  values: [ExampleStruct];
}

and some code working with it:

#include "example.fbs.h"

void ProcessStruct(const ExampleStruct& value);

void ProcessStructs(const flatbuffers::span<const ExampleStruct*> values) {
  for (const ExampleStruct* const value : values) {
    ProcessStruct(*value);
  }
}

const ExampleStructs* const table = ...;

// UB and most likely SIGSEGV, because the correct way to read is the one that flatbuffers::IndirectHelper<const T*> uses
// https://github.com/google/flatbuffers/blob/master/include/flatbuffers/buffer.h#L168
ProcessStructs(flatbuffers::make_span(table->values());

@google-cla
Copy link

google-cla bot commented Oct 25, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the c++ label Oct 25, 2025
@are-you-tilted-already
Copy link
Author

Some people (including me, to be honest) use spans almost whenever they could, so it's better to make flatbuffers::Vector<const T*> to flatbuffers::Span<const T*> conversion impossible.

@are-you-tilted-already
Copy link
Author

@aardappel @dbaileychess what do you think of it?

@aardappel
Copy link
Collaborator

I agree such an illegal conversion should not be possible, so disabling it is better than nothing.

Ideally it be converted to flatbuffers::Span<const T> instead, if on little endian? do we want to make that possible somehow?

And looks like CI wants some parentheses to clarify precedence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants