diff --git a/bson/primitive/objectid.go b/bson/primitive/objectid.go index 30aaafe6d9..a5a7d7c698 100644 --- a/bson/primitive/objectid.go +++ b/bson/primitive/objectid.go @@ -73,15 +73,15 @@ func (id ObjectID) IsZero() bool { // ObjectIDFromHex creates a new ObjectID from a hex string. It returns an error if the hex string is not a // valid ObjectID. func ObjectIDFromHex(s string) (ObjectID, error) { + if len(s) != 24 { + return NilObjectID, ErrInvalidHex + } + b, err := hex.DecodeString(s) if err != nil { return NilObjectID, err } - if len(b) != 12 { - return NilObjectID, ErrInvalidHex - } - var oid [12]byte copy(oid[:], b[:])