diff options
| author | YongDo-Hyun <froster12@naver.com> | 2025-12-27 01:16:53 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-03-27 19:57:09 +0300 |
| commit | 7c9c42329e453c0c1e70ec37efcf5375065b3567 (patch) | |
| tree | 59e8567fc5cb9eec85abe19590119f9802b7b8db | |
| parent | df0519bea5390ac8f3d9a0e168e2f799bd068985 (diff) | |
| download | Project-Tick-7c9c42329e453c0c1e70ec37efcf5375065b3567.tar.gz Project-Tick-7c9c42329e453c0c1e70ec37efcf5375065b3567.zip | |
fix: prevent widening of stored tag type in value assignment; update test output handling
Signed-off-by: YongDo-Hyun <froster12@naver.com>
| -rw-r--r-- | src/value.cpp | 9 | ||||
| -rw-r--r-- | test/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | test/format_test.cpp | 8 |
3 files changed, 8 insertions, 10 deletions
diff --git a/src/value.cpp b/src/value.cpp index 273049baf9..f07e28d82c 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -98,17 +98,12 @@ namespace // helper functions local to this translation unit auto incoming_type = detail::get_primitive_type<T>::value; // If the existing tag is of a narrower type than the incoming type, - // replace it with a new tag of the incoming type so the value is not - // truncated. Otherwise set the existing tag (possibly narrowing). + // reject the assignment to avoid widening the stored tag type. auto existing_type = tag_ptr->get_type(); if(static_cast<int>(existing_type) < static_cast<int>(incoming_type)) { - // replace with a new, wider tag that preserves the value - tag_ptr = make_numeric_tag(incoming_type, val); - if(!tag_ptr) - throw std::bad_cast(); - return; + throw std::bad_cast(); } // Existing type is same or wider: write into the existing tag (may narrow) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 41549842cb..500b939d51 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,6 +62,7 @@ build_data(DATA_OBJECTS trailing_data.zlib ) add_library(NbtTestData STATIC ${DATA_OBJECTS}) +set_property(TARGET NbtTestData PROPERTY LINKER_LANGUAGE CXX) #Specifies that the directory containing the testfiles get copied when the target is built function(use_testfiles target) diff --git a/test/format_test.cpp b/test/format_test.cpp index 1a689ed3f2..ed8d2d1bfc 100644 --- a/test/format_test.cpp +++ b/test/format_test.cpp @@ -87,15 +87,17 @@ int main() // Write to file and read back { + tag_compound file_comp = comp; + file_comp.erase("null"); std::ofstream out("test_output.nbt", std::ios::binary); - nbt::io::write_compound(out, comp); + nbt::io::write_tag("root", file_comp, out); } { std::ifstream in("test_output.nbt", std::ios::binary); - auto [read_comp, name] = nbt::io::read_compound(in); + auto read_pair = nbt::io::read_compound(in); std::cout << "----- read back from file:\n"; - std::cout << read_comp; + std::cout << *read_pair.second; std::cout << "\n-----" << std::endl; } |
