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
This commit is contained in:
andrew@webrtc.org 2014-12-17 22:56:09 +00:00
parent bc03192560
commit 0ab42bc3f6
4 changed files with 11 additions and 21 deletions

View File

@ -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",

View File

@ -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',

View File

@ -15,20 +15,11 @@
#include <limits>
#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 <typename Dst, typename Src>
@ -41,7 +32,7 @@ inline bool IsValueInRangeForNumericType(Src value) {
// overflow or underflow. NaN source will always trigger a CHECK.
template <typename Dst, typename Src>
inline Dst checked_cast(Src value) {
Check(IsValueInRangeForNumericType<Dst>(value));
CHECK(IsValueInRangeForNumericType<Dst>(value));
return static_cast<Dst>(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<Dst>::max();
}
Check(false); // NOTREACHED();
FATAL();
return static_cast<Dst>(value);
}

View File

@ -15,6 +15,7 @@
#include <limits>
#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<uint32_t>::max());
// There could be metadata after the audio; ensure we don't read it.
num_samples = std::min(static_cast<uint32_t>(num_samples),
num_samples = std::min(rtc::checked_cast<uint32_t>(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<uint32_t>(read);
num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read);
return read;
}