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