Optimize a tight loop in FX_Number. (try 2)
While doing string to int conversion:
1) Remove a redundant std::isdigit() call.
2) Don't call CheckedNumeric::IsValid() in this case.
- It's almost always valid.
- Continuing in an invalid state does not cause any security issues.
Compare to https://pdfium-review.googlesource.com/72250, the difference
here is the order of operation has been preserved to avoid accidental
overflow.
Bug: chromium:1124998
Change-Id: I5f5f1dce7033bbc42b601bee4e797e50dda97c83
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/73338
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_number.cpp b/core/fxcrt/fx_number.cpp
index 178ede9..d268fa0 100644
--- a/core/fxcrt/fx_number.cpp
+++ b/core/fxcrt/fx_number.cpp
@@ -50,11 +50,11 @@
cc++;
}
- while (cc < strc.GetLength() && std::isdigit(strc[cc])) {
- unsigned_val = unsigned_val * 10 + FXSYS_DecimalCharToInt(strc.CharAt(cc));
- if (!unsigned_val.IsValid())
- break;
- cc++;
+ for (; cc < strc.GetLength() && std::isdigit(strc[cc]); ++cc) {
+ // Deliberately not using FXSYS_DecimalCharToInt() in a tight loop to avoid
+ // a duplicate std::isdigit() call. Note that the order of operation is
+ // important to avoid unintentional overflows.
+ unsigned_val = unsigned_val * 10 + (strc[cc] - '0');
}
uint32_t uValue = unsigned_val.ValueOrDefault(0);