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

Side by Side Diff: base/debug/stack_trace_unittest.cc

Issue 18656004: Added a new SafeSPrintf() function that implements snprintf() in an async-safe-fashion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Jeffrey's comments Created 7 years, 4 months 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 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits> 5 #include <limits>
6 #include <sstream> 6 #include <sstream>
7 #include <string> 7 #include <string>
8 8
9 #include "base/debug/format.h"
9 #include "base/debug/stack_trace.h" 10 #include "base/debug/stack_trace.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/process/kill.h" 12 #include "base/process/kill.h"
12 #include "base/process/process_handle.h" 13 #include "base/process/process_handle.h"
13 #include "base/test/test_timeouts.h" 14 #include "base/test/test_timeouts.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/multiprocess_func_list.h" 16 #include "testing/multiprocess_func_list.h"
16 17
17 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_IOS) 18 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_IOS)
18 #include "base/test/multiprocess_test.h" 19 #include "base/test/multiprocess_test.h"
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 // of re-entering malloc. 146 // of re-entering malloc.
146 TEST_F(StackTraceTest, AsyncSignalUnsafeSignalHandlerHang) { 147 TEST_F(StackTraceTest, AsyncSignalUnsafeSignalHandlerHang) {
147 ProcessHandle child = this->SpawnChild("MismatchedMallocChildProcess", false); 148 ProcessHandle child = this->SpawnChild("MismatchedMallocChildProcess", false);
148 ASSERT_NE(kNullProcessHandle, child); 149 ASSERT_NE(kNullProcessHandle, child);
149 ASSERT_TRUE(WaitForSingleProcess(child, TestTimeouts::action_timeout())); 150 ASSERT_TRUE(WaitForSingleProcess(child, TestTimeouts::action_timeout()));
150 } 151 }
151 #endif // !defined(OS_IOS) 152 #endif // !defined(OS_IOS)
152 153
153 namespace { 154 namespace {
154 155
155 std::string itoa_r_wrapper(intptr_t i, size_t sz, int base, size_t padding) { 156 std::string itoa_r(intptr_t i, size_t sz, int base, size_t padding) {
jln (very slow on Chromium) 2013/08/01 00:03:15 I wonder if you shouldn't expose itoa_r() and just
156 char buffer[1024]; 157 // The stack tracer uses Format() to convert integer values to ASCII.
158 // It doesn't need all of the features of Format(), but instead restricts
159 // itself to a subset that can be thought of as an itoa_r() function.
160 char buffer[130];
157 CHECK_LE(sz, sizeof(buffer)); 161 CHECK_LE(sz, sizeof(buffer));
158 162 CHECK_LE(sizeof(intptr_t)*8+1, sizeof(buffer));
159 char* result = internal::itoa_r(i, buffer, sz, base, padding); 163 char format[40];
160 EXPECT_TRUE(result); 164 EXPECT_TRUE(base == 10 || base == 16);
165 CHECK_LT(Format(format, "%%0%d%c", padding, base == 10 ? 'd' : 'x'),
166 static_cast<ssize_t>(sizeof(format)));
167 CHECK_LT(FormatN(buffer, sz, format, i),
168 static_cast<ssize_t>(sizeof(buffer)));
161 return std::string(buffer); 169 return std::string(buffer);
162 } 170 }
163 171
164 } // namespace 172 } // namespace
165 173
166 TEST_F(StackTraceTest, itoa_r) { 174 TEST_F(StackTraceTest, itoa_r) {
167 EXPECT_EQ("0", itoa_r_wrapper(0, 128, 10, 0)); 175 EXPECT_EQ("0", itoa_r(0, 128, 10, 0));
168 EXPECT_EQ("-1", itoa_r_wrapper(-1, 128, 10, 0)); 176 EXPECT_EQ("-1", itoa_r(-1, 128, 10, 0));
169 177
170 // Test edge cases. 178 // Test edge cases.
171 if (sizeof(intptr_t) == 4) { 179 if (sizeof(intptr_t) == 4) {
172 EXPECT_EQ("ffffffff", itoa_r_wrapper(-1, 128, 16, 0)); 180 EXPECT_EQ("ffffffff", itoa_r(-1, 128, 16, 0));
173 EXPECT_EQ("-2147483648", 181 EXPECT_EQ("-2147483648",
174 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0)); 182 itoa_r(std::numeric_limits<intptr_t>::min(), 128, 10, 0));
175 EXPECT_EQ("2147483647", 183 EXPECT_EQ("2147483647",
176 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0)); 184 itoa_r(std::numeric_limits<intptr_t>::max(), 128, 10, 0));
177 185
178 EXPECT_EQ("80000000", 186 EXPECT_EQ("80000000",
179 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0)); 187 itoa_r(std::numeric_limits<intptr_t>::min(), 128, 16, 0));
180 EXPECT_EQ("7fffffff", 188 EXPECT_EQ("7fffffff",
181 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0)); 189 itoa_r(std::numeric_limits<intptr_t>::max(), 128, 16, 0));
182 } else if (sizeof(intptr_t) == 8) { 190 } else if (sizeof(intptr_t) == 8) {
183 EXPECT_EQ("ffffffffffffffff", itoa_r_wrapper(-1, 128, 16, 0)); 191 EXPECT_EQ("ffffffffffffffff", itoa_r(-1, 128, 16, 0));
184 EXPECT_EQ("-9223372036854775808", 192 EXPECT_EQ("-9223372036854775808",
185 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0)); 193 itoa_r(std::numeric_limits<intptr_t>::min(), 128, 10, 0));
186 EXPECT_EQ("9223372036854775807", 194 EXPECT_EQ("9223372036854775807",
187 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0)); 195 itoa_r(std::numeric_limits<intptr_t>::max(), 128, 10, 0));
188 196
189 EXPECT_EQ("8000000000000000", 197 EXPECT_EQ("8000000000000000",
190 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0)); 198 itoa_r(std::numeric_limits<intptr_t>::min(), 128, 16, 0));
191 EXPECT_EQ("7fffffffffffffff", 199 EXPECT_EQ("7fffffffffffffff",
192 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0)); 200 itoa_r(std::numeric_limits<intptr_t>::max(), 128, 16, 0));
193 } else { 201 } else {
194 ADD_FAILURE() << "Missing test case for your size of intptr_t (" 202 ADD_FAILURE() << "Missing test case for your size of intptr_t ("
195 << sizeof(intptr_t) << ")"; 203 << sizeof(intptr_t) << ")";
196 } 204 }
197 205
198 // Test hex output. 206 // Test hex output.
199 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0)); 207 EXPECT_EQ("688", itoa_r(0x688, 128, 16, 0));
200 EXPECT_EQ("deadbeef", itoa_r_wrapper(0xdeadbeef, 128, 16, 0)); 208 EXPECT_EQ("deadbeef", itoa_r(0xdeadbeef, 128, 16, 0));
201 209
202 // Check that itoa_r respects passed buffer size limit. 210 // Check that itoa_r respects passed buffer size limit.
203 char buffer[1024]; 211 EXPECT_EQ("deadbeef", itoa_r(0xdeadbeef, 10, 16, 0));
204 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 10, 16, 0)); 212 EXPECT_EQ("deadbeef", itoa_r(0xdeadbeef, 9, 16, 0));
205 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 9, 16, 0)); 213 EXPECT_EQ("deadbee", itoa_r(0xdeadbeef, 8, 16, 0));
206 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 8, 16, 0)); 214 EXPECT_EQ("deadbe", itoa_r(0xdeadbeef, 7, 16, 0));
207 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 7, 16, 0)); 215 EXPECT_EQ("beef", itoa_r(0xbeef, 5, 16, 4));
208 EXPECT_TRUE(internal::itoa_r(0xbeef, buffer, 5, 16, 4)); 216 EXPECT_EQ("0bee", itoa_r(0xbeef, 5, 16, 5));
209 EXPECT_FALSE(internal::itoa_r(0xbeef, buffer, 5, 16, 5)); 217 EXPECT_EQ("00be", itoa_r(0xbeef, 5, 16, 6));
210 EXPECT_FALSE(internal::itoa_r(0xbeef, buffer, 5, 16, 6));
211 218
212 // Test padding. 219 // Test padding.
213 EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 0)); 220 EXPECT_EQ("1", itoa_r(1, 128, 10, 0));
214 EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 1)); 221 EXPECT_EQ("1", itoa_r(1, 128, 10, 1));
215 EXPECT_EQ("01", itoa_r_wrapper(1, 128, 10, 2)); 222 EXPECT_EQ("01", itoa_r(1, 128, 10, 2));
216 EXPECT_EQ("001", itoa_r_wrapper(1, 128, 10, 3)); 223 EXPECT_EQ("001", itoa_r(1, 128, 10, 3));
217 EXPECT_EQ("0001", itoa_r_wrapper(1, 128, 10, 4)); 224 EXPECT_EQ("0001", itoa_r(1, 128, 10, 4));
218 EXPECT_EQ("00001", itoa_r_wrapper(1, 128, 10, 5)); 225 EXPECT_EQ("00001", itoa_r(1, 128, 10, 5));
219 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0)); 226 EXPECT_EQ("688", itoa_r(0x688, 128, 16, 0));
220 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 1)); 227 EXPECT_EQ("688", itoa_r(0x688, 128, 16, 1));
221 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 2)); 228 EXPECT_EQ("688", itoa_r(0x688, 128, 16, 2));
222 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 3)); 229 EXPECT_EQ("688", itoa_r(0x688, 128, 16, 3));
223 EXPECT_EQ("0688", itoa_r_wrapper(0x688, 128, 16, 4)); 230 EXPECT_EQ("0688", itoa_r(0x688, 128, 16, 4));
224 EXPECT_EQ("00688", itoa_r_wrapper(0x688, 128, 16, 5)); 231 EXPECT_EQ("00688", itoa_r(0x688, 128, 16, 5));
225 } 232 }
226 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) 233 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
227 234
228 } // namespace debug 235 } // namespace debug
229 } // namespace base 236 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698