From 92959fb81f819cc6f63a9cc13240922fa4117c04 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 16 Feb 2019 15:40:02 +1100 Subject: [PATCH] Check that extra-field types aren't refined --- lang/parse/parse.go | 16 +++++++++- release/c/wuffs-unsupported-snapshot.c | 44 ++++++++++++++++---------- std/deflate/decode_deflate.wuffs | 22 ++++++------- std/deflate/decode_huffman_fast.wuffs | 4 +-- std/deflate/decode_huffman_slow.wuffs | 4 +-- std/lzw/decode_lzw.wuffs | 10 +++--- 6 files changed, 63 insertions(+), 37 deletions(-) diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 0a30c24b..7c2e1b88 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -342,7 +342,21 @@ func (p *parser) parseFieldNode() (*a.Node, error) { } func (p *parser) parseExtraFieldNode() (*a.Node, error) { - return p.parseFieldNode1(a.FlagsPrivateData) + n, err := p.parseFieldNode1(a.FlagsPrivateData) + if err != nil { + return nil, err + } + typ := n.AsField().XType() + for typ.Decorator() == t.IDArray { + typ = typ.Inner() + } + if (typ.Decorator() != 0) || + (typ.QID()[0] == t.IDBase) && (!typ.IsNumType() || typ.IsRefined()) { + + return nil, fmt.Errorf(`parse: invalid extra-field type %q at %s:%d`, + n.AsField().XType().Str(p.tm), p.filename, p.line()) + } + return n, nil } func (p *parser) parseFieldNode1(flags a.Flags) (*a.Node, error) { diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 799fe09a..a4ebb283 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -6930,7 +6930,9 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, status = v_status; goto exit; } - v_mask = ((((uint32_t)(1)) << self->private_data.f_n_huffs_bits[0]) - 1); + v_mask = ((((uint32_t)(1)) << wuffs_base__u32__min( + self->private_data.f_n_huffs_bits[0], 9)) - + 1); v_i = 0; label_0_continue:; while (v_i < (v_n_lit + v_n_dist)) { @@ -6975,7 +6977,7 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, status = wuffs_deflate__error__bad_huffman_code_length_repetition; goto exit; } - v_rep_symbol = self->private_data.f_code_lengths[(v_i - 1)]; + v_rep_symbol = (self->private_data.f_code_lengths[(v_i - 1)] & 15); v_rep_count = 3; } else if (v_table_entry == 17) { v_n_extra_bits = 3; @@ -7104,10 +7106,10 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, v_i = a_n_codes0; while (v_i < a_n_codes1) { - if (v_counts[self->private_data.f_code_lengths[v_i]] >= 320) { + if (v_counts[(self->private_data.f_code_lengths[v_i] & 15)] >= 320) { return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } - v_counts[self->private_data.f_code_lengths[v_i]] += 1; + v_counts[(self->private_data.f_code_lengths[v_i] & 15)] += 1; v_i += 1; } if ((((uint32_t)(v_counts[0])) + a_n_codes0) == a_n_codes1) { @@ -7148,12 +7150,12 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } if (self->private_data.f_code_lengths[v_i] != 0) { - if (v_offsets[self->private_data.f_code_lengths[v_i]] >= 320) { + if (v_offsets[(self->private_data.f_code_lengths[v_i] & 15)] >= 320) { return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } - v_symbols[v_offsets[self->private_data.f_code_lengths[v_i]]] = + v_symbols[v_offsets[(self->private_data.f_code_lengths[v_i] & 15)]] = ((uint16_t)((v_i - a_n_codes0))); - v_offsets[self->private_data.f_code_lengths[v_i]] += 1; + v_offsets[(self->private_data.f_code_lengths[v_i] & 15)] += 1; } v_i += 1; } @@ -7196,8 +7198,9 @@ label_1_break:; if (v_max_cl < 9) { v_initial_high_bits = (((uint32_t)(1)) << v_max_cl); } - v_prev_cl = ((uint32_t)(self->private_data.f_code_lengths[( - a_n_codes0 + ((uint32_t)(v_symbols[0])))])); + v_prev_cl = ((uint32_t)((self->private_data.f_code_lengths[( + a_n_codes0 + ((uint32_t)(v_symbols[0])))] & + 15))); v_prev_redirect_key = 4294967295; v_top = 0; v_next_top = 512; @@ -7208,8 +7211,9 @@ label_1_break:; if ((a_n_codes0 + ((uint32_t)(v_symbols[v_i]))) >= 320) { return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } - v_cl = ((uint32_t)(self->private_data.f_code_lengths[( - a_n_codes0 + ((uint32_t)(v_symbols[v_i])))])); + v_cl = ((uint32_t)((self->private_data.f_code_lengths[( + a_n_codes0 + ((uint32_t)(v_symbols[v_i])))] & + 15))); if (v_cl > v_prev_cl) { v_code <<= (v_cl - v_prev_cl); if (v_code >= 32768) { @@ -7362,8 +7366,12 @@ wuffs_deflate__decoder__decode_huffman_fast(wuffs_deflate__decoder* self, } v_bits = self->private_impl.f_bits; v_n_bits = self->private_impl.f_n_bits; - v_lmask = ((((uint32_t)(1)) << self->private_data.f_n_huffs_bits[0]) - 1); - v_dmask = ((((uint32_t)(1)) << self->private_data.f_n_huffs_bits[1]) - 1); + v_lmask = ((((uint32_t)(1)) << wuffs_base__u32__min( + self->private_data.f_n_huffs_bits[0], 9)) - + 1); + v_dmask = ((((uint32_t)(1)) << wuffs_base__u32__min( + self->private_data.f_n_huffs_bits[1], 9)) - + 1); label_0_continue:; while ((((uint64_t)(io1_a_dst - iop_a_dst)) >= 258) && (((uint64_t)(io1_a_src - iop_a_src)) >= 12)) { @@ -7699,8 +7707,12 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, } v_bits = self->private_impl.f_bits; v_n_bits = self->private_impl.f_n_bits; - v_lmask = ((((uint32_t)(1)) << self->private_data.f_n_huffs_bits[0]) - 1); - v_dmask = ((((uint32_t)(1)) << self->private_data.f_n_huffs_bits[1]) - 1); + v_lmask = ((((uint32_t)(1)) << wuffs_base__u32__min( + self->private_data.f_n_huffs_bits[0], 9)) - + 1); + v_dmask = ((((uint32_t)(1)) << wuffs_base__u32__min( + self->private_data.f_n_huffs_bits[1], 9)) - + 1); label_0_continue:; while (!(self->private_impl.p_decode_huffman_slow[0] != 0)) { while (true) { @@ -8370,7 +8382,7 @@ wuffs_lzw__decoder__read_from(wuffs_lzw__decoder* self, } v_steps -= 1; v_o = ((v_o - 8) & 8191); - v_c = ((uint32_t)(self->private_data.f_prefixes[v_c])); + v_c = (((uint32_t)(self->private_data.f_prefixes[v_c])) & 4095); } label_1_break:; v_first_byte = self->private_data.f_suffixes[v_c][0]; diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index ef3c19c9..782bafc0 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -107,7 +107,7 @@ pub struct decoder?( // // Exactly one of the eight bits 31-24 should be set. huffs array[2] array[huffs_table_size] base.u32, - n_huffs_bits array[2] base.u32[..9], + n_huffs_bits array[2] base.u32, // history holds up to the last 32KiB of decoded output, if the decoding // was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE) @@ -126,7 +126,7 @@ pub struct decoder?( // // code_lengths[args.n_codes0 + i] holds the number of bits in the i'th // code. - code_lengths array[320] base.u8[..15], + code_lengths array[320] base.u8, ) pub func decoder.workbuf_len() base.range_ii_u64 { @@ -389,7 +389,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) { } // Decode the code lengths for the next two Huffman tables. - mask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1 + mask = ((1 as base.u32) << (this.n_huffs_bits[0].min(x:9))) - 1 i = 0 while i < (n_lit + n_dist) { assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0:n_lit, c0:n_dist) @@ -433,7 +433,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) { if i <= 0 { return "?bad Huffman code length repetition" } - rep_symbol = this.code_lengths[i - 1] + rep_symbol = this.code_lengths[i - 1] & 15 rep_count = 3 assert rep_count <= 11 } else if table_entry == 17 { @@ -561,10 +561,10 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod assert i < 320 via "a < b: a < c; c <= b"(c:args.n_codes1) // TODO: this if should be unnecessary. Have some way to assert that, // for all j, counts[j] <= i, and thus counts[j]++ will not overflow. - if counts[this.code_lengths[i]] >= 320 { + if counts[this.code_lengths[i] & 15] >= 320 { return "?internal error: inconsistent Huffman decoder state" } - counts[this.code_lengths[i]] += 1 + counts[this.code_lengths[i] & 15] += 1 i += 1 } if ((counts[0] as base.u32) + args.n_codes0) == args.n_codes1 { @@ -648,11 +648,11 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod return "?internal error: inconsistent Huffman decoder state" } if this.code_lengths[i] != 0 { - if offsets[this.code_lengths[i]] >= 320 { + if offsets[this.code_lengths[i] & 15] >= 320 { return "?internal error: inconsistent Huffman decoder state" } - symbols[offsets[this.code_lengths[i]]] = (i - args.n_codes0) as base.u16 - offsets[this.code_lengths[i]] += 1 + symbols[offsets[this.code_lengths[i] & 15]] = (i - args.n_codes0) as base.u16 + offsets[this.code_lengths[i] & 15] += 1 } i += 1 } @@ -718,7 +718,7 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod if max_cl < 9 { initial_high_bits = (1 as base.u32) << max_cl } - prev_cl = this.code_lengths[args.n_codes0 + (symbols[0] as base.u32)] as base.u32 + prev_cl = (this.code_lengths[args.n_codes0 + (symbols[0] as base.u32)] & 15) as base.u32 prev_redirect_key = 0xFFFFFFFF top = 0 next_top = 512 @@ -733,7 +733,7 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod if (args.n_codes0 + (symbols[i] as base.u32)) >= 320 { return "?internal error: inconsistent Huffman decoder state" } - cl = this.code_lengths[args.n_codes0 + (symbols[i] as base.u32)] as base.u32 + cl = (this.code_lengths[args.n_codes0 + (symbols[i] as base.u32)] & 15) as base.u32 if cl > prev_cl { code <<= cl - prev_cl if code >= (1 << 15) { diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index e0428d8b..55cd3401 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -41,8 +41,8 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba bits = this.bits n_bits = this.n_bits - lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1 - dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1 + lmask = ((1 as base.u32) << this.n_huffs_bits[0].min(x:9)) - 1 + dmask = ((1 as base.u32) << this.n_huffs_bits[1].min(x:9)) - 1 // Check up front, on each iteration, that we have enough buffer space to // both read (12 bytes) and write (258 bytes) as much as we need to. Doing diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index a18a98e1..4d94fdb3 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -45,8 +45,8 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) { bits = this.bits n_bits = this.n_bits - lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1 - dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1 + lmask = ((1 as base.u32) << this.n_huffs_bits[0].min(x:9)) - 1 + dmask = ((1 as base.u32) << this.n_huffs_bits[1].min(x:9)) - 1 while:loop not coroutine_resumed { // Decode an lcode symbol from H-L. diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 76316c9a..04469c3a 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -57,10 +57,10 @@ pub struct decoder?( )( // read_from per-code state. suffixes array[4096] array[8] base.u8, - prefixes array[4096] base.u16[..4095], + prefixes array[4096] base.u16, // lm1s is the "length minus 1"s of the values for the implicit key-value // table in this decoder. See std/lzw/README.md for more detail. - lm1s array[4096] base.u16[..4095], + lm1s array[4096] base.u16, // output[output_ri:output_wi] is the buffered output, connecting read_from // with write_to and flush. @@ -199,7 +199,7 @@ pri func decoder.read_from!(src base.io_reader) { this.output[output_wi] = code as base.u8 output_wi = (output_wi + 1) & 8191 if save_code <= 4095 { - lm1_a = (this.lm1s[prev_code] + 1) & 4095 + lm1_a = (this.lm1s[prev_code] ~mod+ 1) & 4095 this.lm1s[save_code] = lm1_a if (lm1_a % 8) != 0 { @@ -262,7 +262,7 @@ pri func decoder.read_from!(src base.io_reader) { // This line is essentially "o -= 8". The "& 8191" is a no-op // in practice, but is necessary for the overflow checker. o = (o ~mod- 8) & 8191 - c = this.prefixes[c] as base.u32 + c = (this.prefixes[c] as base.u32) & 4095 } first_byte = this.suffixes[c][0] @@ -272,7 +272,7 @@ pri func decoder.read_from!(src base.io_reader) { } if save_code <= 4095 { - lm1_b = (this.lm1s[prev_code] + 1) & 4095 + lm1_b = (this.lm1s[prev_code] ~mod+ 1) & 4095 this.lm1s[save_code] = lm1_b if (lm1_b % 8) != 0 {