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

Side by Side Diff: tests/MatrixTest.cpp

Issue 41253002: Checking structure sizes before reading them from memory to avoid overflowing the buffer's stream. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Removing SkMatrix's writeToMemory, readFromMemory Created 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkMath.h" 9 #include "SkMath.h"
10 #include "SkMatrix.h" 10 #include "SkMatrix.h"
11 #include "SkMatrixUtils.h" 11 #include "SkMatrixUtils.h"
12 #include "SkRandom.h" 12 #include "SkRandom.h"
13 #include "SkReader32.h"
14 #include "SkWriter32.h"
13 15
14 static bool nearly_equal_scalar(SkScalar a, SkScalar b) { 16 static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
15 // Note that we get more compounded error for multiple operations when 17 // Note that we get more compounded error for multiple operations when
16 // SK_SCALAR_IS_FIXED. 18 // SK_SCALAR_IS_FIXED.
17 #ifdef SK_SCALAR_IS_FLOAT 19 #ifdef SK_SCALAR_IS_FLOAT
18 const SkScalar tolerance = SK_Scalar1 / 200000; 20 const SkScalar tolerance = SK_Scalar1 / 200000;
19 #else 21 #else
20 const SkScalar tolerance = SK_Scalar1 / 1024; 22 const SkScalar tolerance = SK_Scalar1 / 1024;
21 #endif 23 #endif
22 24
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 107
106 dst = src; 108 dst = src;
107 dst.fRight = src.fRight * 2; 109 dst.fRight = src.fRight * 2;
108 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); 110 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
109 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType()); 111 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
110 REPORTER_ASSERT(reporter, matrix.rectStaysRect()); 112 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
111 } 113 }
112 114
113 static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) { 115 static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
114 // add 100 in case we have a bug, I don't want to kill my stack in the test 116 // add 100 in case we have a bug, I don't want to kill my stack in the test
115 char buffer[SkMatrix::kMaxFlattenSize + 100]; 117 static const uint32_t kBufferSize = SkWriter32::MaxWriteMatrixSize() + 100;
116 uint32_t size1 = m.writeToMemory(NULL); 118 char buffer[kBufferSize];
117 uint32_t size2 = m.writeToMemory(buffer); 119 SkWriter32 writer(SkWriter32::MaxWriteMatrixSize(), buffer, kBufferSize);
120 uint32_t size1 = writer.writeMatrixSize(m);
121 writer.writeMatrix(m);
122 uint32_t size2 = writer.bytesWritten();
118 REPORTER_ASSERT(reporter, size1 == size2); 123 REPORTER_ASSERT(reporter, size1 == size2);
119 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize); 124 REPORTER_ASSERT(reporter, size1 <= SkWriter32::MaxWriteMatrixSize());
120 125
121 SkMatrix m2; 126 SkMatrix m2;
122 uint32_t size3 = m2.readFromMemory(buffer); 127 SkReader32 reader(buffer, kBufferSize);
128 reader.readMatrix(&m2);
129 uint32_t size3 = reader.offset();
123 REPORTER_ASSERT(reporter, size1 == size3); 130 REPORTER_ASSERT(reporter, size1 == size3);
124 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); 131 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
125 132
126 char buffer2[SkMatrix::kMaxFlattenSize + 100]; 133 char buffer2[kBufferSize];
127 size3 = m2.writeToMemory(buffer2); 134 SkWriter32 writer2(SkWriter32::MaxWriteMatrixSize(), buffer2, kBufferSize);
135 writer2.writeMatrix(m2);
136 size3 = writer2.bytesWritten();
128 REPORTER_ASSERT(reporter, size1 == size3); 137 REPORTER_ASSERT(reporter, size1 == size3);
129 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); 138 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
130 } 139 }
131 140
132 static void test_matrix_max_stretch(skiatest::Reporter* reporter) { 141 static void test_matrix_max_stretch(skiatest::Reporter* reporter) {
133 SkMatrix identity; 142 SkMatrix identity;
134 identity.reset(); 143 identity.reset();
135 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch()); 144 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
136 145
137 SkMatrix scale; 146 SkMatrix scale;
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 807
799 test_matrix_max_stretch(reporter); 808 test_matrix_max_stretch(reporter);
800 test_matrix_is_similarity(reporter); 809 test_matrix_is_similarity(reporter);
801 test_matrix_recttorect(reporter); 810 test_matrix_recttorect(reporter);
802 test_matrix_decomposition(reporter); 811 test_matrix_decomposition(reporter);
803 test_matrix_homogeneous(reporter); 812 test_matrix_homogeneous(reporter);
804 } 813 }
805 814
806 #include "TestClassDef.h" 815 #include "TestClassDef.h"
807 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) 816 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix)
OLDNEW
« include/core/SkWriter32.h ('K') | « src/pipe/SkGPipeWrite.cpp ('k') | tests/PathTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698