Skip to content
Merged
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
33 changes: 33 additions & 0 deletions appsec/helper-rust/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ impl ConfigDirectory {
}

pub fn runtime_id(&self) -> anyhow::Result<&str> {
if self.data.is_empty() {
// Cleared shmem state (expired()): no config available, same as unwritten shmem.
return Ok("");
}
self.data.iter().position(|&b| b == b'\n').map_or_else(
|| {
Err(anyhow::anyhow!(
Expand All @@ -194,6 +198,13 @@ impl ConfigDirectory {
}

pub fn iter(&self) -> anyhow::Result<impl Iterator<Item = anyhow::Result<Config<'_>>> + '_> {
if self.data.is_empty() {
// Cleared shmem state (expired()): no config available, same as unwritten shmem.
return Ok(ConfigIter {
data: &self.data[..],
pos: 0,
});
}
self.data.iter().position(|&b| b == b'\n').map_or_else(
|| {
Err(anyhow::anyhow!(
Expand Down Expand Up @@ -639,6 +650,28 @@ mod tests {
Ok(payload_size)
}

#[test]
fn config_directory_handles_cleared_shmem() -> anyhow::Result<()> {
// expired() calls writer.write(&[]), which stores size=1 with just the trailing NUL byte.
// This cleared state means "no config available" and must not be treated as an error.
let name = "/helper_rust_cfg_cleared_state";
shm_create_config_dir_with_raw_payload(name, b"")?;

let mut poller = ConfigPoller::new(Path::new(OsStr::from_bytes(name.as_bytes())));
let cfg_dir = poller
.poll()?
.context("expected config snapshot when seq advanced")?;

assert_eq!(cfg_dir.runtime_id()?, "");
let configs: Vec<_> = cfg_dir.iter()?.collect::<anyhow::Result<Vec<_>>>()?;
assert!(configs.is_empty());

unsafe {
let _ = libc::shm_unlink(CString::new(name.as_bytes()).unwrap().as_ptr());
}
Ok(())
}

#[test]
fn config_directory_iter_errors_when_payload_has_no_lf() -> anyhow::Result<()> {
let outer = "/helper_rust_cfg_no_lf_iter";
Expand Down
Loading