Add boundary checking for string manipulation functions