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:
parent
9518519208
commit
6d46bbad5b
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user