Your problem is here:
buf->str[ buflen ] = '\0';
You allocate a buffer (str) of buflen bytes.
This means you can index it (str) from 0 -> (buflen-1).
Thus the accesses above is writing one past the end of the buffer and corrupting memory.
You probably meant:
buf->str[ len ] = '\0';
Since you are using C++ compiler. You should use std::vector<viByte>.
This kind of bug is easy to introduce and unless you have good unit test hard to find. So why not use a class that already is unit test and has had 10 years of millions of people lookign at it to make sure it works correctly.