mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 13:50:40 +01:00

It's easy to make small errors when building field trial strings, and those errors can cause all sorts of weird problems. This CL checks if the FT string has an odd number of delimiters, duplicate names or any trailing chars. If so we'll log a error message. On debug builds we'll also crash. Bug: webrtc:10729 Change-Id: Iebf7155d9b117a02d1e9cfe7f64408e11df2aec5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/140866 Reviewed-by: Sebastian Jansson <srte@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org> Cr-Commit-Position: refs/heads/master@{#28234}
122 lines
4 KiB
C++
122 lines
4 KiB
C++
// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license
|
|
// that can be found in the LICENSE file in the root of the source
|
|
// tree. An additional intellectual property rights grant can be found
|
|
// in the file PATENTS. All contributing project authors may
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
//
|
|
|
|
#include "system_wrappers/include/field_trial.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#include "absl/strings/string_view.h"
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/logging.h"
|
|
|
|
// Simple field trial implementation, which allows client to
|
|
// specify desired flags in InitFieldTrialsFromString.
|
|
namespace webrtc {
|
|
namespace field_trial {
|
|
|
|
static const char* trials_init_string = NULL;
|
|
|
|
#ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
|
|
namespace {
|
|
constexpr char kPersistentStringSeparator = '/';
|
|
// Validates the given field trial string.
|
|
// E.g.:
|
|
// "WebRTC-experimentFoo/Enabled/WebRTC-experimentBar/Enabled100kbps/"
|
|
// Assigns the process to group "Enabled" on WebRTCExperimentFoo trial
|
|
// and to group "Enabled100kbps" on WebRTCExperimentBar.
|
|
//
|
|
// E.g. invalid config:
|
|
// "WebRTC-experiment1/Enabled" (note missing / separator at the end).
|
|
bool FieldTrialsStringIsValid(const absl::string_view trials) {
|
|
if (trials.empty())
|
|
return true;
|
|
|
|
size_t next_item = 0;
|
|
std::map<absl::string_view, absl::string_view> field_trials;
|
|
while (next_item < trials.length()) {
|
|
size_t name_end = trials.find(kPersistentStringSeparator, next_item);
|
|
if (name_end == trials.npos || next_item == name_end)
|
|
return false;
|
|
size_t group_name_end =
|
|
trials.find(kPersistentStringSeparator, name_end + 1);
|
|
if (group_name_end == trials.npos || name_end + 1 == group_name_end)
|
|
return false;
|
|
absl::string_view name = trials.substr(next_item, name_end - next_item);
|
|
absl::string_view group_name =
|
|
trials.substr(name_end + 1, group_name_end - name_end - 1);
|
|
|
|
next_item = group_name_end + 1;
|
|
|
|
// Fail if duplicate with different group name.
|
|
if (field_trials.find(name) != field_trials.end() &&
|
|
field_trials.find(name)->second != group_name) {
|
|
return false;
|
|
}
|
|
|
|
field_trials[name] = group_name;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
std::string FindFullName(const std::string& name) {
|
|
if (trials_init_string == NULL)
|
|
return std::string();
|
|
|
|
std::string trials_string(trials_init_string);
|
|
if (trials_string.empty())
|
|
return std::string();
|
|
|
|
size_t next_item = 0;
|
|
while (next_item < trials_string.length()) {
|
|
// Find next name/value pair in field trial configuration string.
|
|
size_t field_name_end =
|
|
trials_string.find(kPersistentStringSeparator, next_item);
|
|
if (field_name_end == trials_string.npos || field_name_end == next_item)
|
|
break;
|
|
size_t field_value_end =
|
|
trials_string.find(kPersistentStringSeparator, field_name_end + 1);
|
|
if (field_value_end == trials_string.npos ||
|
|
field_value_end == field_name_end + 1)
|
|
break;
|
|
std::string field_name(trials_string, next_item,
|
|
field_name_end - next_item);
|
|
std::string field_value(trials_string, field_name_end + 1,
|
|
field_value_end - field_name_end - 1);
|
|
next_item = field_value_end + 1;
|
|
|
|
if (name == field_name)
|
|
return field_value;
|
|
}
|
|
return std::string();
|
|
}
|
|
#endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
|
|
|
|
// Optionally initialize field trial from a string.
|
|
void InitFieldTrialsFromString(const char* trials_string) {
|
|
RTC_LOG(LS_INFO) << "Setting field trial string:" << trials_string;
|
|
#ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
|
|
if (trials_string) {
|
|
RTC_DCHECK(FieldTrialsStringIsValid(trials_string))
|
|
<< "Invalid field trials string:" << trials_string;
|
|
};
|
|
#endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
|
|
trials_init_string = trials_string;
|
|
}
|
|
|
|
const char* GetFieldTrialString() {
|
|
return trials_init_string;
|
|
}
|
|
|
|
} // namespace field_trial
|
|
} // namespace webrtc
|