From 05a82b15e62eb03f68323cb1577506a577fbb9d2 Mon Sep 17 00:00:00 2001
From: zhupengfei <zhupf321@gmail.com>
Date: Tue, 4 Feb 2020 16:56:30 +0800
Subject: [PATCH] swkbd: Fix digit filter

The DIGIT filter was incorrectly implemented as preventing all digits. It actually limits the maximum digit count to max_digits, according to ctrulib and hardware testing.
---
 src/core/frontend/applets/swkbd.cpp | 6 +++---
 src/core/frontend/applets/swkbd.h   | 5 ++---
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/core/frontend/applets/swkbd.cpp b/src/core/frontend/applets/swkbd.cpp
index d4368bcc65..c29166b65c 100644
--- a/src/core/frontend/applets/swkbd.cpp
+++ b/src/core/frontend/applets/swkbd.cpp
@@ -15,9 +15,9 @@ namespace Frontend {
 
 ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) const {
     if (config.filters.prevent_digit) {
-        if (std::any_of(input.begin(), input.end(),
-                        [](unsigned char c) { return std::isdigit(c); })) {
-            return ValidationError::DigitNotAllowed;
+        if (std::count_if(input.begin(), input.end(),
+                          [](unsigned char c) { return std::isdigit(c); }) > config.max_digits) {
+            return ValidationError::MaxDigitsExceeded;
         }
     }
     if (config.filters.prevent_at) {
diff --git a/src/core/frontend/applets/swkbd.h b/src/core/frontend/applets/swkbd.h
index 1e803411f9..1ca1858cc7 100644
--- a/src/core/frontend/applets/swkbd.h
+++ b/src/core/frontend/applets/swkbd.h
@@ -47,8 +47,7 @@ struct KeyboardConfig {
     bool has_custom_button_text; /// If true, use the button_text instead
     std::vector<std::string> button_text; /// Contains the button text that the caller provides
     struct Filters {
-        bool prevent_digit;     /// Disallow the use of more than a certain number of digits
-                                /// TODO: how many is a certain number
+        bool prevent_digit;     /// Limit maximum digit count to max_digits
         bool prevent_at;        /// Disallow the use of the @ sign.
         bool prevent_percent;   /// Disallow the use of the % sign.
         bool prevent_backslash; /// Disallow the use of the \ sign.
@@ -68,7 +67,7 @@ enum class ValidationError {
     // Button Selection
     ButtonOutOfRange,
     // Configured Filters
-    DigitNotAllowed,
+    MaxDigitsExceeded,
     AtSignNotAllowed,
     PercentNotAllowed,
     BackslashNotAllowed,