summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/fields/payload_field.cc
diff options
context:
space:
mode:
Diffstat (limited to 'system/gd/packet/parser/fields/payload_field.cc')
-rw-r--r--system/gd/packet/parser/fields/payload_field.cc24
1 files changed, 23 insertions, 1 deletions
diff --git a/system/gd/packet/parser/fields/payload_field.cc b/system/gd/packet/parser/fields/payload_field.cc
index 844488242e..93491a1476 100644
--- a/system/gd/packet/parser/fields/payload_field.cc
+++ b/system/gd/packet/parser/fields/payload_field.cc
@@ -117,7 +117,29 @@ std::string PayloadField::GetRustDataType() const {
return "Vec::<u8>";
}
-void PayloadField::GenRustGetter(std::ostream& s, Size start_offset, Size) const {
+void PayloadField::GenBoundsCheck(std::ostream& s, Size start_offset, Size, std::string parent_name) const {
+ if (size_field_ != nullptr) {
+ s << "let want_ = " << start_offset.bytes() << " + (" << size_field_->GetName() << " as usize)";
+ if (!size_modifier_.empty()) {
+ s << " - ((" << size_modifier_.substr(1) << ") / 8)";
+ }
+ s << ";";
+ s << "if bytes.len() < want_ {";
+ s << " return Err(Error::InvalidLengthError{";
+ s << " obj: \"" << parent_name << "\".to_string(),";
+ s << " field: \"" << GetName() << "\".to_string(),";
+ s << " wanted: want_,";
+ s << " got: bytes.len()});";
+ s << "}";
+ if (!size_modifier_.empty()) {
+ s << "if ((" << size_field_->GetName() << " as usize) < ((" << size_modifier_.substr(1) << ") / 8)) {";
+ s << " return Err(Error::ImpossibleStructError);";
+ s << "}";
+ }
+ }
+}
+
+void PayloadField::GenRustGetter(std::ostream& s, Size start_offset, Size, std::string) const {
s << "let " << GetName() << ": " << GetRustDataType() << " = ";
if (size_field_ == nullptr) {
s << "bytes[" << start_offset.bytes() << "..].into();";