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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/kernel_object.cc

Issue 795213003: Fix handling of free descriptors when calling dup2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 6 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 | « no previous file | native_client_sdk/src/tests/nacl_io_test/kernel_proxy_test.cc » ('j') | 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 "nacl_io/kernel_object.h" 5 #include "nacl_io/kernel_object.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <pthread.h> 10 #include <pthread.h>
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 245 }
246 246
247 void KernelObject::FreeAndReassignFD(int fd, 247 void KernelObject::FreeAndReassignFD(int fd,
248 const ScopedKernelHandle& handle, 248 const ScopedKernelHandle& handle,
249 const std::string& path) { 249 const std::string& path) {
250 if (NULL == handle) { 250 if (NULL == handle) {
251 FreeFD(fd); 251 FreeFD(fd);
252 } else { 252 } else {
253 AUTO_LOCK(handle_lock_); 253 AUTO_LOCK(handle_lock_);
254 254
255 // If the required FD is larger than the current set, grow the set 255 // If the required FD is larger than the current set, grow the set.
256 if (fd >= (int)handle_map_.size()) 256 int sz = static_cast<int>(handle_map_.size());
257 if (fd >= sz) {
258 // Expand the handle map to include all the extra descriptors
259 // up to and including fd.
257 handle_map_.resize(fd + 1); 260 handle_map_.resize(fd + 1);
261 // Add all the new descriptors, except fd, to the free list.
262 for (; sz < fd; ++sz) {
263 free_fds_.push_back(sz);
264 std::push_heap(free_fds_.begin(), free_fds_.end(),
265 std::greater<int>());
266 }
267 }
258 268
259 // This path will be from an existing handle, and absolute. 269 // This path will be from an existing handle, and absolute.
260 handle_map_[fd] = Descriptor_t(handle, path); 270 handle_map_[fd] = Descriptor_t(handle, path);
261 } 271 }
262 } 272 }
263 273
264 void KernelObject::FreeFD(int fd) { 274 void KernelObject::FreeFD(int fd) {
265 AUTO_LOCK(handle_lock_); 275 AUTO_LOCK(handle_lock_);
266 276
267 handle_map_[fd].handle.reset(NULL); 277 handle_map_[fd].handle.reset(NULL);
268 free_fds_.push_back(fd); 278 free_fds_.push_back(fd);
269 279
270 // Force lower numbered FD to be available first. 280 // Force lower numbered FD to be available first.
271 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); 281 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>());
272 } 282 }
273 283
274 } // namespace nacl_io 284 } // namespace nacl_io
OLDNEW
« no previous file with comments | « no previous file | native_client_sdk/src/tests/nacl_io_test/kernel_proxy_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698