Commit bbdd442
authored
fix(database/gdb): treat negative Limit/Page/Offset values as zero (gogf#4702)
## 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 gogf#4699
Ref gogf#4703 (discovered during pagination test development)1 parent 6686bd6 commit bbdd442
2 files changed
Lines changed: 72 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
615 | 615 | | |
616 | 616 | | |
617 | 617 | | |
| 618 | + | |
618 | 619 | | |
619 | 620 | | |
620 | 621 | | |
621 | 622 | | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
622 | 626 | | |
623 | 627 | | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
624 | 634 | | |
625 | 635 | | |
626 | 636 | | |
| |||
629 | 639 | | |
630 | 640 | | |
631 | 641 | | |
| 642 | + | |
632 | 643 | | |
633 | 644 | | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
634 | 648 | | |
635 | 649 | | |
636 | 650 | | |
| |||
645 | 659 | | |
646 | 660 | | |
647 | 661 | | |
| 662 | + | |
648 | 663 | | |
649 | 664 | | |
650 | 665 | | |
651 | 666 | | |
652 | 667 | | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
653 | 671 | | |
654 | 672 | | |
655 | 673 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments