This is still an issue. An this is a fix, because we need to be sure that there is a 0 termination only on the very end: 
tmp[max_length] = 0;