[cr147] Some UNSAFE_TODO were converted in RealtimeAnalyser.

Chromium change:
https://source.chromium.org/chromium/chromium/src/+/83fa9fd4069068ef813131f46fc5d946c0915286

commit 83fa9fd4069068ef813131f46fc5d946c0915286
Author: Michael Wilson <mjwilson@chromium.org>
Date:   Thu Feb 26 14:47:49 2026 -0800

    Convert some UNSAFE_TODO to safe operations in RealtimeAnalyser

    Using existing span methods we can directly replace most of the unsafe
    operations.

    This should cause no functional change.

    Bug: 401184803
This commit is contained in:
Max Karolinskiy
2026-03-26 19:55:17 -04:00
parent caf66c41ee
commit a4ff0b59e5
4 changed files with 72 additions and 70 deletions
@@ -3,33 +3,35 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#define BRAVE_REALTIMEANALYSER_CONVERTFLOATTODB \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleConvertFloatToDb(source, destination, len); \
return; \
#define BRAVE_REALTIMEANALYSER_CONVERTFLOATTODB \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleConvertFloatToDb(magnitude_buffer_, \
destination, len); \
return; \
}
#define BRAVE_REALTIMEANALYSER_CONVERTTOBYTEDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleConvertToByteData( \
source, destination, len, min_decibels, range_scale_factor); \
return; \
#define BRAVE_REALTIMEANALYSER_CONVERTTOBYTEDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleConvertToByteData( \
magnitude_buffer_, destination, len, min_decibels, \
range_scale_factor); \
return; \
}
#define BRAVE_REALTIMEANALYSER_GETFLOATTIMEDOMAINDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleFloatTimeDomainData( \
input_buffer, destination, len, write_index, fft_size, \
kInputBufferSize); \
return; \
#define BRAVE_REALTIMEANALYSER_GETFLOATTIMEDOMAINDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleFloatTimeDomainData( \
input_buffer_, destination, len, write_index, fft_size, \
kInputBufferSize); \
return; \
}
#define BRAVE_REALTIMEANALYSER_GETBYTETIMEDOMAINDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleByteTimeDomainData( \
input_buffer, destination, len, write_index, fft_size, \
kInputBufferSize); \
return; \
#define BRAVE_REALTIMEANALYSER_GETBYTETIMEDOMAINDATA \
if (audio_farbling_helper_) { \
audio_farbling_helper_->FarbleByteTimeDomainData( \
input_buffer_, destination, len, write_index, fft_size, \
kInputBufferSize); \
return; \
}
#include <third_party/blink/renderer/modules/webaudio/realtime_analyser.cc>
@@ -1,36 +1,36 @@
diff --git a/third_party/blink/renderer/modules/webaudio/realtime_analyser.cc b/third_party/blink/renderer/modules/webaudio/realtime_analyser.cc
index bd241b08b215e8b86938fd802d7fa6185bc9fc60..2efde07579c6818f0b367315089fd31fa4d1c3ad 100644
index 9f2c3f443f4188f40c7562a98ffc536b55ba040b..a1a60bb1c5275517e61f2d56a710daf2e9da6b6d 100644
--- a/third_party/blink/renderer/modules/webaudio/realtime_analyser.cc
+++ b/third_party/blink/renderer/modules/webaudio/realtime_analyser.cc
@@ -115,6 +115,7 @@ void RealtimeAnalyser::GetFloatFrequencyData(DOMFloat32Array* destination_array,
const float* source = magnitude_buffer_.Data();
float* destination = destination_array->Data();
@@ -114,6 +114,7 @@ void RealtimeAnalyser::GetFloatFrequencyData(DOMFloat32Array* destination_array,
const size_t len = std::min(source_length, destination_array->length());
if (len > 0) {
base::span<float> destination = destination_array->AsSpan();
+ BRAVE_REALTIMEANALYSER_CONVERTFLOATTODB
for (unsigned i = 0; i < len; ++i) {
const float linear_value = UNSAFE_TODO(source[i]);
const float linear_value = magnitude_buffer_[i];
const double db_mag = audio_utilities::LinearToDecibels(linear_value);
@@ -150,6 +151,7 @@ void RealtimeAnalyser::GetByteFrequencyData(DOMUint8Array* destination_array,
const float* source = magnitude_buffer_.Data();
unsigned char* destination = destination_array->Data();
@@ -147,6 +148,7 @@ void RealtimeAnalyser::GetByteFrequencyData(DOMUint8Array* destination_array,
const double min_decibels = min_decibels_;
base::span<unsigned char> destination = destination_array->AsSpan();
+ BRAVE_REALTIMEANALYSER_CONVERTTOBYTEDATA
for (unsigned i = 0; i < len; ++i) {
const float linear_value = UNSAFE_TODO(source[i]);
const float linear_value = magnitude_buffer_[i];
const double db_mag = audio_utilities::LinearToDecibels(linear_value);
@@ -183,6 +185,7 @@ void RealtimeAnalyser::GetFloatTimeDomainData(
@@ -178,6 +180,7 @@ void RealtimeAnalyser::GetFloatTimeDomainData(
const unsigned write_index = GetWriteIndex();
base::span<float> destination = destination_array->AsSpan();
+ BRAVE_REALTIMEANALYSER_GETFLOATTIMEDOMAINDATA
for (unsigned i = 0; i < len; ++i) {
// Buffer access is protected due to modulo operation.
float value = UNSAFE_TODO(
@@ -210,6 +213,7 @@ void RealtimeAnalyser::GetByteTimeDomainData(DOMUint8Array* destination_array) {
float value =
@@ -203,6 +206,7 @@ void RealtimeAnalyser::GetByteTimeDomainData(DOMUint8Array* destination_array) {
const unsigned write_index = GetWriteIndex();
base::span<unsigned char> destination = destination_array->AsSpan();
+ BRAVE_REALTIMEANALYSER_GETBYTETIMEDOMAINDATA
for (unsigned i = 0; i < len; ++i) {
// Buffer access is protected due to modulo operation.
const float value = UNSAFE_TODO(
const float value =
@@ -45,8 +45,8 @@ void BraveAudioFarblingHelper::FarbleAudioChannel(base::span<float> dst) const {
// Calculate values for RealtimeAnalyser::GetFloatTimeDomainData
void BraveAudioFarblingHelper::FarbleFloatTimeDomainData(
const float* input_buffer,
float* destination,
const AudioFloatArray& input_buffer,
base::span<float>& destination,
size_t len,
unsigned write_index,
unsigned fft_size,
@@ -56,26 +56,25 @@ void BraveAudioFarblingHelper::FarbleFloatTimeDomainData(
for (size_t i = 0; i < len; ++i) {
v = lfsr_next(v);
float value = (v / maxUInt64AsDouble) / 10;
UNSAFE_TODO(destination[i]) = value;
destination[i] = value;
}
} else {
for (size_t i = 0; i < len; ++i) {
// Buffer access is protected due to modulo operation.
float value =
fudge_factor_ *
UNSAFE_TODO(
input_buffer[(i + write_index - fft_size + input_buffer_size) %
input_buffer_size]);
input_buffer[(i + write_index - fft_size + input_buffer_size) %
input_buffer_size];
UNSAFE_TODO(destination[i]) = value;
destination[i] = value;
}
}
}
// Calculate values for RealtimeAnalyser::GetByteTimeDomainData
void BraveAudioFarblingHelper::FarbleByteTimeDomainData(
const float* input_buffer,
unsigned char* destination,
const AudioFloatArray& input_buffer,
base::span<unsigned char>& destination,
size_t len,
unsigned write_index,
unsigned fft_size,
@@ -97,16 +96,15 @@ void BraveAudioFarblingHelper::FarbleByteTimeDomainData(
scaled_value = UCHAR_MAX;
}
UNSAFE_TODO(destination[i]) = static_cast<unsigned char>(scaled_value);
destination[i] = static_cast<unsigned char>(scaled_value);
}
} else {
for (size_t i = 0; i < len; ++i) {
// Buffer access is protected due to modulo operation.
float value =
fudge_factor_ *
UNSAFE_TODO(
input_buffer[(i + write_index - fft_size + input_buffer_size) %
input_buffer_size]);
input_buffer[(i + write_index - fft_size + input_buffer_size) %
input_buffer_size];
// Scale from nominal -1 -> +1 to unsigned byte.
double scaled_value = 128 * (value + 1);
@@ -119,15 +117,15 @@ void BraveAudioFarblingHelper::FarbleByteTimeDomainData(
scaled_value = UCHAR_MAX;
}
UNSAFE_TODO(destination[i]) = static_cast<unsigned char>(scaled_value);
destination[i] = static_cast<unsigned char>(scaled_value);
}
}
}
// Calculate values for RealtimeAnalyser::ConvertToByteData
void BraveAudioFarblingHelper::FarbleConvertToByteData(
const float* source,
unsigned char* destination,
const AudioFloatArray& source,
base::span<unsigned char>& destination,
size_t len,
double min_decibels,
double range_scale_factor) const {
@@ -151,11 +149,11 @@ void BraveAudioFarblingHelper::FarbleConvertToByteData(
scaled_value = UCHAR_MAX;
}
UNSAFE_TODO(destination[i]) = static_cast<unsigned char>(scaled_value);
destination[i] = static_cast<unsigned char>(scaled_value);
}
} else {
for (size_t i = 0; i < len; ++i) {
float linear_value = fudge_factor_ * UNSAFE_TODO(source[i]);
float linear_value = fudge_factor_ * source[i];
double db_mag = audio_utilities::LinearToDecibels(linear_value);
// The range m_minDecibels to m_maxDecibels will be scaled to byte values
@@ -171,28 +169,29 @@ void BraveAudioFarblingHelper::FarbleConvertToByteData(
scaled_value = UCHAR_MAX;
}
UNSAFE_TODO(destination[i]) = static_cast<unsigned char>(scaled_value);
destination[i] = static_cast<unsigned char>(scaled_value);
}
}
}
// Calculate values for RealtimeAnalyser::ConvertFloatToDb
void BraveAudioFarblingHelper::FarbleConvertFloatToDb(const float* source,
float* destination,
size_t len) const {
void BraveAudioFarblingHelper::FarbleConvertFloatToDb(
const AudioFloatArray& source,
base::span<float>& destination,
size_t len) const {
if (max_) {
uint64_t v = seed_;
for (size_t i = 0; i < len; ++i) {
v = lfsr_next(v);
float linear_value = (v / maxUInt64AsDouble) / 10;
double db_mag = audio_utilities::LinearToDecibels(linear_value);
UNSAFE_TODO(destination[i]) = static_cast<float>(db_mag);
destination[i] = static_cast<float>(db_mag);
}
} else {
for (size_t i = 0; i < len; ++i) {
float linear_value = fudge_factor_ * UNSAFE_TODO(source[i]);
float linear_value = fudge_factor_ * source[i];
double db_mag = audio_utilities::LinearToDecibels(linear_value);
UNSAFE_TODO(destination[i]) = static_cast<float>(db_mag);
destination[i] = static_cast<float>(db_mag);
}
}
}
@@ -10,6 +10,7 @@
#include <stdint.h>
#include "base/containers/span.h"
#include "third_party/blink/renderer/platform/audio/audio_array.h"
#include "third_party/blink/renderer/platform/platform_export.h"
namespace blink {
@@ -20,25 +21,25 @@ class PLATFORM_EXPORT BraveAudioFarblingHelper final {
~BraveAudioFarblingHelper();
void FarbleAudioChannel(base::span<float> dst) const;
void FarbleFloatTimeDomainData(const float* input_buffer,
float* destination,
void FarbleFloatTimeDomainData(const AudioFloatArray& input_buffer,
base::span<float>& destination,
size_t len,
unsigned write_index,
unsigned fft_size,
unsigned input_buffer_size) const;
void FarbleByteTimeDomainData(const float* input_buffer,
unsigned char* destination,
void FarbleByteTimeDomainData(const AudioFloatArray& input_buffer,
base::span<unsigned char>& destination,
size_t len,
unsigned write_index,
unsigned fft_size,
unsigned input_buffer_size) const;
void FarbleConvertToByteData(const float* source,
unsigned char* destination,
void FarbleConvertToByteData(const AudioFloatArray& source,
base::span<unsigned char>& destination,
size_t len,
double min_decibels,
double range_scale_factor) const;
void FarbleConvertFloatToDb(const float* source,
float* destination,
void FarbleConvertFloatToDb(const AudioFloatArray& source,
base::span<float>& destination,
size_t len) const;
private: