[libcxx] [test] Fix MSVC warning C4244 "conversion from 'X' to 'Y', possible loss of data", part 1/7.

Given `std::basic_streambuf<CharT>::int_type __c`, `std::basic_string<CharT> str_`,
and having checked `__c != std::basic_streambuf<CharT>::traits_type::eof()` (substituting typedefs
for clarity), the line `str_.push_back(__c);` is safe according to humans, but truncates according
to compilers. `str_.push_back(static_cast<CharT>(__c));` avoids that problem.

Fixes D27538.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@289105 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Stephan T. Lavavej
2016-12-08 21:37:47 +00:00
parent 22e3b2af50
commit c53fd92b0f
31 changed files with 31 additions and 31 deletions

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -43,7 +43,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -44,7 +44,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -46,7 +46,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -42,7 +42,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -40,7 +40,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -43,7 +43,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));

View File

@@ -39,7 +39,7 @@ protected:
if (__c != base::traits_type::eof()) if (__c != base::traits_type::eof())
{ {
int n = static_cast<int>(str_.size()); int n = static_cast<int>(str_.size());
str_.push_back(__c); str_.push_back(static_cast<CharT>(__c));
str_.resize(str_.capacity()); str_.resize(str_.capacity());
base::setp(const_cast<CharT*>(str_.data()), base::setp(const_cast<CharT*>(str_.data()),
const_cast<CharT*>(str_.data() + str_.size())); const_cast<CharT*>(str_.data() + str_.size()));