SkSafe_math.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkSafe_math_DEFINED
  8. #define SkSafe_math_DEFINED
  9. // This file protects against known bugs in ucrt\math.h.
  10. // Namely, that header defines inline methods without marking them static,
  11. // which makes it very easy to cause ODR violations and ensuing chaos.
  12. //
  13. // TODO: other headers? Here are some potential problem headers:
  14. // $ grep -R __inline * | grep -v static | cut -f 1 -d: | sort | uniq
  15. // corecrt.h
  16. // corecrt_stdio_config.h
  17. // ctype.h
  18. // fenv.h
  19. // locale.h
  20. // malloc.h
  21. // math.h
  22. // tchar.h
  23. // wchar.h
  24. // I took a quick look through other headers outside math.h.
  25. // Nothing looks anywhere near as likely to be used by Skia as math.h.
  26. #if defined(_MSC_VER) && !defined(_INC_MATH)
  27. // Our strategy here is to simply inject "static" into the headers
  28. // where it should have been written, just before __inline.
  29. //
  30. // Most inline-but-not-static methods in math.h are 32-bit only,
  31. // but not all of them (see frexpf, hypothf, ldexpf...). So to
  32. // be safe, 32- and 64-bit builds both get this treatment.
  33. #define __inline static __inline
  34. #include <math.h>
  35. #undef __inline
  36. #if !defined(_INC_MATH)
  37. #error Hmm. Looks like math.h has changed its header guards.
  38. #endif
  39. #define INC_MATH_IS_SAFE_NOW
  40. #else
  41. #include <math.h>
  42. #endif
  43. #endif//SkSafe_math_DEFINED