Skip to content

Commit debf665

Browse files
committed
Cleanup type conversions in java_bytecode_parsert::read
We can safely read signed values and don't need to defer the type conversion to the call site.
1 parent 66004dc commit debf665

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Diff for: jbmc/src/java_bytecode/java_bytecode_parser.cpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ class java_bytecode_parsert final : public parsert
132132
T read()
133133
{
134134
static_assert(
135-
std::is_unsigned<T>::value, "T should be an unsigned integer");
135+
std::is_unsigned<T>::value || std::is_signed<T>::value,
136+
"T should be a signed or unsigned integer");
136137
const constexpr size_t bytes = sizeof(T);
137-
u8 result = 0;
138+
T result = 0;
138139
for(size_t i = 0; i < bytes; i++)
139140
{
140141
if(!*in)
@@ -145,7 +146,7 @@ class java_bytecode_parsert final : public parsert
145146
result <<= 8u;
146147
result |= static_cast<u1>(in->get());
147148
}
148-
return narrow_cast<T>(result);
149+
return result;
149150
}
150151

151152
void store_unknown_method_handle(size_t bootstrap_method_index);
@@ -700,7 +701,7 @@ void java_bytecode_parsert::rconstant_pool()
700701
std::string s;
701702
s.resize(bytes);
702703
for(auto &ch : s)
703-
ch = read<u1>();
704+
ch = read<std::string::value_type>();
704705
it->s = s; // Add to string table
705706
}
706707
break;
@@ -942,15 +943,15 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
942943

943944
case 'b': // a signed byte
944945
{
945-
const s1 c = read<u1>();
946+
const s1 c = read<s1>();
946947
instruction.args.push_back(from_integer(c, signedbv_typet(8)));
947948
}
948949
address+=1;
949950
break;
950951

951952
case 'o': // two byte branch offset, signed
952953
{
953-
const s2 offset = read<u2>();
954+
const s2 offset = read<s2>();
954955
// By converting the signed offset into an absolute address (by adding
955956
// the current address) the number represented becomes unsigned.
956957
instruction.args.push_back(
@@ -961,7 +962,7 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
961962

962963
case 'O': // four byte branch offset, signed
963964
{
964-
const s4 offset = read<u4>();
965+
const s4 offset = read<s4>();
965966
// By converting the signed offset into an absolute address (by adding
966967
// the current address) the number represented becomes unsigned.
967968
instruction.args.push_back(
@@ -994,15 +995,15 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
994995
{
995996
const u2 v = read<u2>();
996997
instruction.args.push_back(from_integer(v, unsignedbv_typet(16)));
997-
const s2 c = read<u2>();
998+
const s2 c = read<s2>();
998999
instruction.args.push_back(from_integer(c, signedbv_typet(16)));
9991000
address+=4;
10001001
}
10011002
else // local variable index (one byte) plus one signed byte
10021003
{
10031004
const u1 v = read<u1>();
10041005
instruction.args.push_back(from_integer(v, unsignedbv_typet(8)));
1005-
const s1 c = read<u1>();
1006+
const s1 c = read<s1>();
10061007
instruction.args.push_back(from_integer(c, signedbv_typet(8)));
10071008
address+=2;
10081009
}
@@ -1032,7 +1033,7 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
10321033
}
10331034

10341035
// now default value
1035-
const s4 default_value = read<u4>();
1036+
const s4 default_value = read<s4>();
10361037
// By converting the signed offset into an absolute address (by adding
10371038
// the current address) the number represented becomes unsigned.
10381039
instruction.args.push_back(
@@ -1045,8 +1046,8 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
10451046

10461047
for(std::size_t i=0; i<npairs; i++)
10471048
{
1048-
const s4 match = read<u4>();
1049-
const s4 offset = read<u4>();
1049+
const s4 match = read<s4>();
1050+
const s4 offset = read<s4>();
10501051
instruction.args.push_back(
10511052
from_integer(match, signedbv_typet(32)));
10521053
// By converting the signed offset into an absolute address (by adding
@@ -1070,23 +1071,23 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
10701071
}
10711072

10721073
// now default value
1073-
const s4 default_value = read<u4>();
1074+
const s4 default_value = read<s4>();
10741075
instruction.args.push_back(
10751076
from_integer(base_offset+default_value, signedbv_typet(32)));
10761077
address+=4;
10771078

10781079
// now low value
1079-
const s4 low_value = read<u4>();
1080+
const s4 low_value = read<s4>();
10801081
address+=4;
10811082

10821083
// now high value
1083-
const s4 high_value = read<u4>();
1084+
const s4 high_value = read<s4>();
10841085
address+=4;
10851086

10861087
// there are high-low+1 offsets, and they are signed
10871088
for(s4 i=low_value; i<=high_value; i++)
10881089
{
1089-
s4 offset = read<u4>();
1090+
s4 offset = read<s4>();
10901091
instruction.args.push_back(from_integer(i, signedbv_typet(32)));
10911092
// By converting the signed offset into an absolute address (by adding
10921093
// the current address) the number represented becomes unsigned.
@@ -1130,7 +1131,7 @@ void java_bytecode_parsert::rbytecode(std::vector<instructiont> &instructions)
11301131

11311132
case 's': // a signed short
11321133
{
1133-
const s2 s = read<u2>();
1134+
const s2 s = read<s2>();
11341135
instruction.args.push_back(from_integer(s, signedbv_typet(16)));
11351136
}
11361137
address+=2;

0 commit comments

Comments
 (0)