security: add a NULL pointer check to fix a server crash

based on 7d58aac24fe20ffaad7bd9b40c9ddf457c1b06e7