Skip to content

Commit 7e92961

Browse files
committed
Possible crash fix related to nbt string buffer
1 parent d69319f commit 7e92961

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/nbt/nbt.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ namespace nbt {
333333
inline String read_string(input_buffer_ptr file) {
334334
Short s = read_short(file);
335335
nbt_assert_error(exc_env, file, s >= 0, "String specified with invalid length < 0");
336-
uint8_t *str = new uint8_t[s + 1];
337-
assert_error_c(exc_env, file, file->read(str, s) == s, "Buffer to short to read String", delete str);
338-
String so((const char*)str, s);
336+
size_t count = static_cast<size_t>(s);
337+
uint8_t *str = new uint8_t[count + 1];
338+
nbt_assert_error(exc_env, file, str, "Unable to allocate string buffer");
339+
assert_error_c(exc_env, file, file->read(str, count) == count, "Buffer to short to read String", delete [] str);
340+
String so(reinterpret_cast<char*>(str), count);
339341
delete [] str;
340342
return so;
341343
}
@@ -736,7 +738,7 @@ namespace nbt {
736738
parse(file);
737739
}
738740

739-
void parse_file(const char *path)
741+
void parse_file(const char* path)
740742
{
741743
input_buffer_ptr file(new gzfile_buffer(path));
742744
parse(file);

0 commit comments

Comments
 (0)