Skip to content

Commit

Permalink
okay, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed May 13, 2024
1 parent de513a4 commit 2cfdaa6
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions jwt/openid/birthdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})$`)
Expand Down

0 comments on commit 2cfdaa6

Please sign in to comment.