From df0519bea5390ac8f3d9a0e168e2f799bd068985 Mon Sep 17 00:00:00 2001 From: YongDo-Hyun Date: Sat, 27 Dec 2025 01:06:26 +0300 Subject: feat: enhance error handling in ozlibstream and improve JSON escaping in json_formatter; add make_numeric_tag for cleaner tag creation Signed-off-by: YongDo-Hyun --- src/io/ozlibstream.cpp | 2 +- src/text/json_formatter.cpp | 2 +- src/value.cpp | 41 +++++++++++++++++++++-------------------- 3 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp index b936219787..5f479f83f8 100644 --- a/src/io/ozlibstream.cpp +++ b/src/io/ozlibstream.cpp @@ -95,6 +95,7 @@ int deflate_streambuf::sync() void ozlibstream::close() { + std::ios_base::iostate old_ex = exceptions(); try { buf.close(); @@ -105,7 +106,6 @@ void ozlibstream::close() // `setstate` to throw an `ios_base::failure` which would replace // the original exception. Temporarily disable exceptions on this // stream, set the `badbit`, then restore the exception mask. - std::ios_base::iostate old_ex = exceptions(); try { exceptions(std::ios_base::goodbit); setstate(std::ios_base::badbit); diff --git a/src/text/json_formatter.cpp b/src/text/json_formatter.cpp index bd882c15d2..06a9c54f60 100644 --- a/src/text/json_formatter.cpp +++ b/src/text/json_formatter.cpp @@ -217,7 +217,7 @@ namespace //anonymous default: if (static_cast(c) < 32 || c == 127) { // Control characters, escape as \u00XX - os << "\\u00" << std::hex << std::setw(2) << std::setfill('0') << static_cast(static_cast(c)); + os << "\\u00" << std::hex << std::setw(2) << std::setfill('0') << static_cast(c); } else { os << c; } diff --git a/src/value.cpp b/src/value.cpp index 8c16da6e23..273049baf9 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -66,6 +66,21 @@ void value::set(tag&& t) //Primitive assignment namespace // helper functions local to this translation unit { + template + std::unique_ptr make_numeric_tag(tag_type type, T val) + { + switch(type) + { + case tag_type::Byte: return std::unique_ptr(new tag_byte(val)); + case tag_type::Short: return std::unique_ptr(new tag_short(val)); + case tag_type::Int: return std::unique_ptr(new tag_int(val)); + case tag_type::Long: return std::unique_ptr(new tag_long(val)); + case tag_type::Float: return std::unique_ptr(new tag_float(val)); + case tag_type::Double: return std::unique_ptr(new tag_double(val)); + default: return nullptr; + } + } + template void assign_numeric_impl(std::unique_ptr& tag_ptr, T val, tag_type default_type) @@ -73,16 +88,9 @@ namespace // helper functions local to this translation unit using nbt::tag_type; if(!tag_ptr) { - switch(default_type) - { - case tag_type::Byte: tag_ptr.reset(new tag_byte(static_cast(val))); break; - case tag_type::Short: tag_ptr.reset(new tag_short(static_cast(val))); break; - case tag_type::Int: tag_ptr.reset(new tag_int(static_cast(val))); break; - case tag_type::Long: tag_ptr.reset(new tag_long(static_cast(val))); break; - case tag_type::Float: tag_ptr.reset(new tag_float(static_cast(val))); break; - case tag_type::Double: tag_ptr.reset(new tag_double(static_cast(val))); break; - default: throw std::invalid_argument("Invalid default_type"); - } + tag_ptr = make_numeric_tag(default_type, val); + if(!tag_ptr) + throw std::invalid_argument("Invalid default_type"); return; } @@ -97,16 +105,9 @@ namespace // helper functions local to this translation unit if(static_cast(existing_type) < static_cast(incoming_type)) { // replace with a new, wider tag that preserves the value - switch(incoming_type) - { - case tag_type::Byte: tag_ptr.reset(new tag_byte(static_cast(val))); break; - case tag_type::Short: tag_ptr.reset(new tag_short(static_cast(val))); break; - case tag_type::Int: tag_ptr.reset(new tag_int(static_cast(val))); break; - case tag_type::Long: tag_ptr.reset(new tag_long(static_cast(val))); break; - case tag_type::Float: tag_ptr.reset(new tag_float(static_cast(val))); break; - case tag_type::Double: tag_ptr.reset(new tag_double(static_cast(val))); break; - default: throw std::bad_cast(); - } + tag_ptr = make_numeric_tag(incoming_type, val); + if(!tag_ptr) + throw std::bad_cast(); return; } -- cgit 0.0.5-2-1-g0f52