Skip to content

Commit 2437ee3

Browse files
petrosaggbchalios
authored andcommitted
seccompiler: remove some unneeded unsafe blocks
Any `AsRef<[u8]>` value can be converted into something implementing `Read` using `std::io::Cursor` so the json parsing can be performed directly on the static strings. Signed-off-by: Petros Angelatos <[email protected]>
1 parent c4790e6 commit 2437ee3

File tree

1 file changed

+47
-85
lines changed

1 file changed

+47
-85
lines changed

src/seccompiler/src/seccompiler_bin.rs

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn get_argument_values(arguments: &ArgumentsBag) -> Result<Arguments> {
155155
})
156156
}
157157

158-
fn parse_json(reader: &mut dyn Read) -> Result<JsonFile> {
158+
fn parse_json(reader: impl Read) -> Result<JsonFile> {
159159
serde_json::from_reader(reader).map_err(Error::Json)
160160
}
161161

@@ -231,9 +231,8 @@ mod tests {
231231
use crate::backend::SeccompCmpOp::{Le, *};
232232
use crate::backend::{SeccompAction, SeccompCondition as Cond, TargetArch, TargetArchError};
233233

234-
// test helper for generating correct JSON input data
235-
fn get_correct_json_input() -> String {
236-
r#"
234+
// Correct JSON input data
235+
static CORRECT_JSON_INPUT: &str = r#"
237236
{
238237
"thread_1": {
239238
"default_action": {
@@ -328,9 +327,7 @@ mod tests {
328327
]
329328
}
330329
}
331-
"#
332-
.to_string()
333-
}
330+
"#;
334331

335332
#[test]
336333
fn test_error_messages() {
@@ -533,70 +530,51 @@ mod tests {
533530
.is_err());
534531
}
535532

536-
#[allow(clippy::useless_asref)]
537533
#[test]
538534
fn test_parse_json() {
539535
// test with malformed JSON
540536
{
541537
// empty file
542-
let mut json_input = "".to_string();
543-
let json_input = unsafe { json_input.as_bytes_mut() };
544-
545-
assert!(parse_json(&mut json_input.as_ref()).is_err());
538+
assert!(parse_json(std::io::empty()).is_err());
546539

547540
// not json
548-
let mut json_input = "hjkln".to_string();
549-
let json_input = unsafe { json_input.as_bytes_mut() };
550-
551-
assert!(parse_json(&mut json_input.as_ref()).is_err());
541+
let json_input = "hjkln";
542+
assert!(parse_json(json_input.as_bytes()).is_err());
552543

553544
// top-level array
554-
let mut json_input = "[]".to_string();
555-
let json_input = unsafe { json_input.as_bytes_mut() };
556-
557-
assert!(parse_json(&mut json_input.as_ref()).is_err());
545+
let json_input = "[]";
546+
assert!(parse_json(json_input.as_bytes()).is_err());
558547

559548
// thread key must be a string
560-
let mut json_input = "{1}".to_string();
561-
let json_input = unsafe { json_input.as_bytes_mut() };
562-
563-
assert!(parse_json(&mut json_input.as_ref()).is_err());
549+
let json_input = "{1}";
550+
assert!(parse_json(json_input.as_bytes()).is_err());
564551

565552
// empty Filter object
566-
let mut json_input = r#"{"a": {}}"#.to_string();
567-
let json_input = unsafe { json_input.as_bytes_mut() };
568-
569-
assert!(parse_json(&mut json_input.as_ref()).is_err());
553+
let json_input = r#"{"a": {}}"#;
554+
assert!(parse_json(json_input.as_bytes()).is_err());
570555

571556
// missing 'filter' field
572-
let mut json_input =
573-
r#"{"a": {"filter_action": "allow", "default_action":"log"}}"#.to_string();
574-
let json_input = unsafe { json_input.as_bytes_mut() };
575-
assert!(parse_json(&mut json_input.as_ref()).is_err());
557+
let json_input = r#"{"a": {"filter_action": "allow", "default_action":"log"}}"#;
558+
assert!(parse_json(json_input.as_bytes()).is_err());
576559

577560
// wrong key 'filters'
578-
let mut json_input =
579-
r#"{"a": {"filter_action": "allow", "default_action":"log", "filters": []}}"#
580-
.to_string();
581-
let json_input = unsafe { json_input.as_bytes_mut() };
582-
assert!(parse_json(&mut json_input.as_ref()).is_err());
561+
let json_input =
562+
r#"{"a": {"filter_action": "allow", "default_action":"log", "filters": []}}"#;
563+
assert!(parse_json(json_input.as_bytes()).is_err());
583564

584565
// wrong action 'logs'
585-
let mut json_input =
586-
r#"{"a": {"filter_action": "allow", "default_action":"logs", "filter": []}}"#
587-
.to_string();
588-
let json_input = unsafe { json_input.as_bytes_mut() };
589-
assert!(parse_json(&mut json_input.as_ref()).is_err());
566+
let json_input =
567+
r#"{"a": {"filter_action": "allow", "default_action":"logs", "filter": []}}"#;
568+
assert!(parse_json(json_input.as_bytes()).is_err());
590569

591570
// action that expects a value
592-
let mut json_input =
593-
r#"{"a": {"filter_action": "allow", "default_action":"errno", "filter": []}}"#
594-
.to_string();
595-
let json_input = unsafe { json_input.as_bytes_mut() };
596-
assert!(parse_json(&mut json_input.as_ref()).is_err());
571+
let json_input =
572+
r#"{"a": {"filter_action": "allow", "default_action":"errno", "filter": []}}"#;
573+
574+
assert!(parse_json(json_input.as_bytes()).is_err());
597575

598576
// overflowing u64 value
599-
let mut json_input = r#"
577+
let json_input = r#"
600578
{
601579
"thread_2": {
602580
"default_action": "trap",
@@ -616,13 +594,11 @@ mod tests {
616594
]
617595
}
618596
}
619-
"#
620-
.to_string();
621-
let json_input = unsafe { json_input.as_bytes_mut() };
622-
assert!(parse_json(&mut json_input.as_ref()).is_err());
597+
"#;
598+
assert!(parse_json(json_input.as_bytes()).is_err());
623599

624600
// negative integer value
625-
let mut json_input = r#"
601+
let json_input = r#"
626602
{
627603
"thread_2": {
628604
"default_action": "trap",
@@ -642,13 +618,11 @@ mod tests {
642618
]
643619
}
644620
}
645-
"#
646-
.to_string();
647-
let json_input = unsafe { json_input.as_bytes_mut() };
648-
assert!(parse_json(&mut json_input.as_ref()).is_err());
621+
"#;
622+
assert!(parse_json(json_input.as_bytes()).is_err());
649623

650624
// float value
651-
let mut json_input = r#"
625+
let json_input = r#"
652626
{
653627
"thread_2": {
654628
"default_action": "trap",
@@ -668,13 +642,11 @@ mod tests {
668642
]
669643
}
670644
}
671-
"#
672-
.to_string();
673-
let json_input = unsafe { json_input.as_bytes_mut() };
674-
assert!(parse_json(&mut json_input.as_ref()).is_err());
645+
"#;
646+
assert!(parse_json(json_input.as_bytes()).is_err());
675647

676648
// duplicate filter keys
677-
let mut json_input = r#"
649+
let json_input = r#"
678650
{
679651
"thread_1": {
680652
"default_action": "trap",
@@ -687,32 +659,22 @@ mod tests {
687659
"filter": []
688660
}
689661
}
690-
"#
691-
.to_string();
692-
let json_input = unsafe { json_input.as_bytes_mut() };
693-
assert!(parse_json(&mut json_input.as_ref()).is_err());
662+
"#;
663+
assert!(parse_json(json_input.as_bytes()).is_err());
694664
}
695665

696666
// test with correctly formed JSON
697667
{
698668
// empty JSON file
699-
let mut json_input = "{}".to_string();
700-
let json_input = unsafe { json_input.as_bytes_mut() };
701-
702-
assert_eq!(parse_json(&mut json_input.as_ref()).unwrap().0.len(), 0);
669+
let json_input = "{}";
670+
assert_eq!(parse_json(json_input.as_bytes()).unwrap().0.len(), 0);
703671

704672
// empty Filter
705-
let mut json_input =
706-
r#"{"a": {"filter_action": "allow", "default_action":"log", "filter": []}}"#
707-
.to_string();
708-
let json_input = unsafe { json_input.as_bytes_mut() };
709-
assert!(parse_json(&mut json_input.as_ref()).is_ok());
673+
let json_input =
674+
r#"{"a": {"filter_action": "allow", "default_action":"log", "filter": []}}"#;
675+
assert!(parse_json(json_input.as_bytes()).is_ok());
710676

711677
// correctly formed JSON filter
712-
let mut json_input = get_correct_json_input();
713-
// safe because we know the string is UTF-8
714-
let json_input = unsafe { json_input.as_bytes_mut() };
715-
716678
let mut filters = HashMap::new();
717679
filters.insert(
718680
"thread_1".to_string(),
@@ -765,7 +727,7 @@ mod tests {
765727
let mut v1: Vec<_> = filters.into_iter().collect();
766728
v1.sort_by(|x, y| x.0.cmp(&y.0));
767729

768-
let mut v2: Vec<_> = parse_json(&mut json_input.as_ref())
730+
let mut v2: Vec<_> = parse_json(CORRECT_JSON_INPUT.as_bytes())
769731
.unwrap()
770732
.0
771733
.into_iter()
@@ -800,10 +762,10 @@ mod tests {
800762
let in_file = TempFile::new().unwrap();
801763
let out_file = TempFile::new().unwrap();
802764

803-
let mut json_input = get_correct_json_input();
804-
// safe because we know the string is UTF-8
805-
let json_input = unsafe { json_input.as_bytes_mut() };
806-
in_file.as_file().write_all(json_input).unwrap();
765+
in_file
766+
.as_file()
767+
.write_all(CORRECT_JSON_INPUT.as_bytes())
768+
.unwrap();
807769

808770
let arguments = Arguments {
809771
input_file: in_file.as_path().to_str().unwrap().to_string(),

0 commit comments

Comments
 (0)