Skip to content

Commit db9b50f

Browse files
committed
chore: implement safeTTLToInt32 function for TTL conversion and update record set payloads
1 parent 5f83648 commit db9b50f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

internal/stackitprovider/helper.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stackitprovider
22

33
import (
4+
"math"
45
"strings"
56

67
stackitdnsclient "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
@@ -78,7 +79,7 @@ func getStackitRecordSetPayload(change *endpoint.Endpoint) stackitdnsclient.Crea
7879
return stackitdnsclient.CreateRecordSetPayload{
7980
Name: change.DNSName,
8081
Records: records,
81-
Ttl: new(int32(change.RecordTTL)),
82+
Ttl: safeTTLToInt32(change.RecordTTL),
8283
Type: change.RecordType,
8384
}
8485
}
@@ -95,7 +96,7 @@ func getStackitPartialUpdateRecordSetPayload(change *endpoint.Endpoint) stackitd
9596
return stackitdnsclient.PartialUpdateRecordSetPayload{
9697
Name: &change.DNSName,
9798
Records: records,
98-
Ttl: new(int32(change.RecordTTL)),
99+
Ttl: safeTTLToInt32(change.RecordTTL),
99100
}
100101
}
101102

@@ -109,3 +110,19 @@ func getLogFields(change *endpoint.Endpoint, action string, id string) []zap.Fie
109110
zap.String("id", id),
110111
}
111112
}
113+
114+
// safeTTLToInt32 safely converts an endpoint.TTL (int64) to *int32, clamping to valid bounds.
115+
func safeTTLToInt32(ttl endpoint.TTL) *int32 {
116+
var v int32
117+
118+
switch {
119+
case int64(ttl) > math.MaxInt32:
120+
v = math.MaxInt32
121+
case int64(ttl) < 0:
122+
v = 0
123+
default:
124+
v = int32(ttl) // #nosec G115 -- bounds checked above
125+
}
126+
127+
return &v
128+
}

0 commit comments

Comments
 (0)