diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp
index f3a8b3e679ee75defa94b16ce19234823f4d1ee9..e4c04aeb8c4309a352831181c2861d09e7df9557 100644
--- a/llvm/keystone/ks.cpp
+++ b/llvm/keystone/ks.cpp
@@ -375,7 +375,8 @@ ks_err ks_open(ks_arch arch, int mode, ks_engine **result)
                         TripleName = "ppc64";
                 } else {    // little endian
                     if (mode & KS_MODE_PPC32) {
-                        // do not suppor this mode
+                        // do not support this mode
+                        free(ks);
                         return KS_ERR_MODE;
                     }
                     if (mode & KS_MODE_MIPS64)
@@ -423,6 +424,7 @@ ks_err ks_open(ks_arch arch, int mode, ks_engine **result)
 
         if (TripleName.empty()) {
             // this arch is not supported
+            free(ks);
             return KS_ERR_ARCH;
         }