Skip to content

Commit 523a6b4

Browse files
committed
Enforced unsigned data-streams
Fixed Y-index crash from empty section stack
1 parent 7e92961 commit 523a6b4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/mc/level.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ namespace mc {
714714

715715
C->p[C->pos++] = section_name::None;
716716

717-
if (in_level_section(C)) {
717+
if (in_level_section_a(C) || in_level_section_c(C)) {
718718
C->sections.emplace_back();
719719
} else if(in_palette_section(C)) {
720720
C->sections.back().PaletteProperties.emplace_back();
@@ -914,7 +914,7 @@ namespace mc {
914914
* indice_bit_count is the number of bits in the slice and may not exceed 32 since the return type is int.
915915
*/
916916
template<typename T, size_t indice_bit_count>
917-
inline boost::optional<int> get_indice(int x, int z, int y, boost::shared_ptr<nbt::Array<T>> &arr) {
917+
inline boost::optional<uint32_t> get_indice(int x, int z, int y, boost::shared_ptr<nbt::Array<T>> &arr) {
918918
size_t indice_index = (y * 16 + z) * 16 + x;
919919

920920
// Compiler should catch that this is static and pre-compute it,
@@ -927,11 +927,11 @@ namespace mc {
927927
size_t element_index = (indice_index - index_in_element) / indice_count_in_element;
928928

929929
if (arr->length < 0 || element_index >= static_cast<size_t>(arr->length))
930-
return boost::optional<int>();
930+
return boost::optional<uint32_t>();
931931

932932
T element = arr->values[element_index];
933-
int out_bits = ~(0xFFFFFFFF << indice_bit_count);
934-
return boost::optional<int>((element >> (indice_bit_count * index_in_element)) & out_bits);
933+
uint32_t out_bits = ~(0xFFFFFFFF << indice_bit_count);
934+
return boost::optional<uint32_t>((element >> (indice_bit_count * index_in_element)) & out_bits);
935935
}
936936

937937
/**
@@ -943,7 +943,7 @@ namespace mc {
943943
* indice_bit_count is the number of bits in the slice and may not exceed 32 since the return type is int.
944944
*/
945945
template<typename T, size_t indice_bit_count>
946-
inline boost::optional<int> get_stream_indice(int x, int z, int y, boost::shared_ptr<nbt::Array<T>> &arr) {
946+
inline boost::optional<uint32_t> get_stream_indice(int x, int z, int y, boost::shared_ptr<nbt::Array<T>> &arr) {
947947
size_t indice_index = (y * 16 + z) * 16 + x;
948948

949949
size_t bits_into_stream = indice_index * indice_bit_count;
@@ -953,26 +953,26 @@ namespace mc {
953953
size_t element_index = (bits_into_stream - bits_in_element) / element_bits;
954954

955955
if (arr->length < 0 || element_index >= static_cast<size_t>(arr->length))
956-
return boost::optional<int>();
956+
return boost::optional<uint32_t>();
957957

958958
T element = arr->values[element_index];
959959
int result = element >> bits_in_element;
960960
size_t got = element_bits - bits_in_element;
961961
if (got < indice_bit_count) {
962962
if (element_index + 1 >= static_cast<size_t>(arr->length))
963-
return boost::optional<int>();
963+
return boost::optional<uint32_t>();
964964
element = arr->values[element_index + 1];
965965
int got_bits = ~(0xFFFFFFFF << got);
966966
result = (result & got_bits) | (element << got);
967967
}
968968

969-
int out_bits = ~(0xFFFFFFFF << indice_bit_count);
970-
return boost::optional<int>(result & out_bits);
969+
uint32_t out_bits = ~(0xFFFFFFFF << indice_bit_count);
970+
return boost::optional<uint32_t>(result & out_bits);
971971
}
972972

973973
bool Modern_Section_Compound::get_block(BlockT& block, int x, int z, int y) {
974974
if (this->BlockStates) {
975-
boost::optional<int> block_data = boost::optional<int>();
975+
boost::optional<uint32_t> block_data = boost::optional<uint32_t>();
976976

977977
// Static expansion of dynamic palette resolver;
978978
// each section (16**3) contains 4096 unique blocks wihch yields max 2**12
@@ -1036,11 +1036,11 @@ namespace mc {
10361036
}
10371037

10381038
bool Legacy_Section_Compound::get_block(BlockT& block, int x, int z, int y) {
1039-
boost::optional<int> lower_block_type;
1040-
boost::optional<int> block_type = get_indice<nbt::Byte, 8>(x, z, y, this->Blocks);
1039+
boost::optional<uint32_t> lower_block_type;
1040+
boost::optional<uint32_t> block_type = get_indice<nbt::Byte, 8>(x, z, y, this->Blocks);
10411041
// Data values are packed two by two and the position LSB decides which
10421042
// half-byte contains the requested block data value.
1043-
boost::optional<int> block_data = get_indice<nbt::Byte, 4>(x, z, y, this->Data);
1043+
boost::optional<uint32_t> block_data = get_indice<nbt::Byte, 4>(x, z, y, this->Data);
10441044

10451045
if (block_type && block_data) {
10461046
boost::optional<MaterialT*> material = get_material_legacy(block_type.get(), block_data.get());

0 commit comments

Comments
 (0)