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

Side by Side Diff: net/base/file_stream_context_win.cc

Issue 920123002: Fix a crash in the FileStream::Context::Read code path where we were invoking a NULL callback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Windows trybot redness Created 5 years, 10 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 | « net/base/file_stream_context.h ('k') | 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 "net/base/file_stream_context.h" 5 #include "net/base/file_stream_context.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 int buf_len, 74 int buf_len,
75 const CompletionCallback& callback) { 75 const CompletionCallback& callback) {
76 CHECK(!async_in_progress_); 76 CHECK(!async_in_progress_);
77 DCHECK(!async_read_initiated_); 77 DCHECK(!async_read_initiated_);
78 DCHECK(!async_read_completed_); 78 DCHECK(!async_read_completed_);
79 DCHECK(!io_complete_for_read_received_); 79 DCHECK(!io_complete_for_read_received_);
80 80
81 IOCompletionIsPending(callback, buf); 81 IOCompletionIsPending(callback, buf);
82 82
83 async_read_initiated_ = true; 83 async_read_initiated_ = true;
84 result_ = 0;
84 85
85 task_runner_->PostTask( 86 task_runner_->PostTask(
86 FROM_HERE, 87 FROM_HERE,
87 base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this), 88 base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this),
88 file_.GetPlatformFile(), make_scoped_refptr(buf), buf_len, 89 file_.GetPlatformFile(), make_scoped_refptr(buf), buf_len,
89 &io_context_.overlapped, 90 &io_context_.overlapped,
90 base::MessageLoop::current()->message_loop_proxy())); 91 base::MessageLoop::current()->message_loop_proxy()));
91 return ERR_IO_PENDING; 92 return ERR_IO_PENDING;
92 } 93 }
93 94
94 int FileStream::Context::Write(IOBuffer* buf, 95 int FileStream::Context::Write(IOBuffer* buf,
95 int buf_len, 96 int buf_len,
96 const CompletionCallback& callback) { 97 const CompletionCallback& callback) {
97 CHECK(!async_in_progress_); 98 CHECK(!async_in_progress_);
98 99
100 result_ = 0;
101
99 DWORD bytes_written = 0; 102 DWORD bytes_written = 0;
100 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len, 103 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
101 &bytes_written, &io_context_.overlapped)) { 104 &bytes_written, &io_context_.overlapped)) {
102 IOResult error = IOResult::FromOSError(GetLastError()); 105 IOResult error = IOResult::FromOSError(GetLastError());
103 if (error.os_error == ERROR_IO_PENDING) 106 if (error.os_error == ERROR_IO_PENDING)
104 IOCompletionIsPending(callback, buf); 107 IOCompletionIsPending(callback, buf);
105 else 108 else
106 LOG(WARNING) << "WriteFile failed: " << error.os_error; 109 LOG(WARNING) << "WriteFile failed: " << error.os_error;
107 return static_cast<int>(error.result); 110 return static_cast<int>(error.result);
108 } 111 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 DWORD bytes_read, 146 DWORD bytes_read,
144 DWORD error) { 147 DWORD error) {
145 DCHECK_EQ(&io_context_, context); 148 DCHECK_EQ(&io_context_, context);
146 DCHECK(!callback_.is_null()); 149 DCHECK(!callback_.is_null());
147 DCHECK(async_in_progress_); 150 DCHECK(async_in_progress_);
148 151
149 if (!async_read_initiated_) 152 if (!async_read_initiated_)
150 async_in_progress_ = false; 153 async_in_progress_ = false;
151 154
152 if (orphaned_) { 155 if (orphaned_) {
153 async_in_progress_ = false; 156 io_complete_for_read_received_ = true;
154 callback_.Reset(); 157 // If we are called due to a pending read and the asynchronous read task
155 in_flight_buf_ = NULL; 158 // has not completed we have to keep the context around until it completes.
156 CloseAndDelete(); 159 if (async_read_initiated_ && !async_read_completed_)
160 return;
161 DeleteOrphanedContext();
157 return; 162 return;
158 } 163 }
159 164
160 if (error == ERROR_HANDLE_EOF) { 165 if (error == ERROR_HANDLE_EOF) {
161 result_ = 0; 166 result_ = 0;
162 } else if (error) { 167 } else if (error) {
163 IOResult error_result = IOResult::FromOSError(error); 168 IOResult error_result = IOResult::FromOSError(error);
164 result_ = static_cast<int>(error_result.result); 169 result_ = static_cast<int>(error_result.result);
165 } else { 170 } else {
171 if (result_)
172 DCHECK_EQ(result_, static_cast<int>(bytes_read));
166 result_ = bytes_read; 173 result_ = bytes_read;
167 IncrementOffset(&io_context_.overlapped, bytes_read); 174 IncrementOffset(&io_context_.overlapped, bytes_read);
168 } 175 }
169 176
170 if (async_read_initiated_) 177 if (async_read_initiated_)
171 io_complete_for_read_received_ = true; 178 io_complete_for_read_received_ = true;
172 179
173 InvokeUserCallback(); 180 InvokeUserCallback();
174 } 181 }
175 182
176 void FileStream::Context::InvokeUserCallback() { 183 void FileStream::Context::InvokeUserCallback() {
177 // For an asynchonous Read operation don't invoke the user callback until 184 // For an asynchonous Read operation don't invoke the user callback until
178 // we receive the IO completion notification and the asynchronous Read 185 // we receive the IO completion notification and the asynchronous Read
179 // completion notification. 186 // completion notification.
180 if (async_read_initiated_) { 187 if (async_read_initiated_) {
181 if (!io_complete_for_read_received_ || !async_read_completed_) 188 if (!io_complete_for_read_received_ || !async_read_completed_)
182 return; 189 return;
183 async_read_initiated_ = false; 190 async_read_initiated_ = false;
184 io_complete_for_read_received_ = false; 191 io_complete_for_read_received_ = false;
185 async_read_completed_ = false; 192 async_read_completed_ = false;
186 async_in_progress_ = false; 193 async_in_progress_ = false;
187 } 194 }
188 CompletionCallback temp_callback = callback_; 195 CompletionCallback temp_callback = callback_;
189 callback_.Reset(); 196 callback_.Reset();
190 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; 197 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
191 in_flight_buf_ = NULL; 198 in_flight_buf_ = NULL;
192 temp_callback.Run(result_); 199 temp_callback.Run(result_);
193 } 200 }
194 201
202 void FileStream::Context::DeleteOrphanedContext() {
203 async_in_progress_ = false;
204 callback_.Reset();
205 in_flight_buf_ = NULL;
206 CloseAndDelete();
207 }
208
195 // static 209 // static
196 void FileStream::Context::ReadAsync( 210 void FileStream::Context::ReadAsync(
197 FileStream::Context* context, 211 FileStream::Context* context,
198 HANDLE file, 212 HANDLE file,
199 scoped_refptr<net::IOBuffer> buf, 213 scoped_refptr<net::IOBuffer> buf,
200 int buf_len, 214 int buf_len,
201 OVERLAPPED* overlapped, 215 OVERLAPPED* overlapped,
202 scoped_refptr<base::MessageLoopProxy> origin_thread_loop) { 216 scoped_refptr<base::MessageLoopProxy> origin_thread_loop) {
203 DWORD bytes_read = 0; 217 DWORD bytes_read = 0;
204 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped); 218 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
205 origin_thread_loop->PostTask( 219 origin_thread_loop->PostTask(
206 FROM_HERE, base::Bind(&FileStream::Context::ReadAsyncResult, 220 FROM_HERE,
207 base::Unretained(context), ret ? bytes_read : 0, 221 base::Bind(&FileStream::Context::ReadAsyncResult,
208 ret ? 0 : ::GetLastError())); 222 base::Unretained(context), ret, bytes_read, ::GetLastError()));
209 } 223 }
210 224
211 void FileStream::Context::ReadAsyncResult(DWORD bytes_read, DWORD os_error) { 225 void FileStream::Context::ReadAsyncResult(BOOL read_file_ret,
212 if (!os_error) 226 DWORD bytes_read,
213 result_ = bytes_read; 227 DWORD os_error) {
228 // If the context is orphaned and we already received the io completion
229 // notification then we should delete the context and get out.
230 if (orphaned_ && io_complete_for_read_received_) {
231 DeleteOrphanedContext();
232 return;
233 }
214 234
235 async_read_completed_ = true;
236 if (read_file_ret || io_complete_for_read_received_) {
rvargas (doing something else) 2015/02/13 18:22:44 Sounds like an else to err != pending, no? Which,
ananta 2015/02/13 19:35:05 Done.
237 if (read_file_ret)
238 result_ = bytes_read;
239 InvokeUserCallback();
240 return;
241 }
215 IOResult error = IOResult::FromOSError(os_error); 242 IOResult error = IOResult::FromOSError(os_error);
216 if (error.os_error == ERROR_HANDLE_EOF) { 243 if (error.os_error != ERROR_IO_PENDING) {
rvargas (doing something else) 2015/02/13 18:22:44 nit: don't use {} here
ananta 2015/02/13 19:35:05 Reworked the if. PTAL
217 // Report EOF by returning 0 bytes read.
218 OnIOCompleted(&io_context_, 0, error.os_error); 244 OnIOCompleted(&io_context_, 0, error.os_error);
219 } else if (error.os_error != ERROR_IO_PENDING) {
220 // We don't need to inform the caller about ERROR_PENDING_IO as that was
221 // already done when the ReadFile call was queued to the worker pool.
222 if (error.os_error) {
223 LOG(WARNING) << "ReadFile failed: " << error.os_error;
224 OnIOCompleted(&io_context_, 0, error.os_error);
225 }
226 } 245 }
227 async_read_completed_ = true;
228 InvokeUserCallback();
229 } 246 }
230 247
231 } // namespace net 248 } // namespace net
OLDNEW
« no previous file with comments | « net/base/file_stream_context.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698