From 230962230f40a22f9364d24fad636ba8185c5426 Mon Sep 17 00:00:00 2001
From: XMRig <support@xmrig.com>
Date: Wed, 25 Apr 2018 22:03:26 +0700
Subject: [PATCH] Better algorithm validation.

---
 src/common/crypto/Algorithm.cpp | 16 ++++++++++++++++
 src/common/crypto/Algorithm.h   |  2 +-
 src/workers/CpuThread.cpp       | 13 ++++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp
index 2e13fffd5..8d95acde8 100644
--- a/src/common/crypto/Algorithm.cpp
+++ b/src/common/crypto/Algorithm.cpp
@@ -79,6 +79,22 @@ static const char *variants[] = {
 };
 
 
+bool xmrig::Algorithm::isValid() const
+{
+    if (m_algo == INVALID_ALGO) {
+        return false;
+    }
+
+    for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) {
+        if (algorithms[i].algo == m_algo && algorithms[i].variant == m_variant) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+
 const char *xmrig::Algorithm::variantName() const
 {
     if (m_variant == VARIANT_AUTO) {
diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h
index bbabb40fc..aff7a8c87 100644
--- a/src/common/crypto/Algorithm.h
+++ b/src/common/crypto/Algorithm.h
@@ -48,7 +48,6 @@ public:
 
     bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; }
     inline Algo algo() const                   { return m_algo; }
-    inline bool isValid() const                { return m_algo != INVALID_ALGO; }
     inline const char *name() const            { return name(false); }
     inline const char *shortName() const       { return name(true); }
     inline Variant variant() const             { return m_variant; }
@@ -57,6 +56,7 @@ public:
     inline bool operator!=(const Algorithm &other) const  { return !isEqual(other); }
     inline bool operator==(const Algorithm &other) const  { return isEqual(other); }
 
+    bool isValid() const;
     const char *variantName() const;
     void parseAlgorithm(const char *algo);
     void parseVariant(const char *variant);
diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp
index e08ffecd7..86699afc7 100644
--- a/src/workers/CpuThread.cpp
+++ b/src/workers/CpuThread.cpp
@@ -164,7 +164,18 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
     }
 #   endif
 
-    return func_table[40 * algorithm + 10 * variant + av - 1];
+    const size_t index = 40 * algorithm + 10 * variant + av - 1;
+
+#   ifndef NDEBUG
+    cn_hash_fun func = func_table[index];
+
+    assert(index < sizeof(func_table) / sizeof(func_table[0]));
+    assert(func != nullptr);
+
+    return func;
+#   else
+    return func_table[index];
+#   endif
 }