You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: reject overflow when scanning SQL enum numbers - #327
SQL Scan currently casts values directly to the enum's integer type, so out-of-range values can silently become different named values (for example, int64(256) becomes the zero value of an int8 enum). Parse integers with the destination width and signedness before assigning; use the same check for pointers and the sqlnullint numeric-string fallback. Float inputs retain truncation of representable fractional values but reject overflow and non-finite values. Failed numeric conversions preserve the receiver.
Keep valid unnamed values, nil handling, string enum parsing and existing Scan entry types supported. Named integer underlying types keep their actual width/signedness. Regenerate the affected examples and snapshots; Valuer behavior and the broader SQL redesign in #239 are outside this change.
Validation:
Generated-code regression exercises 13 built-in integer/alias types plus two named integer types under --sql, --sqlnullstr and --sqlnullint. The original implementation fails overflow cases; the correction passes.
Go 1.25.0 and 1.26.0: make cover passes, including race/shuffle and example-tag tests. Go 1.26.0 go vet ./... passes.
An independent Go 1.25.5 runtime harness passes 4,055 assertions across 15 integer types and SQL/nullable/marshal/noparse combinations, including float boundaries, unsigned negative zero, typed nil and Null.Valid/Set. Linux/386 test binaries cross-compile for all three combinations (not executed).
make generate followed by make assert-no-changes passes after committing generated files.
gosec v2.29.0, G115 only, with generated files included: the two-type issue reproduction reports 10 findings before and 0 after. GOTOOLCHAIN=go1.25.0 is required for this check; the analyzer cannot read Go 1.27 export data.
Validation ran on macOS/arm64. Native 32-bit, Linux and Windows execution were not run locally.
Summary by CodeRabbit
Bug Fixes
Numeric values scanned into SQL-backed enums are now checked against the enum’s supported range. Out-of-range values return errors instead of being silently converted.
Fractional floating-point values are truncated toward zero before conversion; values that exceed the supported range return errors.
Numeric strings are checked for overflow when scanning into integer-backed enums.
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.
📝 Walkthrough
SQL enum scanners now validate numeric conversions against the enum’s integer width and signedness. Float inputs are truncated before parsing. The generator template, example scanners, generated snapshots, and generated tests reflect these changes.
Changes: SQL enum numeric scanning
Layer / File(s)
Summary
Generator parsing logic and validation generator/enum.tmpl, generator/sql_integer_test.go
The template parses numeric inputs with width and signedness checks, truncates floats, and supports numeric-string fallback for SQL-null-integer enums. Generated tests cover integer boundaries, invalid inputs, and receiver state.
Example scanners use checked numeric parsing instead of direct casts. Float inputs are truncated before conversion, and existing nil-pointer errors remain.
Snapshots update generated scanner code across enum widths and generator options to show checked numeric conversions and corresponding string handling.
Generated SQL enum scanners now reject numeric values that do not fit the enum's integer type, instead of silently wrapping them. Existing valid inputs and string parsing keep working. No outstanding defects were found, so the change looks ready to merge.
Pre-merge checks: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title clearly and concisely describes the main change: rejecting integer overflow during SQL enum scanning.
Linked Issues check
Issue #252 requires the generated --sql Scan path to avoid unsafe integer overflow conversions. generator/enum.tmpl now parses numeric inputs with strconv.ParseInt or strconv.ParseUint at the en…
Out of Scope Changes check
The changed template, generated examples, snapshots, and regression test support the overflow-safe SQL Scan behavior for issue #252. Pointer checks, float checks, named-type handling, and sqlnullint…
Docstring Coverage
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 5 files. (12 skipped: 1…
Full details: Linked Issues check
Explanation
Issue #252 requires the generated --sql Scan path to avoid unsafe integer overflow conversions. generator/enum.tmpl now parses numeric inputs with strconv.ParseInt or strconv.ParseUint at the enum width before assignment. The change covers built-in and named integer types, pointers, and the sqlnullint numeric-string fallback. generator/sql_integer_test.go adds overflow and receiver-preservation tests across 15 integer types and three SQL modes. The PR reports zero G115 findings after the change.
Full details: Out of Scope Changes check
Explanation
The changed template, generated examples, snapshots, and regression test support the overflow-safe SQL Scan behavior for issue #252. Pointer checks, float checks, named-type handling, and sqlnullint parsing apply the same numeric safety requirement to supported Scan entry types. The PR states that Valuer behavior and the broader SQL redesign in #239 remain unchanged.
Full details: Docstring Coverage
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 5 files. (12 skipped: 12 unsupported.)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #252.
SQL Scan currently casts values directly to the enum's integer type, so out-of-range values can silently become different named values (for example, int64(256) becomes the zero value of an int8 enum). Parse integers with the destination width and signedness before assigning; use the same check for pointers and the sqlnullint numeric-string fallback. Float inputs retain truncation of representable fractional values but reject overflow and non-finite values. Failed numeric conversions preserve the receiver.
Keep valid unnamed values, nil handling, string enum parsing and existing Scan entry types supported. Named integer underlying types keep their actual width/signedness. Regenerate the affected examples and snapshots; Valuer behavior and the broader SQL redesign in #239 are outside this change.
Validation:
Validation ran on macOS/arm64. Native 32-bit, Linux and Windows execution were not run locally.
Summary by CodeRabbit