This line changes `str`, so `sp` on the next two lines may point to garbage.