aboutsummaryrefslogtreecommitdiff
path: root/tests/test_safetensors.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:35:54 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:35:54 +0200
commit4f743e18aecda8f5ebf25b400452bd4c3409e4aa (patch)
tree2112bef88bf2e003dfdb5bfb53c8fd8d3bfff50c /tests/test_safetensors.c
parente00becb643b3a99de16462b3a032717a93b983d9 (diff)
downloadffwd-4f743e18aecda8f5ebf25b400452bd4c3409e4aa.tar.gz
ffwd-4f743e18aecda8f5ebf25b400452bd4c3409e4aa.zip
tests: align fixture safetensors headers to 8 bytes
Diffstat (limited to 'tests/test_safetensors.c')
-rw-r--r--tests/test_safetensors.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c
index 607bfc5..1cfafd5 100644
--- a/tests/test_safetensors.c
+++ b/tests/test_safetensors.c
@@ -22,12 +22,19 @@ write_st(const char *path, const char *header, const void *payload, size_t paylo
perror(path);
exit(2);
}
+ /* Pad the header to an 8-byte boundary with trailing spaces so the data
+ * section is 8-aligned, matching the real safetensors serializer; otherwise
+ * tensor data lands unaligned and typed reads of it are UBSan-flagged UB. */
uint64_t n = strlen(header);
+ uint64_t pad = (8 - (n % 8)) % 8;
+ uint64_t total = n + pad;
unsigned char len8[8];
for (int i = 0; i < 8; i++)
- len8[i] = (unsigned char)(n >> (8 * i));
+ len8[i] = (unsigned char)(total >> (8 * i));
fwrite(len8, 1, 8, f);
fwrite(header, 1, n, f);
+ for (uint64_t i = 0; i < pad; i++)
+ fputc(' ', f);
if (payload_len)
fwrite(payload, 1, payload_len, f);
fclose(f);