Skip to content

fix: allow signed integer fields in port validation - #1633

Merged
nodivbyzero merged 3 commits into
go-playground:masterfrom
EJJI0113:fix/port-signed-integers
Sep 17, 2026
Merged

nodivbyzero merged 3 commits into
go-playground:masterfrom
EJJI0113:fix/port-signed-integers

Conversation

@EJJI0113

Copy link
Copy Markdown
Contributor

Fixes Or Enhances

The port validator panics on signed integer fields, including a valid int(8080). This prevents validating port values using the types used by Go's standard networking APIs.

Handle signed integer kinds with reflect.Value.Int() while preserving unsigned integer validation and the existing 1–65535 range. Extend the regression tests to cover signed kinds, named integer types, negative and out-of-range values, and unsigned compatibility. Update the port documentation accordingly.

  • Tests exist or have been written that cover this particular change.

Validation:

  • go test . -run '^Test_port_validator$' -count=1
  • go test -race -covermode=atomic -coverprofile=../validator-coverage.out ./...
  • go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.13.1 run

Fixes #1632.

@EJJI0113
EJJI0113 requested a review from a team as a code owner September 15, 2026 10:51

@ilovelinux ilovelinux left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, a minor suggestion to explicitly reject unsupported values.

Comment thread baked_in.go Outdated
Comment on lines +3054 to +3056
default:
val := field.Uint()
return val >= 1 && val <= 65535

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd implement the same logic implemented in isIso3166AlphaNumeric and similar functions. The panic error is going to be more explicit.

Suggested change
default:
val := field.Uint()
return val >= 1 && val <= 65535
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
val := field.Uint()
return val >= 1 && val <= 65535
default:
panic(fmt.Sprintf("Bad field type %s", field.Type()))

@ilovelinux ilovelinux left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread doc.go Outdated
# Port

This validates that the value falls within the valid port number range of 1 to 65,535.
This validates that a signed or unsigned integer value falls within the valid port number range of 1 to 65,535.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This validates that a signed or unsigned integer value falls within the valid port number range of 1 to 65,535.
This validates that an integer value (signed or unsigned) falls within the valid port range of 1-65,535.

nodivbyzero
nodivbyzero previously approved these changes Sep 16, 2026

@nodivbyzero nodivbyzero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! The changes look good to me with just one minor doc change request.

@nodivbyzero
nodivbyzero merged commit c7f3d2c into go-playground:master Sep 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow port to be int

3 participants