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

fix panic on empty read from file #15

Merged
merged 1 commit into from
Dec 9, 2022
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
19 changes: 9 additions & 10 deletions core/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Implementation of in-memory files
use std::io::{Read, Seek, SeekFrom, Write};
use std::marker::PhantomData;
use std::mem::size_of;
use std::os::unix::prelude::{AsRawFd, IntoRawFd, RawFd};
use std::os::unix::prelude::{AsRawFd, FileExt, IntoRawFd, RawFd};

use anyhow::{anyhow, Result};
use memfd::{FileSeal, Memfd, MemfdOptions};
Expand Down Expand Up @@ -79,21 +78,21 @@ impl<T: Send + Clone + Sized> TempFile<T> {
// TODO: Use an approach without unsafe
let bytes =
unsafe { std::slice::from_raw_parts(value as *const T as *const u8, size_of::<T>()) };
let mut file = self.get_memfd()?.into_file();
file.seek(SeekFrom::Start(0)).typ(SystemError::Panic)?;
file.write_all(bytes)
let file = self.get_memfd()?.into_file();
file.write_all_at(bytes, 0)
.map_err(anyhow::Error::from)
.typ(SystemError::Panic)
}

/// Returns all of the TempFile's data
pub fn read(&self) -> TypedResult<T> {
let mut buf = Vec::with_capacity(size_of::<T>());
let mut file = self.get_memfd()?.into_file();
file.seek(SeekFrom::Start(0)).typ(SystemError::Panic)?;
file.read_to_end(buf.as_mut()).typ(SystemError::Panic)?;
let mut buf = vec![0u8; size_of::<T>()];
let buf = buf.as_mut_slice();
let file = self.get_memfd()?.into_file();
file.read_at(buf, 0).typ(SystemError::Panic)?;
// TODO: Use an approach without unsafe
Ok(unsafe { buf.as_slice().align_to::<T>().1[0].clone() })
let aligned = unsafe { buf.align_to::<T>() };
Ok(aligned.1[0].clone())
}

/// Returns a mutable memory map from a TempFile
Expand Down
11 changes: 6 additions & 5 deletions partition/src/apex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ impl ApexSamplingPortP4 for ApexLinuxPartition {
sampling_port_id: SamplingPortId,
message: &mut [ApexByte],
) -> Result<(Validity, MessageSize), ErrorReturnCode> {
if let Some((port, val)) = SAMPLING_PORTS
.read()
.unwrap()
.get(sampling_port_id as usize - 1)
{
let read = if let Ok(read) = SAMPLING_PORTS.read() {
read
} else {
return Err(ErrorReturnCode::NotAvailable);
};
if let Some((port, val)) = read.get(sampling_port_id as usize - 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Can sampling_port_id be zero? I see potential for an unsigned integer wrap here. If not, I would comment it at least.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The sampling_port_id is always greater than zero, since it is (incidentally) derived from the current length, after inserting an element, of the ArrayVec storing the sampling ports. https://github.com/aeronautical-informatics/apex-linux/pull/15/files#diff-d21de3347b5e6174c93c455b533568b798d139a47f7ba2564655c0731e27f889R120

if let Some(port) = CONSTANTS.sampling.get(*port) {
if message.is_empty() {
return Err(ErrorReturnCode::InvalidParam);
Expand Down