| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/getdents_helper.h" | 5 #include "nacl_io/getdents_helper.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 #include "nacl_io/log.h" | 13 #include "nacl_io/log.h" |
| 14 #include "nacl_io/osinttypes.h" |
| 14 | 15 |
| 15 #include "sdk_util/macros.h" | 16 #include "sdk_util/macros.h" |
| 16 | 17 |
| 17 namespace nacl_io { | 18 namespace nacl_io { |
| 18 | 19 |
| 19 GetDentsHelper::GetDentsHelper() | 20 GetDentsHelper::GetDentsHelper() |
| 20 : curdir_ino_(0), parentdir_ino_(0), init_defaults_(false) { | 21 : curdir_ino_(0), parentdir_ino_(0), init_defaults_(false) { |
| 21 Initialize(); | 22 Initialize(); |
| 22 } | 23 } |
| 23 | 24 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 *out_bytes = 0; | 66 *out_bytes = 0; |
| 66 | 67 |
| 67 // If the buffer pointer is invalid, fail | 68 // If the buffer pointer is invalid, fail |
| 68 if (NULL == pdir) { | 69 if (NULL == pdir) { |
| 69 LOG_TRACE("dirent pointer is NULL."); | 70 LOG_TRACE("dirent pointer is NULL."); |
| 70 return EINVAL; | 71 return EINVAL; |
| 71 } | 72 } |
| 72 | 73 |
| 73 // If the buffer is too small, fail | 74 // If the buffer is too small, fail |
| 74 if (size < sizeof(dirent)) { | 75 if (size < sizeof(dirent)) { |
| 75 LOG_TRACE("dirent buffer size is too small: %d < %d", size, sizeof(dirent)); | 76 LOG_TRACE("dirent buffer size is too small: %" PRIuS " < %" PRIuS "", |
| 77 size, sizeof(dirent)); |
| 76 return EINVAL; | 78 return EINVAL; |
| 77 } | 79 } |
| 78 | 80 |
| 79 // Force size to a multiple of dirent | 81 // Force size to a multiple of dirent |
| 80 size -= size % sizeof(dirent); | 82 size -= size % sizeof(dirent); |
| 81 | 83 |
| 82 size_t max = dirents_.size() * sizeof(dirent); | 84 size_t max = dirents_.size() * sizeof(dirent); |
| 83 if (offs >= max) { | 85 if (offs >= max) { |
| 84 // OK, trying to read past the end. | 86 // OK, trying to read past the end. |
| 85 return 0; | 87 return 0; |
| 86 } | 88 } |
| 87 | 89 |
| 88 if (offs + size >= max) | 90 if (offs + size >= max) |
| 89 size = max - offs; | 91 size = max - offs; |
| 90 | 92 |
| 91 memcpy(pdir, reinterpret_cast<const char*>(dirents_.data()) + offs, size); | 93 memcpy(pdir, reinterpret_cast<const char*>(dirents_.data()) + offs, size); |
| 92 *out_bytes = size; | 94 *out_bytes = size; |
| 93 return 0; | 95 return 0; |
| 94 } | 96 } |
| 95 | 97 |
| 96 } // namespace nacl_io | 98 } // namespace nacl_io |
| OLD | NEW |