use memrchr, strndup, and strnlen modules more correctly