mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
bbdd4429544a580d804c87018e77442ebc533e33
## Summary
Fix bug where negative values in `Limit()`, `Page()`, and `Offset()`
methods generate invalid SQL causing database errors.
## Root Cause
The methods don't validate negative input:
- `Limit(-1)` generates `LIMIT -1` → SQL error
- `Page(1, -10)` generates `LIMIT -10` → SQL error
- `Offset(-5)` generates `OFFSET -5` → SQL error
## Fix
Treat all negative values as zero (safe default):
**Limit() method**:
```go
case 1:
if limit[0] < 0 { limit[0] = 0 }
case 2:
if limit[0] < 0 { limit[0] = 0 }
if limit[1] < 0 { limit[1] = 0 }
```
**Page() method**:
```go
if limit < 0 { limit = 0 }
```
**Offset() method**:
```go
if offset < 0 { offset = 0 }
```
## Behavior Changes
- `Limit(-1)` → `Limit(0)` (no limit)
- `Limit(-10, -5)` → `Limit(0, 0)` (no offset, no limit)
- `Page(1, -10)` → `Page(1, 0)` (no results)
- `Offset(-5)` → `Offset(0)` (no offset)
## Documentation
Added "Note: Negative values are treated as zero" to all three methods.
## Tests
Added `Test_Issue4699` in `database/gdb/gdb_z_unit_issue_test.go` with 7
test cases:
1. Limit with single negative parameter
2. Limit with two negative parameters
3. Limit with mixed parameters (negative start, positive limit)
4. Page with negative limit
5. Page with negative limit on page 2
6. Offset with negative value
7. Offset with positive value (sanity check)
## Related
Fixes #4699
Ref #4703 (discovered during pagination test development)
refactor(container): add default nil checker, rename RegisterNilChecker to SetNilChecker, migrate instance containers to type-safe generics (#4630)
refract(gerror): add ITextArgs interface and its implements, mainly for i18n that needs text and args separately (#4597)
English | 简体中文
A powerful framework for faster, easier, and more efficient project development.
Installation
go get -u github.com/gogf/gf/v2
Documentation
- Official Site: https://goframe.org
- Official Site(en): https://goframe.org/en
- 国内镜像: https://goframe.org.cn
- Mirror Site: https://pages.goframe.org
- Mirror Site: Offline Docs
- GoDoc API: https://pkg.go.dev/github.com/gogf/gf/v2
- Doc Source: https://github.com/gogf/gf-site
Contributors
💖 Thanks to all the contributors who made GoFrame possible 💖
License
GoFrame is licensed under the MIT License, 100% free and open-source, forever.
Languages
GO
100%
