[Bf-blender-cvs] [0d945fe20e8] blender-v3.4-release: Fix deprecation warnings about printf() on macOS

Sergey Sharybin noreply at git.blender.org
Tue Nov 8 12:02:50 CET 2022


Commit: 0d945fe20e87ac7ada2d565f751146c2e8fa1ed6
Author: Sergey Sharybin
Date:   Mon Nov 7 15:43:20 2022 +0100
Branches: blender-v3.4-release
https://developer.blender.org/rB0d945fe20e87ac7ada2d565f751146c2e8fa1ed6

Fix deprecation warnings about printf() on macOS

The new Xcode 14.1 brings the new Apple Clang compiler which
considers sprintf unsafe and geenrates deprecation warnings
suggesting to sue snprintf instead. This only happens for C++
code by default, and C code can still use sprintf without any
warning.

This changes does the following:

- Whenever is trivial replace sprintf() with BLI_snprintf.
- For all other cases use the newly introduced BLI_sprintf
  which is a wrapper around sprintf() but without warning.

There is a discouragement note in the BLI_sprintf comment to
suggest use of BLI_snprintf when the size is known.

Differential Revision: https://developer.blender.org/D16410

===================================================================

M	source/blender/blenkernel/intern/freestyle.c
M	source/blender/blenkernel/intern/image.cc
M	source/blender/blenkernel/intern/node.cc
M	source/blender/blenkernel/intern/pointcache.c
M	source/blender/blenkernel/intern/writeffmpeg.c
M	source/blender/blenlib/BLI_string.h
M	source/blender/blenlib/intern/path_util.c
M	source/blender/blenlib/intern/string.c
M	source/blender/blenlib/intern/uuid.cc
M	source/blender/blenlib/intern/winstuff.c
M	source/blender/blenloader/intern/writefile.cc
M	source/blender/compositor/intern/COM_Debug.cc
M	source/blender/draw/intern/draw_manager_profiling.c
M	source/blender/draw/intern/draw_pbvh.cc
M	source/blender/editors/io/io_collada.c
M	source/blender/editors/object/object_bake_api.c
M	source/blender/editors/object/object_constraint.c
M	source/blender/editors/render/render_internal.cc
M	source/blender/editors/render/render_preview.cc
M	source/blender/editors/space_node/space_node.cc
M	source/blender/editors/space_outliner/tree/tree_element_rna.cc
M	source/blender/editors/space_view3d/view3d_draw.cc
M	source/blender/io/avi/intern/avi_codecs.c
M	source/blender/io/collada/AnimationExporter.cpp
M	source/blender/io/collada/GeometryExporter.cpp
M	source/blender/io/gpencil/intern/gpencil_io_export_svg.cc
M	source/blender/makesrna/intern/rna_particle.c
M	source/blender/nodes/texture/nodes/node_texture_output.c
M	source/blender/python/bmesh/bmesh_py_types.c
M	source/blender/python/intern/bpy_rna.c
M	source/blender/sequencer/intern/disk_cache.c

===================================================================

diff --git a/source/blender/blenkernel/intern/freestyle.c b/source/blender/blenkernel/intern/freestyle.c
index a0649930dfc..28d0d1719d7 100644
--- a/source/blender/blenkernel/intern/freestyle.c
+++ b/source/blender/blenkernel/intern/freestyle.c
@@ -183,7 +183,7 @@ FreestyleLineSet *BKE_freestyle_lineset_add(struct Main *bmain,
     BLI_strncpy(lineset->name, name, sizeof(lineset->name));
   }
   else if (lineset_index > 0) {
-    sprintf(lineset->name, "LineSet %i", lineset_index + 1);
+    BLI_snprintf(lineset->name, sizeof(lineset->name), "LineSet %i", lineset_index + 1);
   }
   else {
     strcpy(lineset->name, "LineSet");
diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index eae8b454189..75e3e22afa7 100644
--- a/source/blender/blenkernel/intern/image.cc
+++ b/source/blender/blenkernel/intern/image.cc
@@ -3610,12 +3610,12 @@ void BKE_image_set_filepath_from_tile_number(char *filepath,
   }
 
   if (tile_format == UDIM_TILE_FORMAT_UDIM) {
-    sprintf(filepath, pattern, tile_number);
+    BLI_sprintf(filepath, pattern, tile_number);
   }
   else if (tile_format == UDIM_TILE_FORMAT_UVTILE) {
     int u = ((tile_number - 1001) % 10);
     int v = ((tile_number - 1001) / 10);
-    sprintf(filepath, pattern, u + 1, v + 1);
+    BLI_sprintf(filepath, pattern, u + 1, v + 1);
   }
 }
 
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index ad9d4e4fcca..eb0c78e9361 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -3436,7 +3436,7 @@ void ntreeRemoveSocketInterface(bNodeTree *ntree, bNodeSocket *sock)
 static void ntree_interface_identifier_base(bNodeTree *ntree, char *base)
 {
   /* generate a valid RNA identifier */
-  sprintf(base, "NodeTreeInterface_%s", ntree->id.name + 2);
+  BLI_sprintf(base, "NodeTreeInterface_%s", ntree->id.name + 2);
   RNA_identifier_sanitize(base, false);
 }
 
@@ -3462,8 +3462,8 @@ static void ntree_interface_identifier(bNodeTree *ntree,
   BLI_uniquename_cb(
       ntree_interface_unique_identifier_check, nullptr, base, '_', identifier, maxlen);
 
-  sprintf(name, "Node Tree %s Interface", ntree->id.name + 2);
-  sprintf(description, "Interface properties of node group %s", ntree->id.name + 2);
+  BLI_sprintf(name, "Node Tree %s Interface", ntree->id.name + 2);
+  BLI_sprintf(description, "Interface properties of node group %s", ntree->id.name + 2);
 }
 
 static void ntree_interface_type_create(bNodeTree *ntree)
diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c
index 868cdde6d01..5622530ea41 100644
--- a/source/blender/blenkernel/intern/pointcache.c
+++ b/source/blender/blenkernel/intern/pointcache.c
@@ -3132,15 +3132,15 @@ static void ptcache_dt_to_str(char *str, double dtime)
 {
   if (dtime > 60.0) {
     if (dtime > 3600.0) {
-      sprintf(
+      BLI_sprintf(
           str, "%ih %im %is", (int)(dtime / 3600), ((int)(dtime / 60)) % 60, ((int)dtime) % 60);
     }
     else {
-      sprintf(str, "%im %is", ((int)(dtime / 60)) % 60, ((int)dtime) % 60);
+      BLI_sprintf(str, "%im %is", ((int)(dtime / 60)) % 60, ((int)dtime) % 60);
     }
   }
   else {
-    sprintf(str, "%is", ((int)dtime) % 60);
+    BLI_sprintf(str, "%is", ((int)dtime) % 60);
   }
 }
 
diff --git a/source/blender/blenkernel/intern/writeffmpeg.c b/source/blender/blenkernel/intern/writeffmpeg.c
index ee827cd8cd4..1d4cbc48833 100644
--- a/source/blender/blenkernel/intern/writeffmpeg.c
+++ b/source/blender/blenkernel/intern/writeffmpeg.c
@@ -1370,7 +1370,7 @@ static void ffmpeg_filepath_get(
 
   if ((rd->ffcodecdata.flags & FFMPEG_AUTOSPLIT_OUTPUT) != 0) {
     if (context) {
-      sprintf(autosplit, "_%03d", context->ffmpeg_autosplit_count);
+      BLI_snprintf(autosplit, sizeof(autosplit), "_%03d", context->ffmpeg_autosplit_count);
     }
   }
 
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index 17abcf52ecc..fb02ea5fb17 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -205,6 +205,13 @@ size_t BLI_vsnprintf_rlen(char *__restrict buffer,
 char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1, 2);
 
+/**
+ * A wrapper around ::sprintf() which does not generate security warnings.
+ *
+ * \note Use BLI_snprintf for cases when the string size is known.
+ */
+int BLI_sprintf(char *__restrict str, const char *__restrict format, ...);
+
 /**
  * This roughly matches C and Python's string escaping with double quotes - `"`.
  *
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 179a1a305d1..25549de182a 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -123,7 +123,7 @@ int BLI_path_sequence_decode(const char *string, char *head, char *tail, ushort
 void BLI_path_sequence_encode(
     char *string, const char *head, const char *tail, ushort numlen, int pic)
 {
-  sprintf(string, "%s%.*d%s", head, numlen, MAX2(0, pic), tail);
+  BLI_sprintf(string, "%s%.*d%s", head, numlen, MAX2(0, pic), tail);
 }
 
 static int BLI_path_unc_prefix_len(const char *path); /* defined below in same file */
@@ -620,7 +620,7 @@ bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char
   }
 
   BLI_strncpy(extension, string + a, sizeof(extension));
-  sprintf(string + a, "%s%s%s", sep, suffix, extension);
+  BLI_sprintf(string + a, "%s%s%s", sep, suffix, extension);
   return true;
 }
 
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index 755d2dbd55d..3c3dcaf90f4 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -241,6 +241,17 @@ char *BLI_sprintfN(const char *__restrict format, ...)
   return n;
 }
 
+int BLI_sprintf(char *__restrict str, const char *__restrict format, ...)
+{
+  va_list arg;
+
+  va_start(arg, format);
+  const int result = vsprintf(str, format, arg);
+  va_end(arg);
+
+  return result;
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
@@ -1114,7 +1125,7 @@ static size_t BLI_str_format_int_grouped_ex(char src[16], char dst[16], int num_
 size_t BLI_str_format_int_grouped(char dst[16], int num)
 {
   char src[16];
-  int num_len = sprintf(src, "%d", num);
+  const int num_len = BLI_snprintf(src, sizeof(src), "%d", num);
 
   return BLI_str_format_int_grouped_ex(src, dst, num_len);
 }
@@ -1124,7 +1135,7 @@ size_t BLI_str_format_uint64_grouped(char dst[16], uint64_t num)
   /* NOTE: Buffer to hold maximum `uint64`, which is 1.8e+19. but
    * we also need space for commas and null-terminator. */
   char src[27];
-  int num_len = sprintf(src, "%" PRIu64 "", num);
+  const int num_len = BLI_snprintf(src, sizeof(src), "%" PRIu64 "", num);
 
   return BLI_str_format_int_grouped_ex(src, dst, num_len);
 }
diff --git a/source/blender/blenlib/intern/uuid.cc b/source/blender/blenlib/intern/uuid.cc
index 023dd1ec409..b845208f0da 100644
--- a/source/blender/blenlib/intern/uuid.cc
+++ b/source/blender/blenlib/intern/uuid.cc
@@ -5,6 +5,7 @@
  */
 
 #include "BLI_assert.h"
+#include "BLI_string.h"
 #include "BLI_uuid.h"
 
 #include <cstdio>
@@ -85,19 +86,19 @@ bool BLI_uuid_equal(const bUUID uuid1, const bUUID uuid2)
 
 void BLI_uuid_format(char *buffer, const bUUID uuid)
 {
-  std::sprintf(buffer,
-               "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-               uuid.time_low,
-               uuid.time_mid,
-               uuid.time_hi_and_version,
-               uuid.clock_seq_hi_and_reserved,
-               uuid.clock_seq_low,
-               uuid.node[0],
-               uuid.node[1],
-               uuid.node[2],
-               uuid.node[3],
-               uuid.node[4],
-               uuid.node[5]);
+  BLI_sprintf(buffer,
+              "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+              uuid.time_low,
+              uuid.time_mid,
+              uuid.time_hi_and_version,
+              uuid.clock_seq_hi_and_reserved,
+              uuid.clock_seq_low,
+              uuid.node[0],
+              uuid.node[1],
+              uuid.node[2],
+              uuid.node[3],
+              uuid.node[4],
+              uuid.node[5]);
 }
 
 bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer)
diff --git a/source/blender/blenlib/intern/winstuff.c b/source/blender/blenlib/intern/winstuff.c
index 7e2c5e8f1dd..3a574b60ae2 100644
--- a/source/blender/blenlib/intern/winstuff.c
+++ b/source/blender/blenlib/intern/winstuff.c
@@ -110,7 +110,7 @@ bool BLI_windows_register_blend_extension(const bool background)
                            &hkey,
                            &dwd);
   if (lresult == ERROR_SUCCESS) {
-    sprintf(buffer, "\"%s\" \"%%1\"", BlPath);
+    BLI_snprintf(buffer, sizeof(buffer), "\"%s\" \"%%1\"", BlPath);
     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
     RegCloseKey(hkey);
   }
@@ -129,7 +129,7 @@ bool BLI_windows_register_blend_extension(const bool background)
                            &hkey,
                            &dwd);
   if (lresult == ERROR_SUCCESS) {
-    sprintf(buffer, "\"%s\", 1", BlPath);
+    BLI_snprintf(buffer, sizeof(buffer), "\"%s\", 1", BlPath);
     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
     RegCloseKey(hkey);
   }
@@ -167,10 +167,12 @@ bool BLI_windows_register_blend_extension(const bool background)
   RegCloseKey(root);
   printf("success (%s)\n", usr_mode ? "user" : "system");
   if (!background) {
-    sprintf(MBox,
-            "File extension registered for %s.",
-            usr_mode ? "the current user. To register for all users, run as an administrator" :
-                       "all users");
+    BLI_snprintf(MBox,
+                 sizeof(MBox),
+                 "File extension registered for %s.",
+                 usr_mode ?
+                     "the current user. To register for all users, run as an administrator" :
+                     "all users");
     MessageBox

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list