diff options
author | neeraj pratap singh <neerajpratapsingh@li-ff7f0d4c-3462-11b2-a85c-d4004c0fa1a0.ibm.com> | 2024-07-29 11:33:06 +0200 |
---|---|---|
committer | neeraj pratap singh <neerajpratapsingh@li-ff7f0d4c-3462-11b2-a85c-d4004c0fa1a0.ibm.com> | 2024-09-24 10:03:18 +0200 |
commit | 39d1cc5fd94aab8be1122ed1a8e3ee5849ce8a73 (patch) | |
tree | 9a9ca71a0f38ff02b3e75f46b0dab2c89631c63f | |
parent | Merge pull request #49974 from neesingh-rh/wip-58619 (diff) | |
download | ceph-39d1cc5fd94aab8be1122ed1a8e3ee5849ce8a73.tar.xz ceph-39d1cc5fd94aab8be1122ed1a8e3ee5849ce8a73.zip |
src/common : proper handling of units in `strict_iec_cast`
Fixes: https://tracker.ceph.com/issues/67169
Signed-off-by: Neeraj Pratap Singh <neesingh@redhat.com>
-rw-r--r-- | src/common/strtol.cc | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/src/common/strtol.cc b/src/common/strtol.cc index c9e982b6396..c97942adec5 100644 --- a/src/common/strtol.cc +++ b/src/common/strtol.cc @@ -146,43 +146,54 @@ T strict_iec_cast(std::string_view str, std::string *err) if (u != std::string_view::npos) { n = str.substr(0, u); unit = str.substr(u, str.length() - u); + // handling cases when prefixes entered as KB, MB, ... + // and KiB, MiB, .... + if (unit.length() > 1 && unit.back() == 'B') { + unit = unit.substr(0, unit.length() - 1); + } // we accept both old si prefixes as well as the proper iec prefixes // i.e. K, M, ... and Ki, Mi, ... - if (unit.back() == 'i') { - if (unit.front() == 'B') { - *err = "strict_iecstrtoll: illegal prefix \"Bi\""; - return 0; - } - } if (unit.length() > 2) { *err = "strict_iecstrtoll: illegal prefix (length > 2)"; return 0; } - switch(unit.front()) { - case 'K': - m = 10; - break; - case 'M': - m = 20; - break; - case 'G': - m = 30; - break; - case 'T': - m = 40; - break; - case 'P': - m = 50; - break; - case 'E': - m = 60; - break; - case 'B': - break; - default: - *err = "strict_iecstrtoll: unit prefix not recognized"; - return 0; + if ((unit.back() == 'i') || (unit.length() == 1)) { + if (unit.back() == 'i') { + if (unit.front() == 'B') { + *err = "strict_iecstrtoll: illegal prefix \"Bi\""; + return 0; + } + } + switch(unit.front()) { + case 'K': + m = 10; + break; + case 'M': + m = 20; + break; + case 'G': + m = 30; + break; + case 'T': + m = 40; + break; + case 'P': + m = 50; + break; + case 'E': + m = 60; + break; + case 'B': + break; + default: + *err = ("strict_iecstrtoll: unit prefix not recognized '" + std::string{unit} + "' "); + return 0; + } } + else { + *err = ("strict_iecstrtoll: illegal prefix '" + std::string{unit} + "' "); + return 0; + } } long long ll = strict_strtoll(n, 10, err); |