Skip to content

Commit bd7a25c

Browse files
michielp1807folkertdev
authored andcommitted
ZSTD_decompressBlock_internal: use Result
1 parent 2e8d83c commit bd7a25c

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

lib/decompress/zstd_decompress.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,14 +1822,17 @@ unsafe fn ZSTD_decompressFrame(
18221822
}
18231823
BlockType::Compressed => {
18241824
debug_assert!(dctx.isFrameDecompression);
1825-
decodedSize = ZSTD_decompressBlock_internal(
1825+
decodedSize = match ZSTD_decompressBlock_internal(
18261826
dctx,
18271827
op.as_mut_ptr().cast(),
18281828
oBlockEnd.offset_from(op.as_mut_ptr()) as size_t,
18291829
ip.as_ptr().cast(),
18301830
cBlockSize,
18311831
not_streaming,
1832-
);
1832+
) {
1833+
Ok(size) => size,
1834+
Err(err) => return err.to_error_code(),
1835+
};
18331836
}
18341837
BlockType::Reserved => {
18351838
return Error::corruption_detected.to_error_code();
@@ -2180,15 +2183,18 @@ unsafe fn decompress_continue(dctx: &mut ZSTD_DCtx, mut dst: Writer<'_>, src: &[
21802183
match dctx.bType {
21812184
BlockType::Compressed => {
21822185
debug_assert!(dctx.isFrameDecompression);
2183-
rSize = ZSTD_decompressBlock_internal(
2186+
dctx.expected = 0; // streaming not supported
2187+
rSize = match ZSTD_decompressBlock_internal(
21842188
dctx,
21852189
dst.as_mut_ptr().cast(),
21862190
dst.capacity(),
21872191
src.as_ptr().cast(),
21882192
src.len(),
21892193
is_streaming,
2190-
);
2191-
dctx.expected = 0; // streaming not supported
2194+
) {
2195+
Ok(size) => size,
2196+
Err(err) => return err.to_error_code(),
2197+
};
21922198
}
21932199
BlockType::Raw => {
21942200
debug_assert!(src.len() <= dctx.expected);

lib/decompress/zstd_decompress_block.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,13 +2332,13 @@ pub unsafe fn ZSTD_decompressBlock_internal(
23322332
src: *const core::ffi::c_void,
23332333
srcSize: size_t,
23342334
streaming: streaming_operation,
2335-
) -> size_t {
2335+
) -> Result<size_t, Error> {
23362336
let Some(dctx) = dctx.as_mut() else {
2337-
return Error::GENERIC.to_error_code();
2337+
return Err(Error::GENERIC);
23382338
};
23392339

23402340
let Ok(streaming) = StreamingOperation::try_from(streaming) else {
2341-
return Error::GENERIC.to_error_code();
2341+
return Err(Error::GENERIC);
23422342
};
23432343

23442344
let src = if src.is_null() {
@@ -2358,15 +2358,12 @@ unsafe fn ZSTD_decompressBlock_internal_help(
23582358
mut dst: Writer<'_>,
23592359
src: &[u8],
23602360
streaming: StreamingOperation,
2361-
) -> size_t {
2361+
) -> Result<size_t, Error> {
23622362
if src.len() > dctx.block_size_max() {
2363-
return Error::srcSize_wrong.to_error_code();
2363+
return Err(Error::srcSize_wrong);
23642364
}
23652365

2366-
let litCSize = match ZSTD_decodeLiteralsBlock(dctx, src, dst.subslice(..), streaming) {
2367-
Ok(size) => size,
2368-
Err(err) => return err.to_error_code(),
2369-
};
2366+
let litCSize = ZSTD_decodeLiteralsBlock(dctx, src, dst.subslice(..), streaming)?;
23702367

23712368
let mut ip = &src[litCSize as usize..];
23722369

@@ -2380,19 +2377,16 @@ unsafe fn ZSTD_decompressBlock_internal_help(
23802377
};
23812378
let mut use_prefetch_decoder = dctx.ddictIsCold;
23822379
let mut nbSeq = 0;
2383-
let seqHSize = match ZSTD_decodeSeqHeaders(dctx, &mut nbSeq, ip) {
2384-
Ok(size) => size,
2385-
Err(err) => return err.to_error_code(),
2386-
};
2380+
let seqHSize = ZSTD_decodeSeqHeaders(dctx, &mut nbSeq, ip)?;
23872381
ip = &ip[seqHSize as usize..];
23882382
if dst.is_empty() && nbSeq > 0 {
2389-
return Error::dstSize_tooSmall.to_error_code();
2383+
return Err(Error::dstSize_tooSmall);
23902384
}
23912385
if MEM_64bits()
23922386
&& ::core::mem::size_of::<size_t>() == ::core::mem::size_of::<*mut core::ffi::c_void>()
23932387
&& (usize::MAX - dst.as_mut_ptr() as usize) < (1 << 20)
23942388
{
2395-
return Error::dstSize_tooSmall.to_error_code();
2389+
return Err(Error::dstSize_tooSmall);
23962390
}
23972391
if offset == Offset::Long
23982392
|| !use_prefetch_decoder && totalHistorySize > ((1) << 24) as size_t && nbSeq > 8
@@ -2415,22 +2409,11 @@ unsafe fn ZSTD_decompressBlock_internal_help(
24152409
dctx.ddictIsCold = false;
24162410

24172411
if use_prefetch_decoder {
2418-
return match ZSTD_decompressSequencesLong(dctx, dst.subslice(..), ip, nbSeq, offset) {
2419-
Ok(size) => size,
2420-
Err(err) => err.to_error_code(),
2421-
};
2422-
}
2423-
2424-
if dctx.litBufferLocation == LitLocation::ZSTD_split {
2425-
match ZSTD_decompressSequencesSplitLitBuffer(dctx, dst, ip, nbSeq, offset) {
2426-
Ok(size) => size,
2427-
Err(err) => err.to_error_code(),
2428-
}
2412+
ZSTD_decompressSequencesLong(dctx, dst.subslice(..), ip, nbSeq, offset)
2413+
} else if dctx.litBufferLocation == LitLocation::ZSTD_split {
2414+
ZSTD_decompressSequencesSplitLitBuffer(dctx, dst, ip, nbSeq, offset)
24292415
} else {
2430-
match ZSTD_decompressSequences(dctx, dst, ip, nbSeq, offset) {
2431-
Ok(size) => size,
2432-
Err(err) => err.to_error_code(),
2433-
}
2416+
ZSTD_decompressSequences(dctx, dst, ip, nbSeq, offset)
24342417
}
24352418
}
24362419

@@ -2453,16 +2436,15 @@ unsafe fn ZSTD_decompressBlock_deprecated(
24532436
ZSTD_checkContinuity(dctx, dst.as_ptr_range());
24542437

24552438
// FIXME: can src and dst overlap in this case?
2456-
let dSize = ZSTD_decompressBlock_internal_help(
2439+
let dSize = match ZSTD_decompressBlock_internal_help(
24572440
dctx,
24582441
dst.subslice(..),
24592442
src.as_slice(),
24602443
StreamingOperation::NotStreaming,
2461-
);
2462-
2463-
if ERR_isError(dSize) {
2464-
return dSize;
2465-
}
2444+
) {
2445+
Ok(size) => size,
2446+
Err(err) => return err.to_error_code(),
2447+
};
24662448

24672449
dctx.previousDstEnd = dst.as_ptr().byte_add(dSize).cast::<c_void>();
24682450
dSize

0 commit comments

Comments
 (0)