Skip to content

Commit 0504a38

Browse files
committed
Avoid using harmful function rand()
1 parent fb3ee40 commit 0504a38

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/core/lib/transport/bdp_estimator.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ BdpEstimator::BdpEstimator(absl::string_view name)
3737
stable_estimate_count_(0),
3838
ping_state_(PingState::UNSCHEDULED),
3939
bw_est_(0),
40+
seed_(0),
4041
name_(name) {}
4142

4243
Timestamp BdpEstimator::CompletePing() {
@@ -63,7 +64,7 @@ Timestamp BdpEstimator::CompletePing() {
6364
if (stable_estimate_count_ >= 2) {
6465
// if the ping estimate is steady, slowly ramp down the probe time
6566
inter_ping_delay_ += Duration::Milliseconds(
66-
100 + static_cast<int>(rand() * 100.0 / RAND_MAX));
67+
100 + static_cast<int>(rand_r(&seed_) * 100.0 / RAND_MAX));
6768
}
6869
}
6970
if (start_inter_ping_delay != inter_ping_delay_) {

src/core/lib/transport/bdp_estimator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class BdpEstimator {
6565
CHECK(ping_state_ == PingState::SCHEDULED);
6666
ping_state_ = PingState::STARTED;
6767
ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
68+
if (!seed_) {
69+
seed_ = ping_start_time_.tv_nsec;
70+
}
6871
}
6972

7073
// Completes a previously started ping, returns when to schedule the next one
@@ -83,6 +86,7 @@ class BdpEstimator {
8386
int stable_estimate_count_;
8487
PingState ping_state_;
8588
double bw_est_;
89+
unsigned int seed_;
8690
absl::string_view name_;
8791
};
8892

0 commit comments

Comments
 (0)