safety check to ensure we dont send out beyond the users buffer
Move length check earlier to ensure we don't go beyond the end of the user's buffer. PR#3320