Skip to content

Conversation

@are-you-tilted-already
Copy link
Contributor

@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
Contributor 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
Contributor 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.

@are-you-tilted-already
Copy link
Contributor Author

are-you-tilted-already commented Nov 4, 2025

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

I've made it possible via make_structs_span on little endian. Not sure if we need something like that for flatbuffers::Array<const T*>, though.

And looks like CI wants some parentheses to clarify precedence.

It is just std::is_pointer_v<T>, which requires at least C++ 17 :)

@aardappel
Copy link
Collaborator

It is just std::is_pointer_v, which requires at least C++ 17 :)

No, the actual error is error: '&&' within '||' and indeed there is a && and || at the same level.

Besides that, you'll need to modify this such that the version using std::is_pointer_v<T> is only used on compilers supporting it, or better yet, implement it in a different way that also works on older compilers. We still support all the platforms on CI.

@are-you-tilted-already
Copy link
Contributor Author

are-you-tilted-already commented Nov 4, 2025

It is just std::is_pointer_v, which requires at least C++ 17 :)

No, the actual error is error: '&&' within '||' and indeed there is a && and || at the same level.

Besides that, you'll need to modify this such that the version using std::is_pointer_v<T> is only used on compilers supporting it, or better yet, implement it in a different way that also works on older compilers. We still support all the platforms on CI.

My bad: fixed. Seems like it's more convenient to use std::is_pointer<T>::value, which requires C++ 11 only and is supported on most(?) compilers.

@aardappel
Copy link
Collaborator

Thanks!

@aardappel aardappel merged commit 5ed02dc into google:master Nov 4, 2025
47 of 50 checks passed
@are-you-tilted-already are-you-tilted-already deleted the disable_make_span_for_pointers branch November 4, 2025 21:49
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