adpcm: length check before initialization

This commit is contained in:
Trek H 2019-05-02 00:02:33 +09:30
parent d06388cfe9
commit ef9a38cb76
1 changed files with 6 additions and 1 deletions

View File

@ -259,8 +259,13 @@ func (e *encoder) init(samps []byte) {
// It writes its output to the encoder's dest. // It writes its output to the encoder's dest.
// The number of bytes written out is returned along with any error that occured. // The number of bytes written out is returned along with any error that occured.
func (e *encoder) Write(inPcm []byte) (int, error) { func (e *encoder) Write(inPcm []byte) (int, error) {
// Determine if there will be a byte that won't contain two full nibbles and will need padding. // Check that pcm has enough data to initialize decoder
pcmLen := len(inPcm) pcmLen := len(inPcm)
if pcmLen < 4 {
return 0, fmt.Errorf("length of given byte array must be greater than 4")
}
// Determine if there will be a byte that won't contain two full nibbles and will need padding.
pad := false pad := false
if (pcmLen-2)%4 != 0 { if (pcmLen-2)%4 != 0 {
pad = true pad = true