From 6d4aeb4120230899589326b5fd87afca58654c05 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 08:03:57 +0100 Subject: feat(assets): Enforce 16-byte alignment and string safety Updates asset_packer to align static asset arrays to 16 bytes and append a null-terminator. This allows assets to be safely reinterpreted as typed pointers (e.g., float*, const char*) without copying. Updates AssetManager documentation to reflect these guarantees. --- tools/asset_packer.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'tools/asset_packer.cc') diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc index 2b90ecc..d86e29b 100644 --- a/tools/asset_packer.cc +++ b/tools/asset_packer.cc @@ -210,7 +210,12 @@ int main(int argc, char* argv[]) { } std::vector buffer((std::istreambuf_iterator(asset_file)), std::istreambuf_iterator()); - fprintf(assets_data_cc_file, "static const uint8_t %s[] = {\n ", + size_t original_size = buffer.size(); + buffer.push_back(0); // Null terminator for safety + + fprintf(assets_data_cc_file, "const size_t ASSET_SIZE_%s = %zu;\n", + info.name.c_str(), original_size); + fprintf(assets_data_cc_file, "alignas(16) static const uint8_t %s[] = {\n ", info.data_array_name.c_str()); for (size_t i = 0; i < buffer.size(); ++i) { if (i > 0 && i % 12 == 0) @@ -241,8 +246,8 @@ int main(int argc, char* argv[]) { info.func_name_str_name.c_str(), info.params_array_name.c_str(), info.proc_params.size()); } else { - fprintf(assets_data_cc_file, "%s, sizeof(%s), false, nullptr, nullptr, 0", - info.data_array_name.c_str(), info.data_array_name.c_str()); + fprintf(assets_data_cc_file, "%s, ASSET_SIZE_%s, false, nullptr, nullptr, 0", + info.data_array_name.c_str(), info.name.c_str()); } fprintf(assets_data_cc_file, " },\n"); } -- cgit v1.2.3