From 14d4e91b8204f148915509f38a585bef11373482 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 20 Jul 2024 19:41:20 -0600 Subject: [PATCH] common: fix ASan error on non-null-terminated (16 char long) texture names --- common/fs.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/common/fs.cc b/common/fs.cc index 2f98216e..cfc37618 100644 --- a/common/fs.cc +++ b/common/fs.cc @@ -159,8 +159,24 @@ struct wad_archive : archive_like uint8_t compression; padding<2> pad; std::array name; // must be null terminated + // NOTE: textures using all 16 exist in the wild, e.g. openquartzmirror + // in free_wad.wad auto stream_data() { return std::tie(filepos, disksize, size, type, compression, pad, name); } + + std::string name_as_string() const { + size_t length = 0; + + // count the number of leading non-null characters + for (int i = 0; i < 16; ++i) { + if (name[i] != 0) + ++length; + else + break; + } + + return std::string(name.data(), length); + } }; std::unordered_map, case_insensitive_hash, case_insensitive_equal> @@ -189,7 +205,11 @@ struct wad_archive : archive_like wadstream >= file; - files[file.name.data()] = std::make_tuple(file.filepos, file.disksize); + std::string tex_name = file.name_as_string(); + if (tex_name.size() == 16) { + logging::print("WARNING: texture name {} ({}) is not null-terminated\n", tex_name, pathname); + } + files[tex_name] = std::make_tuple(file.filepos, file.disksize); } }