diff options
author | Ilia K <ki.stfu@gmail.com> | 2015-02-27 19:43:08 +0000 |
---|---|---|
committer | Ilia K <ki.stfu@gmail.com> | 2015-02-27 19:43:08 +0000 |
commit | 686b1fe65ae90f531d5010b3c6b47b479d4b7ccd (patch) | |
tree | 5f634785f9fc1e7a88b587c29d43b13bae9afc2c /lldb/source/Host/macosx | |
parent | 79e6c74981f4755ed55b38175d8cd34ec91395b1 (diff) | |
download | bcm5719-llvm-686b1fe65ae90f531d5010b3c6b47b479d4b7ccd.tar.gz bcm5719-llvm-686b1fe65ae90f531d5010b3c6b47b479d4b7ccd.zip |
Fix FileSpec::GetPath to return null-terminated strings
Summary:
Before this fix the FileSpec::GetPath() returned string which might be without '\0' at the end.
It could have happened if the size of buffer for path was less than actual path.
Test case:
```
FileSpec test("/path/to/file", false);
char buf[]="!!!!!!";
test.GetPath(buf, 3);
```
Before fix:
```
233 FileSpec test("/path/to/file", false);
234 char buf[]="!!!!!!";
235 test.GetPath(buf, 3);
236
-> 237 if (core_file)
238 {
239 if (!core_file.Exists())
240 {
(lldb) print buf
(char [7]) $0 = "/pa!!!"
```
After fix:
```
233 FileSpec test("/path/to/file", false);
234 char buf[]="!!!!!!";
235 test.GetPath(buf, 3);
236
-> 237 if (core_file)
238 {
239 if (!core_file.Exists())
240 {
(lldb) print buf
(char [7]) $0 = "/p"
```
Reviewers: zturner, abidh, clayborg
Reviewed By: abidh, clayborg
Subscribers: tberghammer, vharron, lldb-commits, clayborg, zturner, abidh
Differential Revision: http://reviews.llvm.org/D7553
llvm-svn: 230787
Diffstat (limited to 'lldb/source/Host/macosx')
-rw-r--r-- | lldb/source/Host/macosx/HostInfoMacOSX.mm | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/lldb/source/Host/macosx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/HostInfoMacOSX.mm index a22674e63aa..edaff083373 100644 --- a/lldb/source/Host/macosx/HostInfoMacOSX.mm +++ b/lldb/source/Host/macosx/HostInfoMacOSX.mm @@ -134,22 +134,23 @@ HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) FileSpec lldb_file_spec; if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) return false; - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); - char *framework_pos = ::strstr(raw_path, "LLDB.framework"); - if (framework_pos) + std::string raw_path = lldb_file_spec.GetPath(); + + size_t framework_pos = raw_path.find("LLDB.framework"); + if (framework_pos != std::string::npos) { framework_pos += strlen("LLDB.framework"); #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) // Shallow bundle - *framework_pos = '\0'; + raw_path.resize(framework_pos); #else // Normal bundle - ::strncpy(framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); + raw_path.resize(framework_pos); + raw_path.append("/Resources"); #endif } - file_spec.GetDirectory().SetCString(raw_path); + file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); return (bool)file_spec.GetDirectory(); } @@ -160,16 +161,16 @@ HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec) if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) return false; - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + std::string raw_path = lldb_file_spec.GetPath(); - char *framework_pos = ::strstr(raw_path, "LLDB.framework"); - if (framework_pos) + size_t framework_pos = raw_path.find("LLDB.framework"); + if (framework_pos != std::string::npos) { framework_pos += strlen("LLDB.framework"); - ::strncpy(framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); + raw_path.resize(framework_pos); + raw_path.append("/Headers"); } - file_spec.GetDirectory().SetCString(raw_path); + file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); return true; } @@ -181,14 +182,14 @@ HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec) if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) return false; - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); + std::string raw_path = lldb_file_spec.GetPath(); - char *framework_pos = ::strstr(raw_path, "LLDB.framework"); - if (framework_pos) + size_t framework_pos = raw_path.find("LLDB.framework"); + if (framework_pos != std::string::npos) { framework_pos += strlen("LLDB.framework"); - ::strncpy(framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); + raw_path.resize(framework_pos); + raw_path.append("/Resources/Python"); } else { @@ -198,9 +199,9 @@ HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec) os.flush(); // We may get our string truncated. Should we protect this with an assert? - ::strncat(raw_path, python_version_dir.c_str(), sizeof(raw_path) - strlen(raw_path) - 1); + raw_path.append(python_version_dir.c_str()); } - file_spec.GetDirectory().SetCString(raw_path); + file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); return true; #else return false; @@ -214,16 +215,16 @@ HostInfoMacOSX::ComputeClangDirectory(FileSpec &file_spec) if (!GetLLDBPath (lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) return false; - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath (raw_path, sizeof (raw_path)); + std::string raw_path = lldb_file_spec.GetPath(); - char *framework_pos = ::strstr (raw_path, "LLDB.framework"); - if (framework_pos) + size_t framework_pos = raw_path.find("LLDB.framework"); + if (framework_pos != std::string::npos) { framework_pos += strlen("LLDB.framework"); - ::strncpy (framework_pos, "/Resources/Clang", PATH_MAX - (framework_pos - raw_path)); + raw_path.resize(framework_pos); + raw_path.append("/Resources/Clang"); } - file_spec.SetFile (raw_path, true); + file_spec.SetFile (raw_path.c_str(), true); return true; } @@ -233,16 +234,17 @@ HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) FileSpec lldb_file_spec; if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) return false; - char raw_path[PATH_MAX]; - lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); - char *framework_pos = ::strstr(raw_path, "LLDB.framework"); - if (!framework_pos) + std::string raw_path = lldb_file_spec.GetPath(); + + size_t framework_pos = raw_path.find("LLDB.framework"); + if (framework_pos == std::string::npos) return false; framework_pos += strlen("LLDB.framework"); - ::strncpy(framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path)); - file_spec.GetDirectory().SetCString(raw_path); + raw_path.resize(framework_pos); + raw_path.append("/Resources/PlugIns"); + file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); return true; } |