Skip to content

Commit 6a4796a

Browse files
fix: deep code review pass 34 - 3 issues fixed (#280)
- Data::with_template_field_lengths() returns Result instead of panicking via assert_eq! (public API should never panic on invalid input) - OptionsData::with_template_field_lengths() same treatment for consistency - Data::new() adds debug_assert catching variable-length field misuse - Remove unreachable template_size==0 dead code in IPFIX parse_inner
1 parent d495c68 commit 6a4796a

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

‎src/variable_versions/ipfix/parser.rs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,13 +1003,7 @@ impl<'a> FieldParser {
10031003
})
10041004
.sum();
10051005
// template_fields is non-empty (checked above) and each contributes >= 1 byte,
1006-
// so template_size is always > 0 here. Return error to match V9 behavior.
1007-
if template_size == 0 {
1008-
return Err(nom::Err::Error(nom::error::Error::new(
1009-
i,
1010-
nom::error::ErrorKind::Verify,
1011-
)));
1012-
}
1006+
// so template_size is always > 0 here.
10131007
let estimated_records = (i.len() / template_size).min(max_records);
10141008
let mut res = Vec::with_capacity(estimated_records);
10151009

‎src/variable_versions/ipfix/types.rs‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ impl Data {
187187
/// [`Data::with_template_field_lengths`] when the template contains
188188
/// variable-length fields (field_length == 65535).
189189
pub fn new(fields: Vec<IPFixFlowRecord>) -> Self {
190+
debug_assert!(
191+
!fields.iter().any(|record| record
192+
.iter()
193+
.any(|(_, v)| matches!(v, FieldValue::Vec(b) if b.len() > 254))),
194+
"Data::new() should not be used with fields that may need variable-length \
195+
encoding; use Data::with_template_field_lengths() instead"
196+
);
190197
Self {
191198
fields,
192199
padding: vec![],
@@ -206,25 +213,30 @@ impl Data {
206213
/// Each entry in `template_field_lengths` corresponds to the template
207214
/// field at the same index; entries with value 65535 cause an RFC 7011
208215
/// variable-length prefix to be emitted during serialization.
216+
///
217+
/// # Errors
218+
///
219+
/// Returns an error if `template_field_lengths` is non-empty and its
220+
/// length does not match the number of fields in the first record.
209221
pub fn with_template_field_lengths(
210222
fields: Vec<IPFixFlowRecord>,
211223
template_field_lengths: Vec<u16>,
212-
) -> Self {
224+
) -> Result<Self, String> {
213225
if !fields.is_empty() && !template_field_lengths.is_empty() {
214226
let record_len = fields[0].len();
215-
assert_eq!(
216-
template_field_lengths.len(),
217-
record_len,
218-
"template_field_lengths length ({}) must match record field count ({})",
219-
template_field_lengths.len(),
220-
record_len,
221-
);
227+
if template_field_lengths.len() != record_len {
228+
return Err(format!(
229+
"template_field_lengths length ({}) must match record field count ({})",
230+
template_field_lengths.len(),
231+
record_len,
232+
));
233+
}
222234
}
223-
Self {
235+
Ok(Self {
224236
fields,
225237
padding: vec![],
226238
template_field_lengths,
227-
}
239+
})
228240
}
229241
}
230242

@@ -271,15 +283,30 @@ impl OptionsData {
271283
}
272284

273285
/// Creates a new OptionsData instance with explicit template field lengths.
286+
///
287+
/// # Errors
288+
///
289+
/// Returns an error if `template_field_lengths` is non-empty and its
290+
/// length does not match the number of fields in the first record.
274291
pub fn with_template_field_lengths(
275292
fields: Vec<Vec<IPFixFieldPair>>,
276293
template_field_lengths: Vec<u16>,
277-
) -> Self {
278-
Self {
294+
) -> Result<Self, String> {
295+
if !fields.is_empty() && !template_field_lengths.is_empty() {
296+
let record_len = fields[0].len();
297+
if template_field_lengths.len() != record_len {
298+
return Err(format!(
299+
"template_field_lengths length ({}) must match record field count ({})",
300+
template_field_lengths.len(),
301+
record_len,
302+
));
303+
}
304+
}
305+
Ok(Self {
279306
fields,
280307
padding: vec![],
281308
template_field_lengths,
282-
}
309+
})
283310
}
284311
}
285312

0 commit comments

Comments
 (0)