From 0ab42bc3f6438db4194b3d77b66629413c7038da Mon Sep 17 00:00:00 2001 From: "andrew@webrtc.org" Date: Wed, 17 Dec 2014 22:56:09 +0000 Subject: [PATCH] Make safe_conversions suitable for rtc_base_approved. Since we want to use checked_cast in WavReader. R=kwiberg@webrtc.org Review URL: https://webrtc-codereview.appspot.com/32839004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7937 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/base/BUILD.gn | 4 ++-- webrtc/base/base.gyp | 4 ++-- webrtc/base/safe_conversions.h | 17 ++++------------- webrtc/common_audio/wav_file.cc | 7 +++---- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index acce9f0401..ef02817e55 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -113,6 +113,8 @@ static_library("rtc_base_approved") { "md5digest.h", "platform_file.cc", "platform_file.h", + "safe_conversions.h", + "safe_conversions_impl.h", "stringencode.cc", "stringencode.h", "stringutils.cc", @@ -225,8 +227,6 @@ static_library("webrtc_base") { "ratelimiter.h", "ratetracker.cc", "ratetracker.h", - "safe_conversions.h", - "safe_conversions_impl.h", "scoped_autorelease_pool.h", "scoped_autorelease_pool.mm", "scoped_ptr.h", diff --git a/webrtc/base/base.gyp b/webrtc/base/base.gyp index 380eee6ed5..645c1dcf4b 100644 --- a/webrtc/base/base.gyp +++ b/webrtc/base/base.gyp @@ -48,6 +48,8 @@ 'md5digest.h', 'platform_file.cc', 'platform_file.h', + 'safe_conversions.h', + 'safe_conversions_impl.h', 'stringencode.cc', 'stringencode.h', 'stringutils.cc', @@ -219,8 +221,6 @@ 'refcount.h', 'referencecountedsingletonfactory.h', 'rollingaccumulator.h', - 'safe_conversions.h', - 'safe_conversions_impl.h', 'schanneladapter.cc', 'schanneladapter.h', 'scoped_autorelease_pool.h', diff --git a/webrtc/base/safe_conversions.h b/webrtc/base/safe_conversions.h index f6cb24e412..7fc67cb67a 100644 --- a/webrtc/base/safe_conversions.h +++ b/webrtc/base/safe_conversions.h @@ -15,20 +15,11 @@ #include -#include "webrtc/base/common.h" -#include "webrtc/base/logging.h" +#include "webrtc/base/checks.h" #include "webrtc/base/safe_conversions_impl.h" namespace rtc { -inline void Check(bool condition) { - if (!condition) { - LOG(LS_ERROR) << "CHECK failed."; - Break(); - // The program should have crashed at this point. - } -} - // Convenience function that returns true if the supplied value is in range // for the destination type. template @@ -41,7 +32,7 @@ inline bool IsValueInRangeForNumericType(Src value) { // overflow or underflow. NaN source will always trigger a CHECK. template inline Dst checked_cast(Src value) { - Check(IsValueInRangeForNumericType(value)); + CHECK(IsValueInRangeForNumericType(value)); return static_cast(value); } @@ -66,11 +57,11 @@ inline Dst saturated_cast(Src value) { // Should fail only on attempting to assign NaN to a saturated integer. case internal::TYPE_INVALID: - Check(false); + FATAL(); return std::numeric_limits::max(); } - Check(false); // NOTREACHED(); + FATAL(); return static_cast(value); } diff --git a/webrtc/common_audio/wav_file.cc b/webrtc/common_audio/wav_file.cc index cc96e41da3..de83b03380 100644 --- a/webrtc/common_audio/wav_file.cc +++ b/webrtc/common_audio/wav_file.cc @@ -15,6 +15,7 @@ #include #include "webrtc/base/checks.h" +#include "webrtc/base/safe_conversions.h" #include "webrtc/common_audio/include/audio_util.h" #include "webrtc/common_audio/wav_header.h" @@ -58,17 +59,15 @@ size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { #ifndef WEBRTC_ARCH_LITTLE_ENDIAN #error "Need to convert samples to big-endian when reading from WAV file" #endif - // TODO(ajm): Import Chromium's safe_conversions.h for this. - CHECK_LE(num_samples, std::numeric_limits::max()); // There could be metadata after the audio; ensure we don't read it. - num_samples = std::min(static_cast(num_samples), + num_samples = std::min(rtc::checked_cast(num_samples), num_samples_remaining_); const size_t read = fread(samples, sizeof(*samples), num_samples, file_handle_); // If we didn't read what was requested, ensure we've reached the EOF. CHECK(read == num_samples || feof(file_handle_)); CHECK_LE(read, num_samples_remaining_); - num_samples_remaining_ -= static_cast(read); + num_samples_remaining_ -= rtc::checked_cast(read); return read; }