Skip to content

Commit 39d1cc5

Browse files
author
neeraj pratap singh
committed
src/common : proper handling of units in strict_iec_cast
Fixes: https://tracker.ceph.com/issues/67169 Signed-off-by: Neeraj Pratap Singh <[email protected]>
1 parent 2ab1415 commit 39d1cc5

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

src/common/strtol.cc

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -146,43 +146,54 @@ T strict_iec_cast(std::string_view str, std::string *err)
146146
if (u != std::string_view::npos) {
147147
n = str.substr(0, u);
148148
unit = str.substr(u, str.length() - u);
149+
// handling cases when prefixes entered as KB, MB, ...
150+
// and KiB, MiB, ....
151+
if (unit.length() > 1 && unit.back() == 'B') {
152+
unit = unit.substr(0, unit.length() - 1);
153+
}
149154
// we accept both old si prefixes as well as the proper iec prefixes
150155
// i.e. K, M, ... and Ki, Mi, ...
151-
if (unit.back() == 'i') {
152-
if (unit.front() == 'B') {
153-
*err = "strict_iecstrtoll: illegal prefix \"Bi\"";
154-
return 0;
155-
}
156-
}
157156
if (unit.length() > 2) {
158157
*err = "strict_iecstrtoll: illegal prefix (length > 2)";
159158
return 0;
160159
}
161-
switch(unit.front()) {
162-
case 'K':
163-
m = 10;
164-
break;
165-
case 'M':
166-
m = 20;
167-
break;
168-
case 'G':
169-
m = 30;
170-
break;
171-
case 'T':
172-
m = 40;
173-
break;
174-
case 'P':
175-
m = 50;
176-
break;
177-
case 'E':
178-
m = 60;
179-
break;
180-
case 'B':
181-
break;
182-
default:
183-
*err = "strict_iecstrtoll: unit prefix not recognized";
184-
return 0;
160+
if ((unit.back() == 'i') || (unit.length() == 1)) {
161+
if (unit.back() == 'i') {
162+
if (unit.front() == 'B') {
163+
*err = "strict_iecstrtoll: illegal prefix \"Bi\"";
164+
return 0;
165+
}
166+
}
167+
switch(unit.front()) {
168+
case 'K':
169+
m = 10;
170+
break;
171+
case 'M':
172+
m = 20;
173+
break;
174+
case 'G':
175+
m = 30;
176+
break;
177+
case 'T':
178+
m = 40;
179+
break;
180+
case 'P':
181+
m = 50;
182+
break;
183+
case 'E':
184+
m = 60;
185+
break;
186+
case 'B':
187+
break;
188+
default:
189+
*err = ("strict_iecstrtoll: unit prefix not recognized '" + std::string{unit} + "' ");
190+
return 0;
191+
}
185192
}
193+
else {
194+
*err = ("strict_iecstrtoll: illegal prefix '" + std::string{unit} + "' ");
195+
return 0;
196+
}
186197
}
187198

188199
long long ll = strict_strtoll(n, 10, err);

0 commit comments

Comments
 (0)