Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
lookup, recognizes direct and compound references, and supports raw and
zlib records. The feature includes the standalone `gzip` feature because
normal installed-package `.rdx` indexes use gzip envelopes.
- [rd-rds] Add a crate-private bounded prefix inspector for serialized closure
roots. It preserves formal order and default presence, validates reference
alignment, and stops after the body tag without constructing an `RObject`.
Inspection-specific byte and formal-count limits complement the shared wire
accounting, with deterministic R-generated plain, compiled, and diagnostic
fixtures covering unsupported prefix shapes and persisted-reference
environments.

### Changed

Expand Down
20 changes: 20 additions & 0 deletions crates/rd-rds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ fn generic_evidence(object: &rd_rds::RObject) -> BTreeSet<String> {
The caller still decides how missing metadata, invalid schemas, base-generic
catalogs, shadowing, and library precedence should affect its provider.

## Closure prefix inspection

The decoder also contains a crate-private bounded inspector for consumers that
need to classify a serialized object before materializing it. For a closure it
walks attributes, environment fields, and the formal/default pairlist while
sharing strict decoding's reference registration/resolution, encoding, depth,
and element accounting. It also applies inspection-specific byte and
formal-count limits.
It reports formal names in wire order (including `...`, duplicate names, and
non-syntactic UTF-8 names), distinguishes missing defaults from present
defaults including `NULL`, and stops immediately after observing the body tag.
The body payload is intentionally not validated. Prefix failures retain their
phase and byte offset in an unavailable result after the root kind is known;
failures before the root flags remain top-level errors. This is an internal
inspection boundary, not a general R object walker or a replacement for
`parse`. Deterministic plain and compiler-produced format-2/format-3 fixtures,
along with generated diagnostic, ALTREP, S4, namespace, and persisted-reference
cases, are produced in the source repository by
`tests/fixtures/generate_closure_inspection_fixture.R`.

## Runnable examples

```text
Expand Down
47 changes: 4 additions & 43 deletions crates/rd-rds/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use crate::wire::{
BASEENV_SXP, BASENAMESPACE_SXP, BUILTINSXP, CHARSXP, CPLXSXP, EMPTYENV_SXP, ENVSXP, EXPRSXP,
GLOBALENV_SXP, INTSXP, ItemFlags, LGLSXP, LISTSXP, MISSINGARG_SXP, NA_INTEGER, NA_REAL_BITS,
Expand Down Expand Up @@ -321,12 +319,7 @@ impl Decoder {
cursor: &mut ByteCursor<'_>,
flags: ItemFlags,
) -> Result<RObject, Error> {
let inline_index = flags.ref_index_inline();
let index = if inline_index == 0 {
cursor.read_be_i32()? as u32
} else {
inline_index
};
let index = self.state.read_ref_index(cursor, flags)?;

match self
.state
Expand Down Expand Up @@ -420,15 +413,7 @@ impl Decoder {
cursor: &mut ByteCursor<'_>,
_flags: ItemFlags,
) -> Result<Symbol, Error> {
let print_name = self.decode_char_item(cursor)?;
let text = print_name
.as_str()
.ok_or(Error::InvalidSymbolName)?
.map_err(|_| Error::InvalidSymbolName)?;
let symbol = Symbol::new(Arc::<str>::from(text.as_ref()));
self.state
.register(RefEntry::Symbol(symbol.clone()), cursor.position())?;
Ok(symbol)
self.state.decode_symbol(cursor)
}

fn decode_persisted(&mut self, cursor: &mut ByteCursor<'_>) -> Result<Persisted, Error> {
Expand All @@ -444,26 +429,7 @@ impl Decoder {
/// count (with the usual -1 long-vector escape), then that many
/// `CHARSXP` items.
fn decode_string_vec(&mut self, cursor: &mut ByteCursor<'_>) -> Result<Vec<RStr>, Error> {
let _placeholder = cursor.read_be_i32()?;
let offset = cursor.position();
let len = cursor.read_be_i32()?;
let len = if len == -1 {
let len = self.state.read_long_len(cursor)?;
return Err(Error::PersistedLongVectorUnsupported { len, offset });
} else if len < 0 {
return Err(Error::NegativeLength { len, offset });
} else {
len as usize
};

if len > self.state.max_vector_len() {
return Err(Error::VectorLengthLimitExceeded {
limit: self.state.max_vector_len(),
length: len,
offset,
});
}
self.state.account_elements(len, offset)?;
let len = self.state.read_string_vec_len(cursor)?;
(0..len)
.map(|_| self.decode_char_item(cursor))
.collect::<Result<Vec<_>, _>>()
Expand Down Expand Up @@ -543,12 +509,7 @@ impl Decoder {
match flags.type_code() {
SYMSXP => self.decode_symbol_with_flags(cursor, flags),
REFSXP => {
let inline_index = flags.ref_index_inline();
let index = if inline_index == 0 {
cursor.read_be_i32()? as u32
} else {
inline_index
};
let index = self.state.read_ref_index(cursor, flags)?;
match self
.state
.resolve(index, cursor.position().saturating_sub(4))?
Expand Down
Loading