Skip to content

Commit 3b64b94

Browse files
committed
Do not assume that byte/char==8 bits
The C/C++ standard does not guarantee this, and limits.h/climits has the CHAR_BIT define set properly.
1 parent ed79565 commit 3b64b94

File tree

11 files changed

+38
-27
lines changed

11 files changed

+38
-27
lines changed

src/ansi-c/literals/convert_character_literal.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Author: Daniel Kroening, kroening@kroening.com
77
\*******************************************************************/
88

99
#include <cassert>
10+
#include <climits>
1011

1112
#include <util/arith_tools.h>
1213
#include <util/std_expr.h>
@@ -62,7 +63,7 @@ exprt convert_character_literal(
6263
for(unsigned i=0; i<value.size(); i++)
6364
{
6465
mp_integer z=(unsigned char)(value[i]);
65-
z=z<<((value.size()-i-1)*8);
66+
z=z<<((value.size()-i-1)*CHAR_BIT);
6667
x+=z;
6768
}
6869

@@ -95,7 +96,7 @@ exprt convert_character_literal(
9596
for(unsigned i=0; i<value.size(); i++)
9697
{
9798
mp_integer z=(unsigned char)(value[i]);
98-
z=z<<((value.size()-i-1)*8);
99+
z=z<<((value.size()-i-1)*CHAR_BIT);
99100
x+=z;
100101
}
101102

src/cbmc/cbmc_parse_options.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <fstream>
1011
#include <cstdlib> // exit()
1112
#include <iostream>
@@ -474,7 +475,7 @@ int cbmc_parse_optionst::doit()
474475
// Print a banner
475476
//
476477
status() << "CBMC version " CBMC_VERSION " "
477-
<< sizeof(void *)*8 << "-bit "
478+
<< sizeof(void *)*CHAR_BIT << "-bit "
478479
<< config.this_architecture() << " "
479480
<< config.this_operating_system() << eom;
480481

@@ -1047,7 +1048,7 @@ void cbmc_parse_optionst::help()
10471048
"\n"
10481049
"* * CBMC " CBMC_VERSION " - Copyright (C) 2001-2016 ";
10491050

1050-
std::cout << "(" << (sizeof(void *)*8) << "-bit version)";
1051+
std::cout << "(" << (sizeof(void *)*CHAR_BIT) << "-bit version)";
10511052

10521053
std::cout << " * *\n";
10531054

src/clobber/clobber_parse_options.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <iostream>
1011
#include <fstream>
1112
#include <cstdlib>
@@ -631,7 +632,7 @@ void clobber_parse_optionst::help()
631632
"\n"
632633
"* * CLOBBER " CBMC_VERSION " - Copyright (C) 2014 ";
633634

634-
std::cout << "(" << (sizeof(void *)*8) << "-bit version)";
635+
std::cout << "(" << (sizeof(void *)*CHAR_BIT) << "-bit version)";
635636

636637
std::cout << " * *\n";
637638

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <cstdlib> // exit()
1011
#include <iostream>
1112
#include <fstream>
@@ -208,7 +209,7 @@ int goto_analyzer_parse_optionst::doit()
208209
// Print a banner
209210
//
210211
status() << "GOTO-ANALYSER version " CBMC_VERSION " "
211-
<< sizeof(void *)*8 << "-bit "
212+
<< sizeof(void *)*CHAR_BIT << "-bit "
212213
<< config.this_architecture() << " "
213214
<< config.this_operating_system() << eom;
214215

@@ -520,7 +521,7 @@ void goto_analyzer_parse_optionst::help()
520521
"\n"
521522
"* * GOTO-ANALYSER " CBMC_VERSION " - Copyright (C) 2016 ";
522523

523-
std::cout << "(" << (sizeof(void *)*8) << "-bit version)";
524+
std::cout << "(" << (sizeof(void *)*CHAR_BIT) << "-bit version)";
524525

525526
std::cout << " * *\n";
526527

src/goto-cc/ms_cl_cmdline.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Author: Daniel Kroening
77
\*******************************************************************/
88

99
#include <cassert>
10+
#include <climits>
1011
#include <cstring>
1112
#include <cstdlib>
1213
#include <iostream>
@@ -199,7 +200,7 @@ static std::istream &my_wgetline(std::istream &in, std::wstring &dest)
199200
break; // line end
200201
}
201202
else
202-
dest+=wchar_t(ch1+(ch2<<8));
203+
dest+=wchar_t(ch1+(ch2<<CHAR_BIT));
203204
}
204205

205206
return in;

src/goto-diff/goto_diff_parse_options.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Peter Schrammel
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <fstream>
1011
#include <cstdlib> // exit()
1112
#include <iostream>
@@ -312,7 +313,7 @@ int goto_diff_parse_optionst::doit()
312313
// Print a banner
313314
//
314315
status() << "GOTO-DIFF version " CBMC_VERSION " "
315-
<< sizeof(void *)*8 << "-bit "
316+
<< sizeof(void *)*CHAR_BIT << "-bit "
316317
<< config.this_architecture() << " "
317318
<< config.this_operating_system() << eom;
318319

src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Author: Daniel Kroening, kroening@kroening.com
77
\*******************************************************************/
88

99
#include <algorithm>
10+
#include <climits>
1011
#include <fstream>
1112
#include <map>
1213
#include <string>
@@ -148,7 +149,7 @@ class java_bytecode_parsert:public parsert
148149
error() << "unexpected end of bytecode file" << eom;
149150
throw 0;
150151
}
151-
result<<=8;
152+
result<<=CHAR_BIT;
152153
result|=in->get();
153154
}
154155
return result;

src/symex/symex_parse_options.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <iostream>
1011
#include <fstream>
1112
#include <cstdlib>
@@ -668,7 +669,7 @@ void symex_parse_optionst::help()
668669
"\n"
669670
"* * Symex " CBMC_VERSION " - Copyright (C) 2013 ";
670671

671-
std::cout << "(" << (sizeof(void *)*8) << "-bit version)";
672+
std::cout << "(" << (sizeof(void *)*CHAR_BIT) << "-bit version)";
672673

673674
std::cout << " * *\n";
674675

src/util/config.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <climits>
910
#include <cstdlib>
1011

1112
#include "namespace.h"
@@ -1234,20 +1235,20 @@ bool configt::set(const cmdlinet &cmdline)
12341235
// the same architecture and OS that we are verifying for.
12351236
if(arch==this_arch && os==this_os)
12361237
{
1237-
assert(ansi_c.int_width==sizeof(int)*8);
1238-
assert(ansi_c.long_int_width==sizeof(long)*8);
1239-
assert(ansi_c.bool_width==sizeof(bool)*8);
1240-
assert(ansi_c.char_width==sizeof(char)*8);
1241-
assert(ansi_c.short_int_width==sizeof(short)*8);
1242-
assert(ansi_c.long_long_int_width==sizeof(long long)*8);
1243-
assert(ansi_c.pointer_width==sizeof(void *)*8);
1244-
assert(ansi_c.single_width==sizeof(float)*8);
1245-
assert(ansi_c.double_width==sizeof(double)*8);
1238+
assert(ansi_c.int_width==sizeof(int)*CHAR_BIT);
1239+
assert(ansi_c.long_int_width==sizeof(long)*CHAR_BIT);
1240+
assert(ansi_c.bool_width==sizeof(bool)*CHAR_BIT);
1241+
assert(ansi_c.char_width==sizeof(char)*CHAR_BIT);
1242+
assert(ansi_c.short_int_width==sizeof(short)*CHAR_BIT);
1243+
assert(ansi_c.long_long_int_width==sizeof(long long)*CHAR_BIT);
1244+
assert(ansi_c.pointer_width==sizeof(void *)*CHAR_BIT);
1245+
assert(ansi_c.single_width==sizeof(float)*CHAR_BIT);
1246+
assert(ansi_c.double_width==sizeof(double)*CHAR_BIT);
12461247
assert(ansi_c.char_is_unsigned==(static_cast<char>(255)==255));
12471248

12481249
#ifndef _WIN32
12491250
// On Windows, long double width varies by compiler
1250-
assert(ansi_c.long_double_width==sizeof(long double)*8);
1251+
assert(ansi_c.long_double_width==sizeof(long double)*CHAR_BIT);
12511252
#endif
12521253
}
12531254

src/util/irep_hash.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Author: Michael Tautschnig, mt@eecs.qmul.ac.uk
3232
// MURMURHASH2A compares most favourably on String6 with 3076 fewer
3333
// calls (2.9%)
3434

35+
#include <climits>
3536
#include <cstddef> // std::size_t
3637

3738
#ifdef _MSC_VER
@@ -151,7 +152,7 @@ inline std::size_t basic_hash_finalize(
151152
// Boost uses the symbol hash_combine, if you're getting problems here then
152153
// you've probably included a Boost header after this one
153154
#define hash_combine(h1, h2) \
154-
basic_hash_combine<sizeof(std::size_t)*8>(h1, h2)
155+
basic_hash_combine<sizeof(std::size_t)*CHAR_BIT>(h1, h2)
155156
#define hash_finalize(h1, len) \
156157
basic_hash_finalize(h1, len)
157158

@@ -304,9 +305,9 @@ inline std::size_t murmurhash2a_hash_finalize<64>(
304305
}
305306

306307
#define hash_combine(h1, h2) \
307-
murmurhash2a_hash_combine<sizeof(std::size_t)*8>(h1, h2)
308+
murmurhash2a_hash_combine<sizeof(std::size_t)*CHAR_BIT>(h1, h2)
308309
#define hash_finalize(h1, len) \
309-
murmurhash2a_hash_finalize<sizeof(std::size_t)*8>(h1, len)
310+
murmurhash2a_hash_finalize<sizeof(std::size_t)*CHAR_BIT>(h1, len)
310311

311312
#endif
312313

@@ -466,9 +467,9 @@ inline std::size_t murmurhash3_hash_finalize<64>(
466467
}
467468

468469
#define hash_combine(h1, h2) \
469-
murmurhash3_hash_combine<sizeof(std::size_t)*8>(h1, h2)
470+
murmurhash3_hash_combine<sizeof(std::size_t)*CHAR_BIT>(h1, h2)
470471
#define hash_finalize(h1, len) \
471-
murmurhash3_hash_finalize<sizeof(std::size_t)*8>(h1, len)
472+
murmurhash3_hash_finalize<sizeof(std::size_t)*CHAR_BIT>(h1, len)
472473

473474
#endif
474475

0 commit comments

Comments
 (0)