Skip to content

Commit 9c4aec8

Browse files
committed
Use non-truncating cast of constant expressions
1 parent 517390e commit 9c4aec8

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

src/ansi-c/c_preprocess.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ bool c_preprocess_visual_studio(
253253

254254
// This marks the command file as UTF-8, which Visual Studio
255255
// understands.
256-
command_file << char(0xef) << char(0xbb) << char(0xbf);
256+
command_file << (unsigned char)0xefu << (unsigned char)0xbbu
257+
<< (unsigned char)0xbfu;
257258

258259
command_file << "/nologo" << '\n';
259260
command_file << "/E" << '\n';

src/goto-cc/ms_cl_cmdline.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ void ms_cl_cmdlinet::process_response_file(const std::string &file)
182182
// these may be Unicode -- which is indicated by 0xff 0xfe
183183
std::string line;
184184
getline(infile, line);
185-
if(line.size()>=2 &&
186-
line[0]==static_cast<char>(0xff) &&
187-
line[1]==static_cast<char>(0xfe))
185+
if(
186+
line.size() >= 2 && static_cast<unsigned char>(line[0]) == 0xffu &&
187+
static_cast<unsigned char>(line[1]) == 0xfeu)
188188
{
189189
// Unicode, UTF-16 little endian
190190

@@ -208,10 +208,10 @@ void ms_cl_cmdlinet::process_response_file(const std::string &file)
208208
209209
#endif
210210
}
211-
else if(line.size()>=3 &&
212-
line[0]==static_cast<char>(0xef) &&
213-
line[1]==static_cast<char>(0xbb) &&
214-
line[2]==static_cast<char>(0xbf))
211+
else if(
212+
line.size() >= 3 && static_cast<unsigned char>(line[0]) == 0xefu &&
213+
static_cast<unsigned char>(line[1]) == 0xbbu &&
214+
static_cast<unsigned char>(line[2]) == 0xbfu)
215215
{
216216
// This is the UTF-8 BOM. We can proceed as usual, since
217217
// we use UTF-8 internally.

src/goto-cc/ms_link_cmdline.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ void ms_link_cmdlinet::process_response_file(const std::string &file)
120120
std::string line;
121121
getline(infile, line);
122122
if(
123-
line.size() >= 2 && line[0] == static_cast<char>(0xff) &&
124-
line[1] == static_cast<char>(0xfe))
123+
line.size() >= 2 && static_cast<unsigned char>(line[0]) == 0xffu &&
124+
static_cast<unsigned char>(line[1]) == 0xfeu)
125125
{
126126
// Unicode, UTF-16 little endian
127127

@@ -146,8 +146,9 @@ void ms_link_cmdlinet::process_response_file(const std::string &file)
146146
#endif
147147
}
148148
else if(
149-
line.size() >= 3 && line[0] == static_cast<char>(0xef) &&
150-
line[1] == static_cast<char>(0xbb) && line[2] == static_cast<char>(0xbf))
149+
line.size() >= 3 && static_cast<unsigned char>(line[0]) == 0xefu &&
150+
static_cast<unsigned char>(line[1]) == 0xbbu &&
151+
static_cast<unsigned char>(line[2]) == 0xbfu)
151152
{
152153
// This is the UTF-8 BOM. We can proceed as usual, since
153154
// we use UTF-8 internally.

src/solvers/smt2/smt2_tokenizer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,13 @@ void smt2_tokenizert::get_token_from_stream()
223223
case ' ':
224224
case '\r':
225225
case '\t':
226+
#include <util/pragma_push.def>
227+
#ifdef _MSC_VER
228+
#pragma warning(disable : 4309)
229+
// truncation of constant value
230+
#endif
226231
case static_cast<char>(160): // non-breaking space
232+
#include <util/pragma_pop.def>
227233
// skip any whitespace
228234
break;
229235

src/util/config.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,16 @@ bool configt::set(const cmdlinet &cmdline)
11201120
INVARIANT(
11211121
ansi_c.double_width == sizeof(double) * CHAR_BIT,
11221122
"double width shall be equal to the system double width");
1123+
#include <util/pragma_push.def>
1124+
#ifdef _MSC_VER
1125+
#pragma warning(disable : 4309)
1126+
// truncation of constant value
1127+
#endif
11231128
INVARIANT(
11241129
ansi_c.char_is_unsigned ==
11251130
(static_cast<char>((1 << CHAR_BIT) - 1) == (1 << CHAR_BIT) - 1),
11261131
"char_is_unsigned flag shall indicate system char unsignedness");
1132+
#include <util/pragma_pop.def>
11271133

11281134
#ifndef _WIN32
11291135
// On Windows, long double width varies by compiler

0 commit comments

Comments
 (0)