diff --git a/jwt/openid/birthdate.go b/jwt/openid/birthdate.go index 998017fb8..944916aac 100644 --- a/jwt/openid/birthdate.go +++ b/jwt/openid/birthdate.go @@ -58,47 +58,21 @@ func (b *BirthdateClaim) UnmarshalJSON(data []byte) error { return nil } -func parseBirthdayInt(s string) int { - // This function does weird things to bypass CodeQL checks. - - // 64 is hardcoded, b/c ... well, if we're on a 64-bit system, - // it should just work. On < 64 bit systems, Go does things correctly - // internally. Since it works for all architectures, we should just - // hardcode this as 64 - i, _ := strconv.ParseInt(s, 10, 64) - - // The real fun starts here. We want to return an int, but on - // systems where MaxInt <= MaxInt32, we can't just return i, - // because i is of type int64. - - // First, let's check the obvious case. if the number is < 0, it's - // really not of any use to us. Let's just return 0 - if i < 0 { - return 0 - } +var intSize int - // Now, check if we are in 32-bit land or not. - // The problem is that we can't first check for MaxInt == MaxInt64, - // because the moment we use the MaxInt64 constant, 32-bit Go - // goes kaput. - // - // So check if we're on a 32-bit Go by checking MaxInt == MaxInt32, - // and check if we're over that 32-bit range by checking i > math.MaxInt32 +func init() { + intSize = 64 if math.MaxInt == math.MaxInt32 { - // If we are on a 32-bit system with a value > MaxInt32, just return 0 - if i > math.MaxInt32 { - return 0 - } - - // This is safe to do, because we have established that i <= math.MaxInt32, - // and that int == 32 bits. This conversion _works_ - return int(i) - } else { - return int(i) + intSize = 32 } +} - // If all else fails, just return a 0 - return 0 +func parseBirthdayInt(s string) int { + i, err := strconv.ParseUint(s, 10, intSize) + if err != nil { + return 0 + } + return int(i) } var birthdateRx = regexp.MustCompile(`^(\d{4})-(\d{2})-(\d{2})$`)