google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
68 stars 21 forks source link

Implicit narrowing conversion in OffsetBitBlock #60

Closed BenjaminLawson closed 1 year ago

BenjaminLawson commented 1 year ago

In Fuchsia, implicit narrowing conversions are errors, so OffsetBitBlock fails to compile:

../../third_party/github.com/google/emboss/src/runtime/cpp/emboss_memory_util.h:917:9: error: implicit conversion loses integer precision: 'int' to 'ValueType' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion]
        ~(MaskToNBits(static_cast<ValueType>(~ValueType{0}), size_) << offset_);

I'll work on a merge request.

BenjaminLawson commented 1 year ago

Looks like the bitwise operators return int, so we need to add static_cast around them:

  ValueType MaskInValue(ValueType original_value, ValueType new_value) const {
    ValueType original_mask = static_cast<ValueType>(
        ~(MaskToNBits(static_cast<ValueType>(~ValueType{0}), size_) << offset_));
    return static_cast<ValueType>((original_value & original_mask) | (new_value << offset_));
  }