Fix nil pointer dereference in GetPrefixDistributionContext

- Use separate variables for IPv4 and IPv6 query results
- Add nil checks before closing rows to prevent panic
- Prevents crash when database queries timeout or fail
This commit is contained in:
Jeffrey Paul 2025-07-28 22:04:22 +02:00
parent 9518519208
commit 6d46bbad5b
2 changed files with 1154 additions and 178381 deletions

View File

@ -908,15 +908,19 @@ func (d *Database) GetPrefixDistributionContext(ctx context.Context) (
GROUP BY mask_length
ORDER BY mask_length
`
rows, err := d.db.QueryContext(ctx, query)
rows4, err := d.db.QueryContext(ctx, query)
if err != nil {
return nil, nil, fmt.Errorf("failed to query IPv4 distribution: %w", err)
}
defer func() { _ = rows.Close() }()
defer func() {
if rows4 != nil {
_ = rows4.Close()
}
}()
for rows.Next() {
for rows4.Next() {
var dist PrefixDistribution
if err := rows.Scan(&dist.MaskLength, &dist.Count); err != nil {
if err := rows4.Scan(&dist.MaskLength, &dist.Count); err != nil {
return nil, nil, fmt.Errorf("failed to scan IPv4 distribution: %w", err)
}
ipv4 = append(ipv4, dist)
@ -930,15 +934,19 @@ func (d *Database) GetPrefixDistributionContext(ctx context.Context) (
GROUP BY mask_length
ORDER BY mask_length
`
rows, err = d.db.QueryContext(ctx, query)
rows6, err := d.db.QueryContext(ctx, query)
if err != nil {
return nil, nil, fmt.Errorf("failed to query IPv6 distribution: %w", err)
}
defer func() { _ = rows.Close() }()
defer func() {
if rows6 != nil {
_ = rows6.Close()
}
}()
for rows.Next() {
for rows6.Next() {
var dist PrefixDistribution
if err := rows.Scan(&dist.MaskLength, &dist.Count); err != nil {
if err := rows6.Scan(&dist.MaskLength, &dist.Count); err != nil {
return nil, nil, fmt.Errorf("failed to scan IPv6 distribution: %w", err)
}
ipv6 = append(ipv6, dist)

179511
log.txt

File diff suppressed because it is too large Load Diff