spf13 / spf13/cast

Add overflow check

Open
#63 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
4k
Forks
337
Avg merge
15m
Merged PRs (30d)
1

Description

You clam your library to be "safe casting from one type to another in Go", but in the code there are no checks for overflow when casting integer types:

switch s := i.(type) {
	case int:
		return int16(s), nil  // <-- this may overflow
	case int64:
		return int16(s), nil  // <-- this may overflow
	case int32:
		return int16(s), nil
	case int16:
		return s, nil
	case int8:
		return int16(s), nil
	case uint:
		return int16(s), nil   // <-- this may overflow
	case uint64:
		return int16(s), nil   // <-- this may overflow
	case uint32:
		return int16(s), nil   // <-- this may overflow
	case uint16:
		return int16(s), nil   // <-- this may overflow
	case uint8:
		return int16(s), nil
	case float64:
		return int16(s), nil   // <-- this may overflow
	case float32:
		return int16(s), nil  // <-- this may overflow

Adding bounds check like this: https://play.golang.org/p/Jgu2NYg1qi3 would solve the issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the type-switch entry point that performs the integer and floating-point casts, then compare the existing conversions with the bounds-check example in the issue. Add overflow handling for the affected casts and verify that values within range still convert successfully while out-of-range values no longer silently wrap.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.