From 4a01b6a00d7231bc8239db2bd5902dd71d8198a9 Mon Sep 17 00:00:00 2001 From: Patryk Obara Date: Sat, 2 Nov 2019 14:59:27 +0100 Subject: [PATCH] Prevent potential null pointer dereference This null-check resolves a potential issue detected by static analysis. It might be the case, that crash never happens due to the way this static function is used at the moment, and because DYNFLG_CHANGED is being cleared in dnew->genreg->Clear() few lines before, but the crash might still happen if initial state of flags is inconsistent or surrounding code will be changed even a little bit. This nullcheck makes the code more robust at no performance penalty. --- src/cpu/core_dyn_x86/risc_x64.h | 3 ++- src/cpu/core_dyn_x86/risc_x86.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cpu/core_dyn_x86/risc_x64.h b/src/cpu/core_dyn_x86/risc_x64.h index d6baa47a..4fea48ff 100644 --- a/src/cpu/core_dyn_x86/risc_x64.h +++ b/src/cpu/core_dyn_x86/risc_x64.h @@ -439,7 +439,8 @@ static void gen_synchreg(DynReg * dnew,DynReg * dsynch) { if ((dnew->flags ^ dsynch->flags) & DYNFLG_CHANGED) { /* Ensure the changed value gets saved */ if (dnew->flags & DYNFLG_CHANGED) { - dnew->genreg->Save(); + if (GCC_LIKELY(dnew->genreg != NULL)) + dnew->genreg->Save(); } else dnew->flags|=DYNFLG_CHANGED; } } diff --git a/src/cpu/core_dyn_x86/risc_x86.h b/src/cpu/core_dyn_x86/risc_x86.h index 989cc4ba..51b2fe5a 100644 --- a/src/cpu/core_dyn_x86/risc_x86.h +++ b/src/cpu/core_dyn_x86/risc_x86.h @@ -233,7 +233,8 @@ static void gen_synchreg(DynReg * dnew,DynReg * dsynch) { if ((dnew->flags ^ dsynch->flags) & DYNFLG_CHANGED) { /* Ensure the changed value gets saved */ if (dnew->flags & DYNFLG_CHANGED) { - dnew->genreg->Save(); + if (GCC_LIKELY(dnew->genreg != NULL)) + dnew->genreg->Save(); } else dnew->flags|=DYNFLG_CHANGED; } }