Skip to content

Commit bf7b1f7

Browse files
gyscosclaude
andcommitted
Refuse to reuse a context an error may have left undefined
zstd.h says of both streaming APIs that "if an operation ends with an error, it may leave [the context] in an undefined state", and that calling them again on such a state is UB - the context has to be reset first. zstd-safe passed that obligation on to callers without saying so, so safe code could hit UB just by ignoring an error and carrying on. Track it: a failing streaming call records its error code, and the next one hands that error back instead of calling into zstd. Anything that starts a new job clears it - the reset directives that drop the session, the initCStream/initDStream family, and the one-shot compress and decompress calls, all of which zstd documents as implying a reset. Costs one predictable branch per streaming call, as suggested on the issue. try_clone carries the flag over to the copy. Fixes #315. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TeZDBS4Uw5kdF77tkJ7cAe
1 parent 1565618 commit bf7b1f7

2 files changed

Lines changed: 175 additions & 16 deletions

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,46 @@ pub fn compress_bound(src_size: usize) -> usize {
220220
unsafe { zstd_sys::ZSTD_compressBound(src_size) }
221221
}
222222

223+
/// Remembers an error that may have left a context in a state zstd considers
224+
/// undefined.
225+
///
226+
/// zstd.h says of both `ZSTD_compressStream2()` and `ZSTD_decompressStream()`
227+
/// that "if an operation ends with an error, it may leave [the context] in an
228+
/// undefined state", and that calling them again on such a state is undefined
229+
/// behaviour - the context has to be reset first. So once a streaming
230+
/// operation fails, refuse to run another one until something resets the
231+
/// context, and hand the original error back instead.
232+
#[derive(Clone, Debug, Default)]
233+
struct Poison(Option<ErrorCode>);
234+
235+
impl Poison {
236+
/// Fails with the error that poisoned the context, if there was one.
237+
fn guard(&self) -> Result<(), ErrorCode> {
238+
match self.0 {
239+
Some(code) => Err(code),
240+
None => Ok(()),
241+
}
242+
}
243+
244+
/// Remembers `res` if it failed, and passes it through.
245+
fn record(&mut self, res: SafeResult) -> SafeResult {
246+
if let Err(code) = res {
247+
self.0 = Some(code);
248+
}
249+
res
250+
}
251+
252+
/// The context was reset, so it is usable again.
253+
fn clear(&mut self) {
254+
self.0 = None;
255+
}
256+
}
257+
223258
/// Compression context
224259
///
225260
/// It is recommended to allocate a single context per thread and re-use it
226261
/// for many compression operations.
227-
pub struct CCtx<'a>(NonNull<zstd_sys::ZSTD_CCtx>, PhantomData<&'a ()>);
262+
pub struct CCtx<'a>(NonNull<zstd_sys::ZSTD_CCtx>, PhantomData<&'a ()>, Poison);
228263

229264
impl Default for CCtx<'_> {
230265
fn default() -> Self {
@@ -241,6 +276,7 @@ impl<'a> CCtx<'a> {
241276
Some(CCtx(
242277
NonNull::new(unsafe { zstd_sys::ZSTD_createCCtx() })?,
243278
PhantomData,
279+
Poison::default(),
244280
))
245281
}
246282

@@ -261,6 +297,7 @@ impl<'a> CCtx<'a> {
261297
src: &[u8],
262298
compression_level: CompressionLevel,
263299
) -> SafeResult {
300+
self.2.clear();
264301
// Safety: ZSTD_compressCCtx returns how many bytes were written.
265302
unsafe {
266303
dst.write_from(|buffer, capacity| {
@@ -282,6 +319,7 @@ impl<'a> CCtx<'a> {
282319
dst: &mut C,
283320
src: &[u8],
284321
) -> SafeResult {
322+
self.2.clear();
285323
// Safety: ZSTD_compress2 returns how many bytes were written.
286324
unsafe {
287325
dst.write_from(|buffer, capacity| {
@@ -304,6 +342,7 @@ impl<'a> CCtx<'a> {
304342
dict: &[u8],
305343
compression_level: CompressionLevel,
306344
) -> SafeResult {
345+
self.2.clear();
307346
// Safety: ZSTD_compress_usingDict returns how many bytes were written.
308347
unsafe {
309348
dst.write_from(|buffer, capacity| {
@@ -328,6 +367,7 @@ impl<'a> CCtx<'a> {
328367
src: &[u8],
329368
cdict: &CDict<'_>,
330369
) -> SafeResult {
370+
self.2.clear();
331371
// Safety: ZSTD_compress_usingCDict returns how many bytes were written.
332372
unsafe {
333373
dst.write_from(|buffer, capacity| {
@@ -349,11 +389,12 @@ impl<'a> CCtx<'a> {
349389
/// * `reset()`
350390
/// * `set_parameter(CompressionLevel, compression_level)`
351391
pub fn init(&mut self, compression_level: CompressionLevel) -> SafeResult {
392+
self.2.clear();
352393
// Safety: Just FFI
353394
let code = unsafe {
354395
zstd_sys::ZSTD_initCStream(self.0.as_ptr(), compression_level)
355396
};
356-
parse_code(code)
397+
self.2.record(parse_code(code))
357398
}
358399

359400
/// Wraps the `ZSTD_initCStream_srcSize()` function.
@@ -365,6 +406,7 @@ impl<'a> CCtx<'a> {
365406
compression_level: CompressionLevel,
366407
pledged_src_size: u64,
367408
) -> SafeResult {
409+
self.2.clear();
368410
// Safety: Just FFI
369411
let code = unsafe {
370412
zstd_sys::ZSTD_initCStream_srcSize(
@@ -385,6 +427,8 @@ impl<'a> CCtx<'a> {
385427
dict: &[u8],
386428
compression_level: CompressionLevel,
387429
) -> SafeResult {
430+
self.2.clear();
431+
self.2.clear();
388432
// Safety: Just FFI
389433
let code = unsafe {
390434
zstd_sys::ZSTD_initCStream_usingDict(
@@ -497,6 +541,7 @@ impl<'a> CCtx<'a> {
497541
output: &mut OutBuffer<'_, C>,
498542
input: &mut InBuffer<'_>,
499543
) -> SafeResult {
544+
self.2.guard()?;
500545
let mut output = output.wrap();
501546
let mut input = input.wrap();
502547
// Safety: Just FFI
@@ -507,7 +552,7 @@ impl<'a> CCtx<'a> {
507552
ptr_mut(&mut input),
508553
)
509554
};
510-
parse_code(code)
555+
self.2.record(parse_code(code))
511556
}
512557

513558
/// Performs a step of a streaming compression operation.
@@ -531,17 +576,19 @@ impl<'a> CCtx<'a> {
531576
input: &mut InBuffer<'_>,
532577
end_op: zstd_sys::ZSTD_EndDirective,
533578
) -> SafeResult {
579+
self.2.guard()?;
534580
let mut output = output.wrap();
535581
let mut input = input.wrap();
536582
// Safety: Just FFI
537-
parse_code(unsafe {
583+
let code = unsafe {
538584
zstd_sys::ZSTD_compressStream2(
539585
self.0.as_ptr(),
540586
ptr_mut(&mut output),
541587
ptr_mut(&mut input),
542588
end_op,
543589
)
544-
})
590+
};
591+
self.2.record(parse_code(code))
545592
}
546593

547594
/// Flush any intermediate buffer.
@@ -553,12 +600,13 @@ impl<'a> CCtx<'a> {
553600
&mut self,
554601
output: &mut OutBuffer<'_, C>,
555602
) -> SafeResult {
603+
self.2.guard()?;
556604
let mut output = output.wrap();
557605
// Safety: Just FFI
558606
let code = unsafe {
559607
zstd_sys::ZSTD_flushStream(self.0.as_ptr(), ptr_mut(&mut output))
560608
};
561-
parse_code(code)
609+
self.2.record(parse_code(code))
562610
}
563611

564612
/// Ends the stream.
@@ -570,12 +618,13 @@ impl<'a> CCtx<'a> {
570618
&mut self,
571619
output: &mut OutBuffer<'_, C>,
572620
) -> SafeResult {
621+
self.2.guard()?;
573622
let mut output = output.wrap();
574623
// Safety: Just FFI
575624
let code = unsafe {
576625
zstd_sys::ZSTD_endStream(self.0.as_ptr(), ptr_mut(&mut output))
577626
};
578-
parse_code(code)
627+
self.2.record(parse_code(code))
579628
}
580629

581630
/// Returns the size currently used by this context.
@@ -593,9 +642,14 @@ impl<'a> CCtx<'a> {
593642
/// Wraps the `ZSTD_CCtx_reset()` function.
594643
pub fn reset(&mut self, reset: ResetDirective) -> SafeResult {
595644
// Safety: Just FFI
596-
parse_code(unsafe {
645+
let resets_session = reset.resets_session();
646+
let res = parse_code(unsafe {
597647
zstd_sys::ZSTD_CCtx_reset(self.0.as_ptr(), reset.as_sys())
598-
})
648+
});
649+
if res.is_ok() && resets_session {
650+
self.2.clear();
651+
}
652+
res
599653
}
600654

601655
/// Sets a compression parameter.
@@ -767,7 +821,7 @@ impl<'a> CCtx<'a> {
767821
)
768822
})?;
769823

770-
Ok(CCtx(context, self.1))
824+
Ok(CCtx(context, self.1, self.2.clone()))
771825
}
772826

773827
/// Wraps the `ZSTD_getBlockSize()` function.
@@ -908,7 +962,7 @@ pub fn get_error_name(code: usize) -> &'static str {
908962
/// If no dictionary was used, it will most likely be `'static`.
909963
///
910964
/// Same as `DStream`.
911-
pub struct DCtx<'a>(NonNull<zstd_sys::ZSTD_DCtx>, PhantomData<&'a ()>);
965+
pub struct DCtx<'a>(NonNull<zstd_sys::ZSTD_DCtx>, PhantomData<&'a ()>, Poison);
912966

913967
impl Default for DCtx<'_> {
914968
fn default() -> Self {
@@ -924,6 +978,7 @@ impl<'a> DCtx<'a> {
924978
Some(DCtx(
925979
NonNull::new(unsafe { zstd_sys::ZSTD_createDCtx() })?,
926980
PhantomData,
981+
Poison::default(),
927982
))
928983
}
929984

@@ -948,6 +1003,7 @@ impl<'a> DCtx<'a> {
9481003
dst: &mut C,
9491004
src: &[u8],
9501005
) -> SafeResult {
1006+
self.2.clear();
9511007
unsafe {
9521008
dst.write_from(|buffer, capacity| {
9531009
parse_code(zstd_sys::ZSTD_decompressDCtx(
@@ -975,6 +1031,7 @@ impl<'a> DCtx<'a> {
9751031
src: &[u8],
9761032
dict: &[u8],
9771033
) -> SafeResult {
1034+
self.2.clear();
9781035
unsafe {
9791036
dst.write_from(|buffer, capacity| {
9801037
parse_code(zstd_sys::ZSTD_decompress_usingDict(
@@ -1001,6 +1058,7 @@ impl<'a> DCtx<'a> {
10011058
src: &[u8],
10021059
ddict: &DDict<'_>,
10031060
) -> SafeResult {
1061+
self.2.clear();
10041062
unsafe {
10051063
dst.write_from(|buffer, capacity| {
10061064
parse_code(zstd_sys::ZSTD_decompress_usingDDict(
@@ -1023,15 +1081,18 @@ impl<'a> DCtx<'a> {
10231081
///
10241082
/// Wraps the `ZSTD_initCStream()` function.
10251083
pub fn init(&mut self) -> SafeResult {
1084+
self.2.clear();
10261085
let code = unsafe { zstd_sys::ZSTD_initDStream(self.0.as_ptr()) };
1027-
parse_code(code)
1086+
self.2.record(parse_code(code))
10281087
}
10291088

10301089
/// Wraps the `ZSTD_initDStream_usingDict()` function.
10311090
#[cfg(feature = "experimental")]
10321091
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
10331092
#[deprecated]
10341093
pub fn init_using_dict(&mut self, dict: &[u8]) -> SafeResult {
1094+
self.2.clear();
1095+
self.2.clear();
10351096
let code = unsafe {
10361097
zstd_sys::ZSTD_initDStream_usingDict(
10371098
self.0.as_ptr(),
@@ -1065,9 +1126,14 @@ impl<'a> DCtx<'a> {
10651126
///
10661127
/// Wraps the `ZSTD_DCtx_reset()` function.
10671128
pub fn reset(&mut self, reset: ResetDirective) -> SafeResult {
1068-
parse_code(unsafe {
1129+
let resets_session = reset.resets_session();
1130+
let res = parse_code(unsafe {
10691131
zstd_sys::ZSTD_DCtx_reset(self.0.as_ptr(), reset.as_sys())
1070-
})
1132+
});
1133+
if res.is_ok() && resets_session {
1134+
self.2.clear();
1135+
}
1136+
res
10711137
}
10721138

10731139
/// Loads a dictionary.
@@ -1195,6 +1261,7 @@ impl<'a> DCtx<'a> {
11951261
output: &mut OutBuffer<'_, C>,
11961262
input: &mut InBuffer<'_>,
11971263
) -> SafeResult {
1264+
self.2.guard()?;
11981265
let mut output = output.wrap();
11991266
let mut input = input.wrap();
12001267
let code = unsafe {
@@ -1204,7 +1271,7 @@ impl<'a> DCtx<'a> {
12041271
ptr_mut(&mut input),
12051272
)
12061273
};
1207-
parse_code(code)
1274+
self.2.record(parse_code(code))
12081275
}
12091276

12101277
/// Wraps the `ZSTD_DStreamInSize()` function.
@@ -1284,7 +1351,7 @@ impl<'a> DCtx<'a> {
12841351

12851352
unsafe { zstd_sys::ZSTD_copyDCtx(context.as_ptr(), self.0.as_ptr()) };
12861353

1287-
Ok(DCtx(context, self.1))
1354+
Ok(DCtx(context, self.1, self.2.clone()))
12881355
}
12891356
}
12901357

@@ -2119,6 +2186,18 @@ pub enum ResetDirective {
21192186
}
21202187

21212188
impl ResetDirective {
2189+
/// Does this drop a session in progress?
2190+
///
2191+
/// Only a session reset brings a context back from the undefined state an
2192+
/// error can leave it in - `Parameters` alone is refused while a session is
2193+
/// open.
2194+
fn resets_session(&self) -> bool {
2195+
matches!(
2196+
self,
2197+
ResetDirective::SessionOnly | ResetDirective::SessionAndParameters
2198+
)
2199+
}
2200+
21222201
fn as_sys(self) -> zstd_sys::ZSTD_ResetDirective {
21232202
match self {
21242203
ResetDirective::SessionOnly => zstd_sys::ZSTD_ResetDirective::ZSTD_reset_session_only,

0 commit comments

Comments
 (0)