Skip to content

Commit 50a9b70

Browse files
committed
Detect and report invalid stream earlier.
PiperOrigin-RevId: 876198329 (cherry picked from commit 0ed4268
1 parent e61745a commit 50a9b70

2 files changed

Lines changed: 59 additions & 31 deletions

File tree

β€Žc/dec/decode.cβ€Ž

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
11171117

11181118
/* Decodes a command or literal and updates block type ring-buffer.
11191119
Reads 3..54 bits. */
1120-
static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1120+
static BROTLI_INLINE BrotliDecoderErrorCode DecodeBlockTypeAndLength(
11211121
int safe, BrotliDecoderState* s, int tree_type) {
11221122
uint32_t max_block_type = s->num_block_types[tree_type];
11231123
const HuffmanCode* type_tree = &s->block_type_trees[
@@ -1128,7 +1128,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
11281128
uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2];
11291129
uint32_t block_type;
11301130
if (max_block_type <= 1) {
1131-
return BROTLI_FALSE;
1131+
return BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH;
11321132
}
11331133

11341134
/* Read 0..15 + 3..39 bits. */
@@ -1138,11 +1138,13 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
11381138
} else {
11391139
BrotliBitReaderState memento;
11401140
BrotliBitReaderSaveState(br, &memento);
1141-
if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE;
1141+
if (!SafeReadSymbol(type_tree, br, &block_type)) {
1142+
return BROTLI_DECODER_NEEDS_MORE_INPUT;
1143+
}
11421144
if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) {
11431145
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
11441146
BrotliBitReaderRestoreState(br, &memento);
1145-
return BROTLI_FALSE;
1147+
return BROTLI_DECODER_NEEDS_MORE_INPUT;
11461148
}
11471149
}
11481150

@@ -1158,7 +1160,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
11581160
}
11591161
ringbuffer[0] = ringbuffer[1];
11601162
ringbuffer[1] = block_type;
1161-
return BROTLI_TRUE;
1163+
return BROTLI_DECODER_SUCCESS;
11621164
}
11631165

11641166
static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(
@@ -1194,62 +1196,68 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
11941196

11951197
/* Decodes the block type and updates the state for literal context.
11961198
Reads 3..54 bits. */
1197-
static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal(
1199+
static BROTLI_INLINE BrotliDecoderErrorCode DecodeLiteralBlockSwitchInternal(
11981200
int safe, BrotliDecoderState* s) {
1199-
if (!DecodeBlockTypeAndLength(safe, s, 0)) {
1200-
return BROTLI_FALSE;
1201+
BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 0);
1202+
if (result != BROTLI_DECODER_SUCCESS) {
1203+
return result;
12011204
}
12021205
PrepareLiteralDecoding(s);
1203-
return BROTLI_TRUE;
1206+
return BROTLI_DECODER_SUCCESS;
12041207
}
12051208

1206-
static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1207-
DecodeLiteralBlockSwitchInternal(0, s);
1209+
static BROTLI_NOINLINE BrotliDecoderErrorCode
1210+
DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1211+
return DecodeLiteralBlockSwitchInternal(0, s);
12081212
}
12091213

1210-
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(
1214+
static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeLiteralBlockSwitch(
12111215
BrotliDecoderState* s) {
12121216
return DecodeLiteralBlockSwitchInternal(1, s);
12131217
}
12141218

12151219
/* Block switch for insert/copy length.
12161220
Reads 3..54 bits. */
1217-
static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
1221+
static BROTLI_INLINE BrotliDecoderErrorCode DecodeCommandBlockSwitchInternal(
12181222
int safe, BrotliDecoderState* s) {
1219-
if (!DecodeBlockTypeAndLength(safe, s, 1)) {
1220-
return BROTLI_FALSE;
1223+
BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 1);
1224+
if (result != BROTLI_DECODER_SUCCESS) {
1225+
return result;
12211226
}
12221227
s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
1223-
return BROTLI_TRUE;
1228+
return BROTLI_DECODER_SUCCESS;
12241229
}
12251230

1226-
static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1227-
DecodeCommandBlockSwitchInternal(0, s);
1231+
static BROTLI_NOINLINE BrotliDecoderErrorCode
1232+
DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1233+
return DecodeCommandBlockSwitchInternal(0, s);
12281234
}
12291235

1230-
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
1236+
static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeCommandBlockSwitch(
12311237
BrotliDecoderState* s) {
12321238
return DecodeCommandBlockSwitchInternal(1, s);
12331239
}
12341240

12351241
/* Block switch for distance codes.
12361242
Reads 3..54 bits. */
1237-
static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal(
1243+
static BROTLI_INLINE BrotliDecoderErrorCode DecodeDistanceBlockSwitchInternal(
12381244
int safe, BrotliDecoderState* s) {
1239-
if (!DecodeBlockTypeAndLength(safe, s, 2)) {
1240-
return BROTLI_FALSE;
1245+
BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 2);
1246+
if (result != BROTLI_DECODER_SUCCESS) {
1247+
return result;
12411248
}
12421249
s->dist_context_map_slice = s->dist_context_map +
12431250
(s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS);
12441251
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
1245-
return BROTLI_TRUE;
1252+
return BROTLI_DECODER_SUCCESS;
12461253
}
12471254

1248-
static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1249-
DecodeDistanceBlockSwitchInternal(0, s);
1255+
static BROTLI_NOINLINE BrotliDecoderErrorCode
1256+
DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1257+
return DecodeDistanceBlockSwitchInternal(0, s);
12501258
}
12511259

1252-
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(
1260+
static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeDistanceBlockSwitch(
12531261
BrotliDecoderState* s) {
12541262
return DecodeDistanceBlockSwitchInternal(1, s);
12551263
}
@@ -1719,6 +1727,9 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
17191727
return BrotliCheckInputAmount(br, num);
17201728
}
17211729

1730+
/* NB: METHOD should return BROTLI_FALSE only in case there is not enough input;
1731+
in case of "unsafe" execution, when input is guaranteed to be sufficient,
1732+
result is ignored. */
17221733
#define BROTLI_SAFE(METHOD) \
17231734
{ \
17241735
if (safe) { \
@@ -1731,6 +1742,22 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
17311742
} \
17321743
}
17331744

1745+
/* NB: METHOD should return BROTLI_DECODER_SUCCESS, BROTLI_DECODER_ERROR_*, or
1746+
BROTLI_DECODER_NEEDS_MORE_INPUT; the later two break the processing. */
1747+
#define BROTLI_SAFE_WITH_STATUS(METHOD) \
1748+
{ \
1749+
BrotliDecoderErrorCode status; \
1750+
if (safe) { \
1751+
status = Safe##METHOD; \
1752+
} else { \
1753+
status = METHOD; \
1754+
} \
1755+
if (status != BROTLI_DECODER_SUCCESS) { \
1756+
result = status; \
1757+
goto saveStateAndReturn; \
1758+
} \
1759+
}
1760+
17341761
static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
17351762
int safe, BrotliDecoderState* s) {
17361763
int pos = s->pos;
@@ -1769,7 +1796,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
17691796
goto saveStateAndReturn;
17701797
}
17711798
if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) {
1772-
BROTLI_SAFE(DecodeCommandBlockSwitch(s));
1799+
BROTLI_SAFE_WITH_STATUS(DecodeCommandBlockSwitch(s));
17731800
goto CommandBegin;
17741801
}
17751802
/* Read the insert/copy length in the command. */
@@ -1797,7 +1824,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
17971824
goto saveStateAndReturn;
17981825
}
17991826
if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
1800-
BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
1827+
BROTLI_SAFE_WITH_STATUS(DecodeLiteralBlockSwitch(s));
18011828
PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
18021829
if (!s->trivial_literal_context) goto CommandInner;
18031830
}
@@ -1833,7 +1860,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
18331860
goto saveStateAndReturn;
18341861
}
18351862
if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
1836-
BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
1863+
BROTLI_SAFE_WITH_STATUS(DecodeLiteralBlockSwitch(s));
18371864
if (s->trivial_literal_context) goto CommandInner;
18381865
}
18391866
context = BROTLI_CONTEXT(p1, p2, s->context_lookup);
@@ -1880,7 +1907,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
18801907
} else {
18811908
/* Read distance code in the command, unless it was implicitly zero. */
18821909
if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) {
1883-
BROTLI_SAFE(DecodeDistanceBlockSwitch(s));
1910+
BROTLI_SAFE_WITH_STATUS(DecodeDistanceBlockSwitch(s));
18841911
}
18851912
BROTLI_SAFE(ReadDistance(s, br));
18861913
}

β€Žc/include/brotli/decode.hβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ typedef enum {
8484
BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \
8585
BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \
8686
BROTLI_ERROR_CODE(_ERROR_FORMAT_, DISTANCE, -16) SEPARATOR \
87+
BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_SWITCH, -17) SEPARATOR \
8788
\
88-
/* -17..-18 codes are reserved */ \
89+
/* Code -18 is reserved */ \
8990
\
9091
BROTLI_ERROR_CODE(_ERROR_, DICTIONARY_NOT_SET, -19) SEPARATOR \
9192
BROTLI_ERROR_CODE(_ERROR_, INVALID_ARGUMENTS, -20) SEPARATOR \

0 commit comments

Comments
Β (0)