1c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// Copyright (c) 2011, Google Inc. 2c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// All rights reserved. 3c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// 4c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// Redistribution and use in source and binary forms, with or without 5c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// modification, are permitted provided that the following conditions are 6c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// met: 7c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// 8c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// * Redistributions of source code must retain the above copyright 9c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// notice, this list of conditions and the following disclaimer. 10c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// * Redistributions in binary form must reproduce the above 11c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// copyright notice, this list of conditions and the following disclaimer 12c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// in the documentation and/or other materials provided with the 13c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// distribution. 14c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// * Neither the name of Google Inc. nor the names of its 15c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// contributors may be used to endorse or promote products derived from 16c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// this software without specific prior written permission. 17c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// 18c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 30c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// safe_readlink.cc: Implement google_breakpad::SafeReadLink. 31c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org// See safe_readlink.h for details. 32c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 33c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org#include <stddef.h> 34c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 35c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org#include "third_party/lss/linux_syscall_support.h" 36c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 37c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.orgnamespace google_breakpad { 38c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 39c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.orgbool SafeReadLink(const char* path, char* buffer, size_t buffer_size) { 40c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org // sys_readlink() does not add a NULL byte to |buffer|. In order to return 41c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org // a NULL-terminated string in |buffer|, |buffer_size| should be at least 42c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org // one byte longer than the expected path length. Also, sys_readlink() 43c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org // returns the actual path length on success, which does not count the 44c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org // NULL byte, so |result_size| should be less than |buffer_size|. 45c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org ssize_t result_size = sys_readlink(path, buffer, buffer_size); 46c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org if (result_size >= 0 && static_cast<size_t>(result_size) < buffer_size) { 47c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org buffer[result_size] = '\0'; 48c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org return true; 49c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org } 50c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org return false; 51c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org} 52c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org 53c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org} // namespace google_breakpad 54