[Lldb-commits] [PATCH] D147246: [lldb] Replace sprintf with snprintf (NFC)

Dave Lee via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 30 10:21:42 PDT 2023


kastiglione created this revision.
kastiglione added a reviewer: JDevlieghere.
Herald added a project: All.
kastiglione requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

On macOS, `sprintf` is deprecated, using `snprintf` is recommended instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147246

Files:
  lldb/source/Core/SourceManager.cpp
  lldb/source/DataFormatters/StringPrinter.cpp
  lldb/source/Host/macosx/objcxx/Host.mm


Index: lldb/source/Host/macosx/objcxx/Host.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/Host.mm
+++ lldb/source/Host/macosx/objcxx/Host.mm
@@ -719,12 +719,12 @@
                                 const Args &args) {
   size_t count = args.GetArgumentCount();
   char buf[50]; // long enough for 'argXXX'
-  memset(buf, 0, 50);
-  sprintf(buf, "%sCount", prefix);
+  memset(buf, 0, sizeof(buf));
+  snprintf(buf, sizeof(buf), "%sCount", prefix);
   xpc_dictionary_set_int64(message, buf, count);
   for (size_t i = 0; i < count; i++) {
-    memset(buf, 0, 50);
-    sprintf(buf, "%s%zi", prefix, i);
+    memset(buf, 0, sizeof(buf));
+    snprintf(buf, sizeof(buf), "%s%zi", prefix, i);
     xpc_dictionary_set_string(message, buf, args.GetArgumentAtIndex(i));
   }
 }
Index: lldb/source/DataFormatters/StringPrinter.cpp
===================================================================
--- lldb/source/DataFormatters/StringPrinter.cpp
+++ lldb/source/DataFormatters/StringPrinter.cpp
@@ -154,11 +154,11 @@
   switch (escape_style) {
   case StringPrinter::EscapeStyle::CXX:
     // Prints 4 characters, then a \0 terminator.
-    escaped_len = sprintf((char *)data, "\\x%02x", *buffer);
+    escaped_len = snprintf((char *)data, max_buffer_size, "\\x%02x", *buffer);
     break;
   case StringPrinter::EscapeStyle::Swift:
     // Prints up to 6 characters, then a \0 terminator.
-    escaped_len = sprintf((char *)data, "\\u{%x}", *buffer);
+    escaped_len = snprintf((char *)data, max_buffer_size, "\\u{%x}", *buffer);
     break;
   }
   lldbassert(escaped_len > 0 && "unknown string escape style");
@@ -201,11 +201,11 @@
   switch (escape_style) {
   case StringPrinter::EscapeStyle::CXX:
     // Prints 10 characters, then a \0 terminator.
-    escaped_len = sprintf((char *)data, "\\U%08x", codepoint);
+    escaped_len = snprintf((char *)data, max_buffer_size, "\\U%08x", codepoint);
     break;
   case StringPrinter::EscapeStyle::Swift:
     // Prints up to 12 characters, then a \0 terminator.
-    escaped_len = sprintf((char *)data, "\\u{%x}", codepoint);
+    escaped_len = snprintf((char *)data, max_buffer_size, "\\u{%x}", codepoint);
     break;
   }
   lldbassert(escaped_len > 0 && "unknown string escape style");
Index: lldb/source/Core/SourceManager.cpp
===================================================================
--- lldb/source/Core/SourceManager.cpp
+++ lldb/source/Core/SourceManager.cpp
@@ -205,7 +205,8 @@
       }
 
       char buffer[3];
-      sprintf(buffer, "%2.2s", (line == curr_line) ? current_line_cstr : "");
+      snprintf(buffer, sizeof(buffer), "%2.2s",
+               (line == curr_line) ? current_line_cstr : "");
       std::string current_line_highlight(buffer);
 
       auto debugger_sp = m_debugger_wp.lock();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147246.509718.patch
Type: text/x-patch
Size: 2827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230330/b6a9b32b/attachment-0001.bin>


More information about the lldb-commits mailing list