Skip to content

Conversation

rarguelloF
Copy link
Contributor

What does this PR do?

Motivation

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running ./scripts/lint.sh locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@pr-commenter
Copy link

pr-commenter bot commented Sep 2, 2025

Benchmarks

Benchmark execution time: 2025-09-05 12:32:55

Comparing candidate commit 00310d9 in PR branch rarguelloF/AIDM-708/llmobs-client with baseline commit c0cbb8f in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics.

seconds = int(time.Until(time.Unix(resetTime, 0)).Seconds())
} else {
// Assume it's a duration in seconds
seconds = int(resetTime)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of a signed 64-bit integer from
strconv.ParseInt
to a lower bit size type int without an upper bound check.

Copilot Autofix

AI 5 days ago

To fix the issue, we need to ensure that converting resetTime from int64 to int will not overflow or wrap around unexpectedly. The best approach is to add bounds checks when converting. Since resetTime comes from external input, we should check that its value is within acceptable bounds for the platform's int. In Go, this can be done using constants math.MaxInt32 or math.MaxInt64 depending on platform, but for maximum safety and compatibility, use math.MinInt32, math.MaxInt32 and restrict values accordingly. Add a check before converting to int, and fallback gracefully if out of bounds (e.g., use a default backoff value of 1 second).

Required changes:

  • Add import "math" (if not present).
  • Before converting to int, check if resetTime is within [0, math.MaxInt32] range (since negative sleep seconds make no sense and int32 is the minimum required for cross-platform code); use only positive values.
  • If value falls outside the range, use a default, e.g., 1 second.

Update the block around lines 440–448 to implement these bounds checks.


Suggested changeset 1
llmobs/internal/dne_client.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/llmobs/internal/dne_client.go b/llmobs/internal/dne_client.go
--- a/llmobs/internal/dne_client.go
+++ b/llmobs/internal/dne_client.go
@@ -16,7 +16,7 @@
 	"net/url"
 	"strconv"
 	"time"
-
+	"math"
 	"github.com/DataDog/dd-trace-go/v2/internal"
 	"github.com/cenkalti/backoff/v5"
 )
@@ -441,10 +441,15 @@
 					seconds := 0
 					if resetTime > time.Now().Unix() {
 						// Assume it's a Unix timestamp
-						seconds = int(time.Until(time.Unix(resetTime, 0)).Seconds())
+						sec := time.Until(time.Unix(resetTime, 0)).Seconds()
+						if sec > 0 && sec <= float64(math.MaxInt32) {
+							seconds = int(sec)
+						}
 					} else {
 						// Assume it's a duration in seconds
-						seconds = int(resetTime)
+						if resetTime > 0 && resetTime <= int64(math.MaxInt32) {
+							seconds = int(resetTime)
+						}
 					}
 					if seconds > 0 {
 						waitSeconds = seconds
EOF
@@ -16,7 +16,7 @@
"net/url"
"strconv"
"time"

"math"
"github.com/DataDog/dd-trace-go/v2/internal"
"github.com/cenkalti/backoff/v5"
)
@@ -441,10 +441,15 @@
seconds := 0
if resetTime > time.Now().Unix() {
// Assume it's a Unix timestamp
seconds = int(time.Until(time.Unix(resetTime, 0)).Seconds())
sec := time.Until(time.Unix(resetTime, 0)).Seconds()
if sec > 0 && sec <= float64(math.MaxInt32) {
seconds = int(sec)
}
} else {
// Assume it's a duration in seconds
seconds = int(resetTime)
if resetTime > 0 && resetTime <= int64(math.MaxInt32) {
seconds = int(resetTime)
}
}
if seconds > 0 {
waitSeconds = seconds
Copilot is powered by AI and may make mistakes. Always verify output.
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Sep 2, 2025

No data reported at this time.
This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 00310d9 | Docs | Was this helpful? Give us feedback!

// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025 Datadog, Inc.

package errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar package that we merged a bit ago. Could it cover the use cases we have here, instead of creating a new package for it? We can add more functionality to the other package as needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! I didn't know this existed. I already refactored the code to use this package, will push the changes shortly 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants