Skip to content

Commit 31c4f4c

Browse files
committed
apply fix according to comments
1 parent 2a1ffff commit 31c4f4c

2 files changed

Lines changed: 34 additions & 47 deletions

File tree

onnxruntime/core/session/onnxruntime_c_api.cc

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4830,36 +4830,54 @@ ORT_API(const OrtApi*, OrtApis::GetApi, uint32_t version) {
48304830
}
48314831

48324832
namespace {
4833-
consteval bool IsOrtVersionValid() {
4834-
// This is a consteval function to validate the format of ORT_VERSION at compile time.
4835-
// It should be in the format "X.Y.Z" where X == 1, Y and Z are non-negative integers.
4836-
std::string_view version(ORT_VERSION);
4833+
// Parse a non-negative integer from a string_view without leading zeros.
4834+
// Returns -1 on failure (empty, leading zero, non-digit, or overflow).
4835+
consteval int64_t ParseUint(std::string_view str) {
4836+
if (str.empty()) return -1;
4837+
// Leading zeros are not allowed (except "0" itself).
4838+
if (str.size() > 1 && str[0] == '0') return -1;
4839+
int64_t result = 0;
4840+
for (char c : str) {
4841+
if (c < '0' || c > '9') return -1;
4842+
result = result * 10 + (c - '0');
4843+
if (result > UINT32_MAX) return -1;
4844+
}
4845+
return result;
4846+
}
4847+
4848+
consteval bool IsOrtVersionValid(std::string_view version) {
4849+
// Validates ORT_VERSION at compile time.
4850+
// It must be in the format "1.Y.Z" where:
4851+
// - Major version is 1
4852+
// - Y and Z are non-negative integers without leading zeros
4853+
// - Y (minor version) must equal ORT_API_VERSION
48374854
size_t first_dot = version.find('.');
4838-
if (first_dot == std::string_view::npos || first_dot == 0 || first_dot == version.size() - 1) {
4839-
return false; // Must have two dots and cannot start or end with a dot
4840-
}
4855+
if (first_dot == std::string_view::npos) return false;
48414856
size_t second_dot = version.find('.', first_dot + 1);
4842-
if (second_dot == std::string_view::npos || second_dot == first_dot + 1 || second_dot == version.size() - 1) {
4843-
return false; // Must have two dots and cannot be adjacent or end with a dot
4844-
}
4857+
if (second_dot == std::string_view::npos) return false;
4858+
if (version.find('.', second_dot + 1) != std::string_view::npos) return false; // Exactly two dots
48454859
std::string_view major = version.substr(0, first_dot);
48464860
std::string_view minor = version.substr(first_dot + 1, second_dot - first_dot - 1);
48474861
std::string_view patch = version.substr(second_dot + 1);
48484862
if (major != "1") {
48494863
return false; // Major version must be 1
48504864
}
4851-
auto is_non_negative_integer = [](std::string_view str) {
4852-
return !str.empty() && std::all_of(str.begin(), str.end(), [](char c) { return c >= '0' && c <= '9'; });
4853-
};
4854-
if (!is_non_negative_integer(minor) || !is_non_negative_integer(patch)) {
4855-
return false; // Minor and patch versions must be non-negative integers
4865+
int64_t minor_val = ParseUint(minor);
4866+
int64_t patch_val = ParseUint(patch);
4867+
if (minor_val < 0 || patch_val < 0) {
4868+
return false; // Minor and patch must be valid non-negative integers without leading zeros
4869+
}
4870+
if (static_cast<uint32_t>(minor_val) != ORT_API_VERSION) {
4871+
return false; // Minor version must match ORT_API_VERSION
48564872
}
48574873
return true;
48584874
}
48594875
} // namespace
48604876

48614877
ORT_API(const char*, OrtApis::GetVersionString) {
4862-
static_assert(IsOrtVersionValid(), "ORT_VERSION must be in the format '1.Y.Z' where Y and Z are non-negative integers");
4878+
static_assert(IsOrtVersionValid(ORT_VERSION),
4879+
"ORT_VERSION must be in the format '1.Y.Z' where Y and Z are non-negative integers without leading "
4880+
"zeros, and Y must equal ORT_API_VERSION");
48634881
return ORT_VERSION;
48644882
}
48654883

onnxruntime/test/shared_lib/test_version.cc

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)