Fix: incorrect length calculation to allocate a string, leading to segfault