Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum types aren't validated #187

Open
EricRahm opened this issue Sep 12, 2024 · 0 comments
Open

Enum types aren't validated #187

EricRahm opened this issue Sep 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@EricRahm
Copy link
Collaborator

Background

Given the following definition:

enum McuCommand:
    ACK                  = 0x000000
    NACK                 = 0x000001
    BUSY                 = 0x000005

struct McuMessageHeader:
  0 [+1] McuCommand reason

I would expect the generated C++ code to validate the reason field to make sure it is a valid enum value. Or at least I'd want to have a helper that could validate it for me.

std::array<std::byte, 1> msg;
msg[0] = std::byte{static_cast<std::byte>(McuCommand::BUSY) + 1};
auto view = MakeMcuMessageHeaderView(&msg);
assert(!view.Ok()); // doesn't assert

Issue

We appear to use AllValuesAreOk as our validator for EnumView types, for example:

template <class Storage>
inline typename ::emboss::support::EnumView<
    /**/ ::McuCommand,
    ::emboss::support::FixedSizeViewParameters<24, ::emboss::support::AllValuesAreOk>,
    typename ::emboss::support::BitBlock</**/::emboss::support::LittleEndianByteOrderer<typename Storage::template OffsetStorageType</**/0, 6>>, 24>>

 GenericMcuMessageHeaderView<Storage>::reason()
    const;

This leads to, well, all values being ok.

Possible fixes

I see this as a few incremental improvements depending on what we want to do.

Generate a validator

Minmal of what we should do so that users can manually validate. Something like:

struct EnumTrait<::McuCommand> {
  static constexpr bool is_valid(std::underlying_type_t<::McuCommand> value) {
   switch(value) {
    case ::McuCommand::ACK:
    case ::McuCommand::NACK:
    case ::McuCommand::BUSY:
      return true;
    default:
      return false;
   }
  }
}

Add some min and max helpers

It might be useful for quick checks to include a min and max value. Something like:

struct EnumTrait<::McuCommand> {
  static constexpr std::underlying_type_t<::McuCommand> min = ::McuCommand::ACK;
  static constexpr std::underlying_type_t<::McuCommand> max = ::McuCommand::BUSY;

  static constexpr bool is_valid(std::underlying_type_t<::McuCommand> value) {
    if (value < min || value > max) { return false; }
    // <snip>
  }
}

Add a proper validator

We could also generate a proper validator that will work with emboss views:

struct EnumValidator<McuCommand> {
  template <typename ValueType>
  static constexpr bool ValueIsOk(ValueType value) {
    // add some checks that the value would fit in the underlying type
    return EnumTrait<McuCommand>::is_valid(static_cast<std::underlying_type_t<::McuCommand>>(value));
  }
};

Hook the validator in

We could go further and use the validator by default.
Pros: This is the right thing to do
Cons: This might cause runtime assertions for existing code.

Something like:

template <class Storage>
inline typename ::emboss::support::EnumView<
    /**/ ::McuCommand,
    ::emboss::support::FixedSizeViewParameters<24, EnumValidator<McuCommand>>,
    typename ::emboss::support::BitBlock</**/::emboss::support::LittleEndianByteOrderer<typename Storage::template OffsetStorageType</**/0, 6>>, 24>>

 GenericMcuMessageHeaderView<Storage>::reason()
    const;
@EricRahm EricRahm added the bug Something isn't working label Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant