seiyab / gost

static code check for Golang
MIT License
1 stars 0 forks source link

[sliceInitialLength] false positive: should allow truncation in append #27

Open seiyab opened 7 months ago

seiyab commented 7 months ago

Some programmer truncates slice on append. Following code is reported though this doesn't cause unintended preceding zero values.

https://github.com/ethereum/go-ethereum/blob/3c75c64e6bbf64f842c6f725a595713262c2f8fe/accounts/usbwallet/ledger.go#L501-L507

    chunk := make([]byte, 64)
    space := len(chunk) - len(header)

    for i := 0; len(apdu) > 0; i++ {
        // Construct the new message to stream
        chunk = append(chunk[:0], header...)
        binary.BigEndian.PutUint16(chunk[3:], uint16(i))
seiyab commented 7 months ago

https://github.com/ethereum/go-ethereum/blob/34aac1d7562bf141fe6da1d4f3cdea8819e7b23b/internal/cmdtest/test_cmd.go#L127-L135

func (tt *TestCmd) matchExactOutput(want []byte) error {
    buf := make([]byte, len(want))
    n := 0
    tt.withKillTimeout(func() { n, _ = io.ReadFull(tt.stdout, buf) })
    buf = buf[:n]
    if n < len(want) || !bytes.Equal(buf, want) {
        // Grab any additional buffered output in case of mismatch
        // because it might help with debugging.
        buf = append(buf, make([]byte, tt.stdout.Buffered())...)