Avoid the risk of leaking small amounts of memory.

Removing from the bottom of a stack (nipN(depth)) would leak the first
entry in the array by slicing [1:], leaving array[0] dangling an
inaccessible (but unable to be freed until the whole slice is gone). We
left it like this for a while, but best not to leak the memory. Happens
rarely so the performance hit shouldn't matter that much. Do the same
thing for condstack.
This commit is contained in:
Owain G. Ainsworth 2013-09-26 01:53:11 +01:00
parent a63edcd2dc
commit 0a3e7f682b
2 changed files with 6 additions and 2 deletions

View file

@ -840,7 +840,9 @@ func opcodeEndif(op *parsedOpcode, s *Script) error {
return StackErrNoIf return StackErrNoIf
} }
s.condStack = s.condStack[1:] stk := make([]int, len(s.condStack) -1, len(s.condStack) -1)
copy(stk, s.condStack[1:])
s.condStack = stk
return nil return nil
} }

View file

@ -173,7 +173,9 @@ func (s *Stack) nipN(idx int) (so []byte, err error) {
if idx == 0 { if idx == 0 {
s.stk = s.stk[:sz-1] s.stk = s.stk[:sz-1]
} else if idx == sz-1 { } else if idx == sz-1 {
s.stk = s.stk[1:] s1 := make([][]byte, sz - 1, sz - 1)
copy(s1, s.stk[1:])
s.stk = s1
} else { } else {
s1 := s.stk[sz-idx : sz] s1 := s.stk[sz-idx : sz]
s.stk = s.stk[:sz-idx-1] s.stk = s.stk[:sz-idx-1]