Fix strncpy in all files of the "source" folder

http://reviews.llvm.org/D7553

Files:
  source/API/SBFileSpec.cpp
  source/API/SBModule.cpp
  source/Host/common/FileSpec.cpp
  source/Host/common/SocketAddress.cpp
  source/Host/macosx/HostInfoMacOSX.mm
  source/Host/posix/HostInfoPosix.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
  source/Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.cpp
  source/lldb.cpp

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
Index: source/API/SBFileSpec.cpp
===================================================================
--- source/API/SBFileSpec.cpp
+++ source/API/SBFileSpec.cpp
@@ -94,7 +94,8 @@
     llvm::SmallString<64> result(src_path);
     lldb_private::FileSpec::Resolve (result);
     size_t result_length = std::min(dst_len-1, result.size());
-    ::strncpy(dst_path, result.c_str(), result_length + 1);
+    ::strncpy(dst_path, result.c_str(), result_length);
+    dst_path[result_length] = '\0';
     return result_length;
 }
 
Index: source/API/SBModule.cpp
===================================================================
--- source/API/SBModule.cpp
+++ source/API/SBModule.cpp
@@ -220,7 +220,7 @@
 
     if (!uuid_string.empty())
     {
-        strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
+        ::strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer) - 1);
         uuid_string_buffer[sizeof (uuid_string_buffer) - 1] = '\0';
         uuid_c_string = uuid_string_buffer;
     }
Index: source/Host/common/FileSpec.cpp
===================================================================
--- source/Host/common/FileSpec.cpp
+++ source/Host/common/FileSpec.cpp
@@ -782,7 +782,8 @@
     std::string result = GetPath(denormalize);
 
     size_t result_length = std::min(path_max_len-1, result.length());
-    ::strncpy(path, result.c_str(), result_length + 1);
+    ::strncpy(path, result.c_str(), result_length);
+    path[result_length] = '\0';
     return result_length;
 }
 
Index: source/Host/common/SocketAddress.cpp
===================================================================
--- source/Host/common/SocketAddress.cpp
+++ source/Host/common/SocketAddress.cpp
@@ -48,8 +48,7 @@
                 const char* formatted = inet_ntoa(*static_cast<const in_addr*>(src));
                 if (formatted && strlen(formatted) < size)
                 {
-                    strncpy(dst, formatted, size);
-                    return dst;
+                    return ::strcpy(dst, formatted);
                 }
             }
             return nullptr;
@@ -64,8 +63,7 @@
                                           );
                 if (full_size < static_cast<int>(size))
                 {
-                    strncpy(dst,tmp,size);
-                    return dst;
+                    return ::strcpy(dst, tmp);
                 }
                 return nullptr;
             }
Index: source/Host/macosx/HostInfoMacOSX.mm
===================================================================
--- source/Host/macosx/HostInfoMacOSX.mm
+++ source/Host/macosx/HostInfoMacOSX.mm
@@ -146,7 +146,8 @@
         *framework_pos = '\0';
 #else
         // Normal bundle
-        ::strncpy(framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
+        ::strncpy(framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path) - 1);
+        framework_pos[PATH_MAX - (framework_pos - raw_path) - 1] = '\0';
 #endif
     }
     file_spec.GetDirectory().SetCString(raw_path);
@@ -167,7 +168,8 @@
     if (framework_pos)
     {
         framework_pos += strlen("LLDB.framework");
-        ::strncpy(framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
+        ::strncpy(framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path) - 1);
+        framework_pos[PATH_MAX - (framework_pos - raw_path) - 1] = '\0';
     }
     file_spec.GetDirectory().SetCString(raw_path);
     return true;
@@ -188,7 +190,8 @@
     if (framework_pos)
     {
         framework_pos += strlen("LLDB.framework");
-        ::strncpy(framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
+        ::strncpy(framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path) - 1);
+        framework_pos[PATH_MAX - (framework_pos - raw_path) - 1] = '\0';
     }
     else
     {
@@ -221,7 +224,8 @@
     if (framework_pos)
     {
         framework_pos += strlen("LLDB.framework");
-        ::strncpy (framework_pos, "/Resources/Clang", PATH_MAX - (framework_pos - raw_path));
+        ::strncpy(framework_pos, "/Resources/Clang", PATH_MAX - (framework_pos - raw_path) - 1);
+        framework_pos[PATH_MAX - (framework_pos - raw_path) - 1] = '\0';
     }
     file_spec.SetFile (raw_path, true);
     return true;
@@ -241,7 +245,8 @@
         return false;
 
     framework_pos += strlen("LLDB.framework");
-    ::strncpy(framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
+    ::strncpy(framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path) - 1);
+    framework_pos[PATH_MAX - (framework_pos - raw_path) - 1] = '\0';
     file_spec.GetDirectory().SetCString(raw_path);
     return true;
 }
Index: source/Host/posix/HostInfoPosix.cpp
===================================================================
--- source/Host/posix/HostInfoPosix.cpp
+++ source/Host/posix/HostInfoPosix.cpp
@@ -153,11 +153,9 @@
     char *lib_pos = ::strstr(raw_path, "/lib");
     if (lib_pos != nullptr)
     {
-        // First terminate the raw path at the start of lib.
-        *lib_pos = '\0';
-
         // Now write in bin in place of lib.
-        ::strncpy(lib_pos, "/bin", PATH_MAX - (lib_pos - raw_path));
+        ::strncpy(lib_pos, "/bin", PATH_MAX - (lib_pos - raw_path) - 1);
+        lib_pos[PATH_MAX - (lib_pos - raw_path) - 1] = '\0';
 
         if (log)
             log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__, raw_path);
Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
@@ -3639,7 +3639,10 @@
             if (thread_suffix_supported)
                 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
             else
-                ::strncpy (packet, "QSaveRegisterState", sizeof(packet));
+            {
+                ::strncpy (packet, "QSaveRegisterState", sizeof(packet) - 1);
+                packet[sizeof(packet) - 1] = '\0';
+            }
             
             StringExtractorGDBRemote response;
 
Index: source/Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.cpp
===================================================================
--- source/Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.cpp
+++ source/Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.cpp
@@ -231,7 +231,8 @@
                                                                                 const char *node_content = (const char *)::xmlNodeGetContent(value_node);
                                                                                 if (node_content)
                                                                                 {
-                                                                                    strncpy(DBGBuildSourcePath, node_content, sizeof(DBGBuildSourcePath));
+                                                                                    strncpy(DBGBuildSourcePath, node_content, sizeof(DBGBuildSourcePath) - 1);
+                                                                                    DBGBuildSourcePath[sizeof(DBGBuildSourcePath) - 1] = '\0';
                                                                                     xmlFree((void *) node_content);
                                                                                 }
                                                                             }
Index: source/lldb.cpp
===================================================================
--- source/lldb.cpp
+++ source/lldb.cpp
@@ -360,13 +360,14 @@
         
         const char *newline_loc = strchr(version_string, '\n');
         
-        size_t version_len = sizeof(g_version_string);
+        size_t version_len = sizeof(g_version_string) - 1;
         
         if (newline_loc &&
             (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
             version_len = newline_loc - version_string;
         
         ::strncpy(g_version_string, version_string, version_len);
+        g_version_string[version_len] = '\0';
     }
 
     return g_version_string;
_______________________________________________
lldb-commits mailing list
lldb-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits

Reply via email to