Language: Go
Label: safe
CWE: CWE-1284 \u2014 Improper Validation of Specified Quantity in Input
The product receives input that is expected to specify a quantity (such as size or length), but it does not validate or incorrectly validates that the quantity has the required properties.

Code:
func FromBytes(size int, bits []byte) (Bitfield, error) {
	bf, err := NewBitfield(size)
	if err != nil {
		return nil, err
	}
	start := len(bf) - len(bits)
	if start < 0 {
		return nil, fmt.Errorf("bitfield too small: got %d; need %d", size, len(bits)*8)
	}
	copy(bf[start:], bits)
	return bf, nil
}