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

Side by Side Diff: base/win/scoped_handle.cc

Issue 1320673016: Get GenericScopedHandle::Set to preserve LastError code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/win/scoped_handle.h" 5 #include "base/win/scoped_handle.h"
6 6
7 #include <unordered_map> 7 #include <unordered_map>
8 8
9 #include "base/debug/alias.h" 9 #include "base/debug/alias.h"
10 #include "base/hash.h" 10 #include "base/hash.h"
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 221
222 } // namespace 222 } // namespace
223 223
224 void* GetHandleVerifier() { 224 void* GetHandleVerifier() {
225 return g_active_verifier; 225 return g_active_verifier;
226 } 226 }
227 227
228 namespace base { 228 namespace base {
229 namespace win { 229 namespace win {
230 230
231 // A common pattern in Chrome is to create a handle using an OS function,
grt (UTC plus 2) 2015/09/10 01:36:49 this is possibly at the wrong layer. either Generi
232 // store it in a ScopedHandle, and then call GetLastError(). With VC++ 2015
233 // this can fail (crbug.com/528394) because the handle tracking code may
234 // allocate memory and that may zero out the LastError value. The
235 // PreserveLastError object is used to save and restore the LastError code so
236 // that this intuitive pattern works.
237 class PreserveLastError {
238 public:
239 PreserveLastError() : last_error_(GetLastError()) {}
240 ~PreserveLastError() { SetLastError(last_error_); }
241
242 private:
243 unsigned last_error_;
rvargas (doing something else) 2015/09/10 01:39:35 DWORD (or unsigned int)
brucedawson 2015/09/10 21:52:49 In the new code I went with 'auto'. Could also do
244 DISALLOW_COPY_AND_ASSIGN(PreserveLastError);
245 };
246
231 // Static. 247 // Static.
232 bool HandleTraits::CloseHandle(HANDLE handle) { 248 bool HandleTraits::CloseHandle(HANDLE handle) {
249 PreserveLastError error_preserver;
grt (UTC plus 2) 2015/09/10 01:36:49 isn't this the wrong thing here? the last-error-co
rvargas (doing something else) 2015/09/10 01:39:35 I don't think we need this anywhere else but on Se
brucedawson 2015/09/10 01:46:27 The bugs are hard to track and difficult to avoid.
brucedawson 2015/09/10 01:46:27 Having been bitten by this I felt compelled to pre
brucedawson 2015/09/10 21:52:49 CloseHandle failures only cause failures under the
233 return ActiveVerifier::Get()->CloseHandle(handle); 250 return ActiveVerifier::Get()->CloseHandle(handle);
234 } 251 }
235 252
236 // Static. 253 // Static.
237 void VerifierTraits::StartTracking(HANDLE handle, const void* owner, 254 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
238 const void* pc1, const void* pc2) { 255 const void* pc1, const void* pc2) {
256 PreserveLastError error_preserver;
239 return ActiveVerifier::Get()->StartTracking(handle, owner, pc1, pc2); 257 return ActiveVerifier::Get()->StartTracking(handle, owner, pc1, pc2);
240 } 258 }
241 259
242 // Static. 260 // Static.
243 void VerifierTraits::StopTracking(HANDLE handle, const void* owner, 261 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
244 const void* pc1, const void* pc2) { 262 const void* pc1, const void* pc2) {
263 PreserveLastError error_preserver;
245 return ActiveVerifier::Get()->StopTracking(handle, owner, pc1, pc2); 264 return ActiveVerifier::Get()->StopTracking(handle, owner, pc1, pc2);
246 } 265 }
247 266
248 void DisableHandleVerifier() { 267 void DisableHandleVerifier() {
249 return ActiveVerifier::Get()->Disable(); 268 return ActiveVerifier::Get()->Disable();
250 } 269 }
251 270
252 void OnHandleBeingClosed(HANDLE handle) { 271 void OnHandleBeingClosed(HANDLE handle) {
272 PreserveLastError error_preserver;
grt (UTC plus 2) 2015/09/10 01:36:49 this function is called before the handle is close
brucedawson 2015/09/10 21:52:49 I agree that the error codes should not be preserv
253 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); 273 return ActiveVerifier::Get()->OnHandleBeingClosed(handle);
254 } 274 }
255 275
256 } // namespace win 276 } // namespace win
257 } // namespace base 277 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698