Skip to content

Commit 7ea5ecf

Browse files
committed
Add basic error strings for deserialization errors
1 parent e02ead7 commit 7ea5ecf

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

api/src/config.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ impl BootloaderConfig {
149149
/// ELF file.
150150
///
151151
/// TODO: return error enum
152-
pub fn deserialize(serialized: &[u8]) -> Result<Self, ()> {
152+
pub fn deserialize(serialized: &[u8]) -> Result<Self, &'static str> {
153153
if serialized.len() != Self::SERIALIZED_LEN {
154-
return Err(());
154+
return Err("invalid len");
155155
}
156156

157157
let s = serialized;
158158

159159
let (uuid, s) = split_array_ref(s);
160160
if uuid != &Self::UUID {
161-
return Err(());
161+
return Err("invalid UUID");
162162
}
163163

164164
let (version, s) = {
@@ -169,7 +169,7 @@ impl BootloaderConfig {
169169
let pre = match pre {
170170
[0] => false,
171171
[1] => true,
172-
_ => return Err(()),
172+
_ => return Err("invalid pre version"),
173173
};
174174

175175
let version = ApiVersion {
@@ -206,27 +206,27 @@ impl BootloaderConfig {
206206
physical_memory: match physical_memory_some {
207207
[0] if physical_memory == [0; 9] => Option::None,
208208
[1] => Option::Some(Mapping::deserialize(&physical_memory)?),
209-
_ => return Err(()),
209+
_ => return Err("invalid phys memory value"),
210210
},
211211
page_table_recursive: match page_table_recursive_some {
212212
[0] if page_table_recursive == [0; 9] => Option::None,
213213
[1] => Option::Some(Mapping::deserialize(&page_table_recursive)?),
214-
_ => return Err(()),
214+
_ => return Err("invalid page table recursive value"),
215215
},
216216
aslr: match alsr {
217217
1 => true,
218218
0 => false,
219-
_ => return Err(()),
219+
_ => return Err("invalid aslr value"),
220220
},
221221
dynamic_range_start: match dynamic_range_start_some {
222222
[0] if dynamic_range_start == [0; 8] => Option::None,
223223
[1] => Option::Some(u64::from_le_bytes(dynamic_range_start)),
224-
_ => return Err(()),
224+
_ => return Err("invalid dynamic range start value"),
225225
},
226226
dynamic_range_end: match dynamic_range_end_some {
227227
[0] if dynamic_range_end == [0; 8] => Option::None,
228228
[1] => Option::Some(u64::from_le_bytes(dynamic_range_end)),
229-
_ => return Err(()),
229+
_ => return Err("invalid dynamic range end value"),
230230
},
231231
};
232232
(mappings, s)
@@ -242,19 +242,19 @@ impl BootloaderConfig {
242242
minimum_framebuffer_height: match min_framebuffer_height_some {
243243
[0] if min_framebuffer_height == [0; 8] => Option::None,
244244
[1] => Option::Some(u64::from_le_bytes(min_framebuffer_height)),
245-
_ => return Err(()),
245+
_ => return Err("minimum_framebuffer_height invalid"),
246246
},
247247
minimum_framebuffer_width: match min_framebuffer_width_some {
248248
[0] if min_framebuffer_width == [0; 8] => Option::None,
249249
[1] => Option::Some(u64::from_le_bytes(min_framebuffer_width)),
250-
_ => return Err(()),
250+
_ => return Err("minimum_framebuffer_width invalid"),
251251
},
252252
};
253253
(frame_buffer, s)
254254
};
255255

256256
if !s.is_empty() {
257-
return Err(());
257+
return Err("unexpected rest");
258258
}
259259

260260
Ok(Self {
@@ -509,17 +509,17 @@ impl Mapping {
509509
}
510510
}
511511

512-
fn deserialize(serialized: &[u8; 9]) -> Result<Self, ()> {
512+
fn deserialize(serialized: &[u8; 9]) -> Result<Self, &'static str> {
513513
let (&variant, s) = split_array_ref(serialized);
514514
let (&addr, s) = split_array_ref(s);
515515
if !s.is_empty() {
516-
return Err(());
516+
return Err("invalid mapping format");
517517
}
518518

519519
match variant {
520520
[0] if addr == [0; 8] => Ok(Mapping::Dynamic),
521521
[1] => Ok(Mapping::FixedAddress(u64::from_le_bytes(addr))),
522-
_ => Err(()),
522+
_ => Err("invalid mapping value"),
523523
}
524524
}
525525
}

0 commit comments

Comments
 (0)