Because the path you have provided contains the null character, which has the numeric code of 0.
You can see the assertion second from the top of the stack trace.
This is because bufPrint inserts at the start of the buffer and doesn’t touch the rest of it.
And you are passing the entire buffer to createFile.
Simply use the success result of bufPrint it’s a slice that points to just the part of the buffer that contains what it printed.
Your catch on bufPrint is also highly questionable, I would recommend one of the following:
The reason text shouldn’t contain the null character (0) is because It’s used to indicate the end of text, when you don’t have a length, for example when giving a file name to the OS to open/create it.
This is a holdover from when memory was more limited, it’s still common practice in c.
ah thank you very much, I was looking at that assertion and I did NOT understand it because I thought having a 0 at the end was the whole point (I come from C, I was about to handcode some kind of memcpy solution)
what if I want to truncate the name however ? If I read things correctly, the only possible error is NoSpaceLeft, in which case as much is written as possible, so it should be correct although it looks disgusting, I am open to suggestions
log.info prints all the 100 characters in path, most of them are zero bytes and are invisible.
When createFile tries to create the file, it detects that path contains zero bytes that are invalid characters for filenames.
The problem is that in the code, the return value of bufPrint is ignored, but this is a slice (a pair of pointer and length) with the correct length of the path written in buffer.
zig uses slices which are pointer + length, the std library prioritises the length property over null termination even when in this case it would work as its being lowered to the OS assuming your OS uses null termination when passing text to it, but it could result in unexpected behaviour so zig warns you.
I argue that you generally shouldn’t rely on the state of data under a failure condition. As that could, and IMO this case should, change.