@@ -2,7 +2,6 @@ package asm
22
33import (
44 "io"
5- "io/ioutil"
65
76 "github.com/vbatts/tar-split/archive/tar"
87 "github.com/vbatts/tar-split/tar/storage"
@@ -119,20 +118,34 @@ func NewInputTarStream(r io.Reader, p storage.Packer, fp storage.FilePutter) (io
119118 }
120119 }
121120
122- // it is allowable, and not uncommon that there is further padding on the
123- // end of an archive, apart from the expected 1024 null bytes.
124- remainder , err := ioutil .ReadAll (outputRdr )
125- if err != nil && err != io .EOF {
126- pW .CloseWithError (err )
127- return
128- }
129- _ , err = p .AddEntry (storage.Entry {
130- Type : storage .SegmentType ,
131- Payload : remainder ,
132- })
133- if err != nil {
134- pW .CloseWithError (err )
135- return
121+ // It is allowable, and not uncommon that there is further padding on
122+ // the end of an archive, apart from the expected 1024 null bytes. We
123+ // do this in chunks rather than in one go to avoid cases where a
124+ // maliciously crafted tar file tries to trick us into reading many GBs
125+ // into memory.
126+ const paddingChunkSize = 1024 * 1024
127+ var paddingChunk [paddingChunkSize ]byte
128+ for {
129+ var isEOF bool
130+ n , err := outputRdr .Read (paddingChunk [:])
131+ if err != nil {
132+ if err != io .EOF {
133+ pW .CloseWithError (err )
134+ return
135+ }
136+ isEOF = true
137+ }
138+ _ , err = p .AddEntry (storage.Entry {
139+ Type : storage .SegmentType ,
140+ Payload : paddingChunk [:n ],
141+ })
142+ if err != nil {
143+ pW .CloseWithError (err )
144+ return
145+ }
146+ if isEOF {
147+ break
148+ }
136149 }
137150 pW .Close ()
138151 }()
0 commit comments