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

Always check read_len's remaining length in structs #176

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3335,6 +3335,16 @@ fn add_deserialize_initial_len_check(deser_body: &mut dyn CodeBlock, len_info: R
if fixed != 0 {
deser_body.line(&format!("read_len.read_elems({fixed})?;"));
}
// We MUST check even in the fixed case, as you might be parsing something that
// is a CBOR prefix field-wise to your data e.g.:
// foo = [uint, bytes]
// bar = [uint, bytes, str]
// would have any bar be parsable as foo (problematic when we have foo / bar in a choice)
// so we must ensure we end up with precisely 0 left over at the end even in fixed cases.
// We do the check right away instead of waiting. We don't do this inside of
// add_deserialize_final_len_check for all variants as some enum use-cases
// break as they rely on being able to do the final check without read_len
deser_body.line("read_len.finish()?;");
}
}
}
Expand Down Expand Up @@ -5926,6 +5936,8 @@ fn generate_enum(
// A possible workaround for this could be to read it beforehand if possible but
// that gets complicated for optional fields inside those plain groups so we'll
// just avoid this check instead for this one case.
// This can cause issues when there are overlapping (CBOR field-wise) variants inlined here.
// Issue: https://github.com/dcSpark/cddl-codegen/issues/175
add_deserialize_final_len_check(deser_body, rep, RustStructCBORLen::Fixed(0));
deser_body.line(&format!(
"Err(DeserializeError::new(\"{name}\", DeserializeFailure::NoVariantMatched))"
Expand Down
14 changes: 13 additions & 1 deletion tests/core/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,16 @@ text_map_wrapper = { 721: embedded_text }
inline_wrapper = [{ * text => text }]

top_level_array = [* uint]
top_level_single_elem = [uint]
top_level_single_elem = [uint]

overlapping_inlined = [
0 //
0, uint //
0, uint, text
]

overlapping0 = [0]
overlapping1 = [0, uint]
overlapping2 = [0, uint, text]

overlapping = overlapping0 / overlapping1 / overlapping2
21 changes: 21 additions & 0 deletions tests/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,25 @@ mod tests {
arr2.index_0 *= arr2.index_0;
assert_eq!(arr2.index_0, 81);
}

#[test]
fn overlapping() {
let overlap0 = Overlapping::new_overlapping0(Overlapping0::new());
deser_test(&overlap0);
let overlap1 = Overlapping::new_overlapping1(Overlapping1::new(9));
deser_test(&overlap1);
let overlap2 = Overlapping::new_overlapping2(Overlapping2::new(5, "overlapping".into()));
deser_test(&overlap2);
}

#[test]
fn overlapping_inlined() {
// this test won't work until https://github.com/dcSpark/cddl-codegen/issues/175 is resolved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we say sth about this limitation in the docs as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pretty niche limitation. I thought we agreed we'd use github issues instead of the readme to cover exactly things like this anyway?

let overlap0 = OverlappingInlined::new_i0();
deser_test(&overlap0);
let overlap1 = OverlappingInlined::new_overlapping_inlined1(9);
//deser_test(&overlap1);
let overlap2 = OverlappingInlined::new_overlapping_inlined2(5, "overlapping".into());
//deser_test(&overlap2);
}
}