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

Side by Side Diff: include/core/SkTypes.h

Issue 1510683002: Add sk_careful_memcpy to catch undefined behavior in memcpy. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: reed Created 5 years 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 unified diff | Download patch
« no previous file with comments | « include/core/SkTDArray.h ('k') | src/core/SkPathRef.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkTypes_DEFINED 8 #ifndef SkTypes_DEFINED
9 #define SkTypes_DEFINED 9 #define SkTypes_DEFINED
10 10
11 // IWYU pragma: begin_exports 11 // IWYU pragma: begin_exports
12 #include "SkPreConfig.h" 12 #include "SkPreConfig.h"
13 #include "SkUserConfig.h" 13 #include "SkUserConfig.h"
14 #include "SkPostConfig.h" 14 #include "SkPostConfig.h"
15 #include <stddef.h> 15 #include <stddef.h>
16 #include <stdint.h> 16 #include <stdint.h>
17 17
18 #if defined(SK_ARM_HAS_NEON) 18 #if defined(SK_ARM_HAS_NEON)
19 #include <arm_neon.h> 19 #include <arm_neon.h>
20 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 20 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
21 #include <immintrin.h> 21 #include <immintrin.h>
22 #endif 22 #endif
23 // IWYU pragma: end_exports 23 // IWYU pragma: end_exports
24 24
25 #include <string.h> 25 #include <string.h>
26 26
27 /**
28 * sk_careful_memcpy() is just like memcpy(), but guards against undefined beha vior.
29 *
30 * It is undefined behavior to call memcpy() with null dst or src, even if len i s 0.
31 * If an optimizer is "smart" enough, it can exploit this to do unexpected thing s.
32 * memcpy(dst, src, 0);
33 * if (src) {
34 * printf("%x\n", *src);
35 * }
36 * In this code the compiler can assume src is not null and omit the if (src) {. ..} check,
37 * unconditionally running the printf, crashing the program if src really is nul l.
38 * Of the compilers we pay attention to only GCC performs this optimization in p ractice.
39 */
40 static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
41 // When we pass >0 len we had better already be passing valid pointers.
42 // So we just need to skip calling memcpy when len == 0.
43 if (len) {
44 memcpy(dst,src,len);
45 }
46 return dst;
47 }
48
27 /** \file SkTypes.h 49 /** \file SkTypes.h
28 */ 50 */
29 51
30 /** See SkGraphics::GetVersion() to retrieve these at runtime 52 /** See SkGraphics::GetVersion() to retrieve these at runtime
31 */ 53 */
32 #define SKIA_VERSION_MAJOR 1 54 #define SKIA_VERSION_MAJOR 1
33 #define SKIA_VERSION_MINOR 0 55 #define SKIA_VERSION_MINOR 0
34 #define SKIA_VERSION_PATCH 0 56 #define SKIA_VERSION_PATCH 0
35 57
36 /* 58 /*
(...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 private: 690 private:
669 void* fPtr; 691 void* fPtr;
670 size_t fSize; // can be larger than the requested size (see kReuse) 692 size_t fSize; // can be larger than the requested size (see kReuse)
671 uint32_t fStorage[(kSize + 3) >> 2]; 693 uint32_t fStorage[(kSize + 3) >> 2];
672 }; 694 };
673 // Can't guard the constructor because it's a template class. 695 // Can't guard the constructor because it's a template class.
674 696
675 #endif /* C++ */ 697 #endif /* C++ */
676 698
677 #endif 699 #endif
OLDNEW
« no previous file with comments | « include/core/SkTDArray.h ('k') | src/core/SkPathRef.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698