Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 325cf938 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: generate bounds checks

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost DirectHciTest
Change-Id: I2d349518e466853a65fbbb296a0ea69e34a4a34d
parent 4b0a1c71
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ class BodyField : public PacketField {

  void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override;

  void GenBoundsCheck(std::ostream&, Size, Size, std::string) const override{};
  // Body fields can only be dynamically sized.
  const SizeField* size_field_{nullptr};
};
+11 −0
Original line number Diff line number Diff line
@@ -130,3 +130,14 @@ bool PacketField::GenRustNameAndType(std::ostream& s) const {
  s << GetName() << ": " << param_type;
  return true;
}

void PacketField::GenBoundsCheck(std::ostream& s, Size start_offset, Size, std::string context) const {
  Size size = GetSize();
  s << "if bytes.len() < " << start_offset.bytes() + size.bytes() << " {";
  s << " return Err(Error::InvalidLengthError{";
  s << "    obj: \"" << context << "\".to_string(),";
  s << "    field: \"" << GetName() << "\".to_string(),";
  s << "    wanted: " << start_offset.bytes() + size.bytes() << ",";
  s << "    got: bytes.len()});";
  s << "}";
}
+2 −0
Original line number Diff line number Diff line
@@ -128,6 +128,8 @@ class PacketField : public Loggable {

  virtual void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const = 0;

  virtual void GenBoundsCheck(std::ostream& s, Size start_offset, Size, std::string) const;

  virtual bool GetterIsByRef() const {
    return true;
  }
+2 −0
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ class PaddingField : public PacketField {

  void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override;

  void GenBoundsCheck(std::ostream&, Size, Size, std::string) const override{};

 private:
  Size size_;
};
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ class PayloadField : public PacketField {

  void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override;

  void GenBoundsCheck(std::ostream&, Size, Size, std::string) const override{};

  // Payload fields can only be dynamically sized.
  const SizeField* size_field_;
  // Only used if the size of the payload is based on another field.
Loading