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 NewBitfield(size int) (Bitfield, error) {
	if size < 0 {
		return nil, fmt.Errorf("bitfield size must be positive; got %d", size)
	}
	if size%8 != 0 {
		return nil, fmt.Errorf("bitfield size must be a multiple of 8; got %d", size)
	}
	return make([]byte, size/8), nil
}