Skip to content

Commit

Permalink
common: fix ASan error on non-null-terminated (16 char long) texture …
Browse files Browse the repository at this point in the history
…names
  • Loading branch information
ericwa committed Jul 21, 2024
1 parent 0215d52 commit 14d4e91
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion common/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,24 @@ struct wad_archive : archive_like
uint8_t compression;
padding<2> pad;
std::array<char, 16> 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<std::string, std::tuple<uint32_t, uint32_t>, case_insensitive_hash, case_insensitive_equal>
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 14d4e91

Please sign in to comment.