Skip to content

Commit f8e8d5c

Browse files
committed
Use non-truncating cast of constant expressions
1 parent e352e42 commit f8e8d5c

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ bool c_preprocess_visual_studio(
325325

326326
// This marks the command file as UTF-8, which Visual Studio
327327
// understands.
328-
command_file << char(0xef) << char(0xbb) << char(0xbf);
328+
command_file << (unsigned char)0xefu << (unsigned char)0xbbu
329+
<< (unsigned char)0xbfu;
329330

330331
command_file << "/nologo" << '\n';
331332
command_file << "/E" << '\n';

src/goto-cc/ms_cl_cmdline.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ void ms_cl_cmdlinet::process_response_file(const std::string &file)
180180
// these may be Unicode -- which is indicated by 0xff 0xfe
181181
std::string line;
182182
getline(infile, line);
183-
if(line.size()>=2 &&
184-
line[0]==static_cast<char>(0xff) &&
185-
line[1]==static_cast<char>(0xfe))
183+
if(
184+
line.size() >= 2 && static_cast<unsigned char>(line[0]) == 0xffu &&
185+
static_cast<unsigned char>(line[1]) == 0xfeu)
186186
{
187187
// Unicode, UTF-16 little endian
188188

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

src/goto-cc/ms_link_cmdline.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void ms_link_cmdlinet::process_response_file(const std::string &file)
124124
std::string line;
125125
getline(infile, line);
126126
if(
127-
line.size() >= 2 && line[0] == static_cast<char>(0xff) &&
128-
line[1] == static_cast<char>(0xfe))
127+
line.size() >= 2 && static_cast<unsigned char>(line[0]) == 0xffu &&
128+
static_cast<unsigned char>(line[1]) == 0xfeu)
129129
{
130130
// Unicode, UTF-16 little endian
131131

@@ -150,8 +150,9 @@ void ms_link_cmdlinet::process_response_file(const std::string &file)
150150
#endif
151151
}
152152
else if(
153-
line.size() >= 3 && line[0] == static_cast<char>(0xef) &&
154-
line[1] == static_cast<char>(0xbb) && line[2] == static_cast<char>(0xbf))
153+
line.size() >= 3 && static_cast<unsigned char>(line[0]) == 0xefu &&
154+
static_cast<unsigned char>(line[1]) == 0xbbu &&
155+
static_cast<unsigned char>(line[2]) == 0xbfu)
155156
{
156157
// This is the UTF-8 BOM. We can proceed as usual, since
157158
// we use UTF-8 internally.

src/solvers/smt2/smt2_tokenizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@ void smt2_tokenizert::get_token_from_stream()
225225
case ' ':
226226
case '\r':
227227
case '\t':
228+
#include <util/pragma_push.def>
229+
#ifdef _MSC_VER
230+
#pragma warning(disable : 4309)
231+
// truncation of constant value
232+
#endif
228233
case static_cast<char>(160): // non-breaking space
234+
#include <util/pragma_pop.def>
229235
// skip any whitespace
230236
break;
231237

src/util/config.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
99
#include "config.h"
1010

1111
#include <cstdlib>
12+
#include <limits>
1213

1314
#include "arith_tools.h"
1415
#include "cmdline.h"
@@ -1001,16 +1002,22 @@ bool configt::set(const cmdlinet &cmdline)
10011002
INVARIANT(
10021003
ansi_c.double_width == sizeof(double) * 8,
10031004
"double width shall be equal to the system double width");
1005+
#include <util/pragma_push.def>
1006+
#ifdef _MSC_VER
1007+
#pragma warning(disable : 4309)
1008+
// truncation of constant value
1009+
#endif
10041010
INVARIANT(
10051011
ansi_c.char_is_unsigned == (static_cast<char>(255) == 255),
10061012
"char_is_unsigned flag shall indicate system char unsignedness");
1013+
#include <util/pragma_pop.def>
10071014

1008-
#ifndef _WIN32
1015+
#ifndef _WIN32
10091016
// On Windows, long double width varies by compiler
10101017
INVARIANT(
10111018
ansi_c.long_double_width == sizeof(long double) * 8,
10121019
"long double width shall be equal to the system long double width");
1013-
#endif
1020+
#endif
10141021
}
10151022

10161023
// the following allows overriding the defaults

0 commit comments

Comments
 (0)