parser: use nil to indicate zero-count vectors

This commit is contained in:
George Tankersley
2018-09-20 21:54:25 +00:00
parent dfb05c261d
commit 2d9cf24f74

View File

@@ -276,14 +276,16 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
// TODO: vector. At the moment we're assuming trusted input. // TODO: vector. At the moment we're assuming trusted input.
// See https://nvd.nist.gov/vuln/detail/CVE-2018-17144 for an example. // See https://nvd.nist.gov/vuln/detail/CVE-2018-17144 for an example.
tx.transparentInputs = make([]*txIn, txInCount) if txInCount > 0 {
for i := 0; i < txInCount; i++ { tx.transparentInputs = make([]*txIn, txInCount)
ti := &txIn{} for i := 0; i < txInCount; i++ {
s, err = ti.ParseFromSlice([]byte(s)) ti := &txIn{}
if err != nil { s, err = ti.ParseFromSlice([]byte(s))
return nil, errors.Wrap(err, "while parsing transparent input") if err != nil {
return nil, errors.Wrap(err, "while parsing transparent input")
}
tx.transparentInputs[i] = ti
} }
tx.transparentInputs[i] = ti
} }
var txOutCount uint64 var txOutCount uint64
@@ -291,14 +293,16 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
return nil, errors.New("could not read tx_out_count") return nil, errors.New("could not read tx_out_count")
} }
tx.transparentOutputs = make([]*txOut, txOutCount) if txOutCount > 0 {
for i := 0; i < txOutCount; i++ { tx.transparentOutputs = make([]*txOut, txOutCount)
to := &txOut{} for i := 0; i < txOutCount; i++ {
s, err = to.ParseFromSlice([]byte(s)) to := &txOut{}
if err != nil { s, err = to.ParseFromSlice([]byte(s))
return nil, errors.Wrap(err, "while parsing transparent output") if err != nil {
return nil, errors.Wrap(err, "while parsing transparent output")
}
tx.transparentOutputs[i] = to
} }
tx.transparentOutputs[i] = to
} }
if ok := s.ReadUint32(&tx.nLockTime); !ok { if ok := s.ReadUint32(&tx.nLockTime); !ok {
@@ -321,14 +325,16 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
return nil, errors.New("could not read nShieldedSpend") return nil, errors.New("could not read nShieldedSpend")
} }
tx.shieldedSpends = make([]*spend, spendCount) if spendCount > 0 {
for i := 0; i < spendCount; i++ { tx.shieldedSpends = make([]*spend, spendCount)
newSpend := &spend{} for i := 0; i < spendCount; i++ {
s, err = newSpend.ParseFromSlice([]byte(s)) newSpend := &spend{}
if err != nil { s, err = newSpend.ParseFromSlice([]byte(s))
return nil, errors.Wrap(err, "while parsing shielded Spend") if err != nil {
return nil, errors.Wrap(err, "while parsing shielded Spend")
}
tx.shieldedSpends[i] = newSpend
} }
tx.shieldedSpends[i] = newSpend
} }
var outputCount uint64 var outputCount uint64
@@ -336,14 +342,16 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
return nil, errors.New("could not read nShieldedOutput") return nil, errors.New("could not read nShieldedOutput")
} }
tx.shieldedOutputs = make([]*output, outputCount) if outputCount > 0 {
for i := 0; i < outputCount; i++ { tx.shieldedOutputs = make([]*output, outputCount)
newOutput := &output{} for i := 0; i < outputCount; i++ {
s, err = newOutput.ParseFromSlice([]byte(s)) newOutput := &output{}
if err != nil { s, err = newOutput.ParseFromSlice([]byte(s))
return nil, errors.Wrap(err, "while parsing shielded Output") if err != nil {
return nil, errors.Wrap(err, "while parsing shielded Output")
}
tx.shieldedOutputs[i] = newOutput
} }
tx.shieldedOutputs[i] = newOutput
} }
} }