simplify size check in string
b/72956754
Bug: skia:
Change-Id: I50627d9c7fe84630c496f8829608cde875512da0
Reviewed-on: https://skia-review.googlesource.com/107304
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Herb Derby <herb@google.com>
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp
index 59c57c9..0978904 100644
--- a/src/core/SkString.cpp
+++ b/src/core/SkString.cpp
@@ -5,8 +5,8 @@
* found in the LICENSE file.
*/
-
#include "SkAtomics.h"
+#include "SkSafeMath.h"
#include "SkString.h"
#include "SkUtils.h"
#include <stdarg.h>
@@ -224,13 +224,18 @@
return sk_sp<SkString::Rec>(const_cast<Rec*>(&gEmptyRec));
}
- len = trim_size_t_to_u32(len);
- // add 1 for terminating 0, then align4 so we can have some slop when growing the string
- const size_t actualLength = SizeOfRec() + SkAlign4(len + 1);
- SkASSERT_RELEASE(len < actualLength); // Check for overflow.
+ SkSafeMath safe;
+ // We store a 32bit version of the length
+ uint32_t stringLen = safe.castTo<uint32_t>(len);
+ // Add SizeOfRec() for our overhead and 1 for null-termination
+ size_t allocationSize = safe.add(len, SizeOfRec() + sizeof(char));
+ // Align up to a multiple of 4
+ allocationSize = safe.alignUp(allocationSize, 4);
- void* storage = ::operator new (actualLength);
- sk_sp<Rec> rec(new (storage) Rec(SkToU32(len), 1));
+ SkASSERT_RELEASE(safe.ok());
+
+ void* storage = ::operator new (allocationSize);
+ sk_sp<Rec> rec(new (storage) Rec(stringLen, 1));
if (text) {
memcpy(rec->data(), text, len);
}