Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: include/core/SkFixed.h

Issue 1439483002: Fix code that left shifts a negative value. This has undefined behavior. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comment about unsigned cast. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/ClampRangeTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkFixed.h
diff --git a/include/core/SkFixed.h b/include/core/SkFixed.h
index c2a0a7a4fbf56a30fa5d6026fdb490ab804b5cff..48284effda6110f0011248d73efd590f930de621 100644
--- a/include/core/SkFixed.h
+++ b/include/core/SkFixed.h
@@ -60,11 +60,15 @@ typedef int32_t SkFixed;
inline SkFixed SkIntToFixed(int n)
{
SkASSERT(n >= -32768 && n <= 32767);
- return n << 16;
+ // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
+ // shifting.
+ return (unsigned)n << 16;
}
#else
- // force the cast to SkFixed to ensure that the answer is signed (like the debug version)
- #define SkIntToFixed(n) (SkFixed)((n) << 16)
+ // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
+ // shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
+ // debug version).
+ #define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
#endif
#define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
« no previous file with comments | « no previous file | tests/ClampRangeTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698