Fixed incorrect string length being stored

This commit is contained in:
calcium 2024-08-24 23:43:47 -04:00
parent 76bf452547
commit 586b2912be

View File

@ -14,31 +14,34 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFor
case Tags::Utf8: { case Tags::Utf8: {
// std::cout << "FIXME: Implement Utf8 Constant Pool Tag" << std::endl; // std::cout << "FIXME: Implement Utf8 Constant Pool Tag" << std::endl;
ConstantUtf8Info ref; ConstantUtf8Info ref;
std::vector<uint8_t> length = Utils::ReadFromStart(Globals::buffer(), 2); ref.length = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
// std::vector<uint8_t> length = Utils::ReadFromStart(Globals::buffer(), 2);
ref.tag = t; ref.tag = t;
ref.length = Utils::vecToVal<uint16_t>(length); // ref.length = Utils::vecToVal<uint16_t>(length);
unsigned char x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
bool valid = true; bool valid = true;
ref.bytes = new unsigned char[ref.length.ToHostFormat()]; unsigned char* bytes = new unsigned char[ref.length.ToHostFormat()];
int len = 0;
for (int i = 0; i < ref.length.ToHostFormat(); i++) for (int i = 0; i < ref.length.ToHostFormat(); i++)
{ {
if ((x & 0x80) == 0) if ((x & 0x80) == 0)
{ {
ref.bytes[i] = x; bytes[i] = x;
} }
else if ((x & 0x800) == 0) else if ((x & 0x800) == 0)
{ {
unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
ref.bytes[i] = ((x & 0x1f) << 6) + (y & 0x3f); bytes[i] = ((x & 0x1f) << 6) + (y & 0x3f);
} }
else if ((x & 0x10000) == 0) else if ((x & 0x10000) == 0)
{ {
unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char y = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
unsigned char z = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); unsigned char z = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
ref.bytes[i] = ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f); bytes[i] = ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f);
} }
else else
{ {
@ -46,11 +49,20 @@ void ConstantPoolTags::ConstantPoolParser(ConstantPoolTags::Tags t, JavaClassFor
exit(1); exit(1);
} }
if (i != ref.length.ToHostFormat() - 1) if (i < ref.length.ToHostFormat() - 1)
x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1); x = Utils::ReadFromStartIntoVal<unsigned char>(Globals::buffer(), 1);
len += 1;
} }
std::string bytes1;
bytes1.append(reinterpret_cast<const char*>(bytes), 0, len);
ref.bytes = bytes1;
jc.constant_pool.push_back(ref); jc.constant_pool.push_back(ref);
ref.length = len;
break; break;
} }
case Tags::Integer: case Tags::Integer: