File tree 2 files changed +20
-3
lines changed
2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -138,16 +138,23 @@ func (e *encoder) Write(p []byte) (n int, err error) {
138
138
if len (p ) == 0 {
139
139
return 0 , nil
140
140
}
141
+ if maxLit := uint8 (1 << e .litWidth - 1 ); maxLit != 0xff {
142
+ for _ , x := range p {
143
+ if x > maxLit {
144
+ e .err = errors .New ("lzw: input byte too large for the litWidth" )
145
+ return 0 , e .err
146
+ }
147
+ }
148
+ }
141
149
n = len (p )
142
- litMask := uint32 (1 << e .litWidth - 1 )
143
150
code := e .savedCode
144
151
if code == invalidCode {
145
152
// The first code sent is always a literal code.
146
- code , p = uint32 (p [0 ])& litMask , p [1 :]
153
+ code , p = uint32 (p [0 ]), p [1 :]
147
154
}
148
155
loop:
149
156
for _ , x := range p {
150
- literal := uint32 (x ) & litMask
157
+ literal := uint32 (x )
151
158
key := code << 8 | literal
152
159
// If there is a hash table hit for this key then we continue the loop
153
160
// and do not emit a code yet.
Original file line number Diff line number Diff line change @@ -104,6 +104,16 @@ func TestWriterReturnValues(t *testing.T) {
104
104
}
105
105
}
106
106
107
+ func TestSmallLitWidth (t * testing.T ) {
108
+ w := NewWriter (ioutil .Discard , LSB , 2 )
109
+ if _ , err := w .Write ([]byte {0x03 }); err != nil {
110
+ t .Fatalf ("write a byte < 1<<2: %v" , err )
111
+ }
112
+ if _ , err := w .Write ([]byte {0x04 }); err == nil {
113
+ t .Fatal ("write a byte >= 1<<2: got nil error, want non-nil" )
114
+ }
115
+ }
116
+
107
117
func benchmarkEncoder (b * testing.B , n int ) {
108
118
b .StopTimer ()
109
119
b .SetBytes (int64 (n ))
You can’t perform that action at this time.
0 commit comments