Why -1 here?  The strlen() returns length not including final \0.  So I think you actually want a +1 for a final \0.  Am I thinking wrong?