Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 8b36837

Browse files
chore: add validation for numeric input and handle out of range values (#2030)
* feat: implement input validation to allow only valid numeric selections * chore:refactor function name to follow coding conventions --------- Co-authored-by: vansangpfiev <vansangpfiev@gmail.com>
1 parent c9a35d0 commit 8b36837

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

engine/utils/cli_selection_utils.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ inline void PrintMenu(
2525
std::endl(std::cout);
2626
}
2727

28+
inline std::optional<int> GetNumericValue(const std::string& sval) {
29+
try {
30+
return std::stoi(sval);
31+
} catch (const std::invalid_argument&) {
32+
// Not a valid number
33+
return std::nullopt;
34+
} catch (const std::out_of_range&) {
35+
// Number out of range
36+
return std::nullopt;
37+
}
38+
}
39+
2840
inline std::optional<std::string> PrintModelSelection(
2941
const std::vector<std::string>& downloaded,
3042
const std::vector<std::string>& availables,
3143
const std::optional<std::string> default_selection = std::nullopt) {
3244

33-
std::string selection{""};
45+
std::string selection;
3446
if (!downloaded.empty()) {
3547
std::cout << "Downloaded models:\n";
3648
for (const auto& option : downloaded) {
@@ -60,7 +72,15 @@ inline std::optional<std::string> PrintModelSelection(
6072
return std::nullopt;
6173
}
6274

63-
if (std::stoi(selection) > availables.size() || std::stoi(selection) < 1) {
75+
// Validate if the selection consists solely of numeric characters
76+
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
77+
return std::nullopt;
78+
}
79+
80+
// deal with out of range numeric values
81+
std::optional<int> numeric_value = GetNumericValue(selection);
82+
83+
if (!numeric_value.has_value() || numeric_value.value() > availables.size() || numeric_value.value() < 1) {
6484
return std::nullopt;
6585
}
6686

@@ -71,7 +91,7 @@ inline std::optional<std::string> PrintSelection(
7191
const std::vector<std::string>& options,
7292
const std::string& title = "Select an option") {
7393
std::cout << title << "\n";
74-
std::string selection{""};
94+
std::string selection;
7595
PrintMenu(options);
7696
std::cout << "Select an option (" << 1 << "-" << options.size() << "): ";
7797
std::getline(std::cin, selection);
@@ -80,7 +100,14 @@ inline std::optional<std::string> PrintSelection(
80100
return std::nullopt;
81101
}
82102

83-
if (std::stoi(selection) > options.size() || std::stoi(selection) < 1) {
103+
// Validate if the selection consists solely of numeric characters
104+
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
105+
return std::nullopt;
106+
}
107+
108+
// deal with out of range numeric values
109+
std::optional<int> numeric_value = GetNumericValue(selection);
110+
if (!numeric_value.has_value() || numeric_value.value() > options.size() || numeric_value.value() < 1) {
84111
return std::nullopt;
85112
}
86113

0 commit comments

Comments
 (0)