[Bf-blender-cvs] [74c293863de] master: Cycles: Remove use of sprintf() in MD5 code

Sergey Sharybin noreply at git.blender.org
Thu Nov 3 15:14:42 CET 2022


Commit: 74c293863ded1c052601b4caed07efe3453d697d
Author: Sergey Sharybin
Date:   Thu Nov 3 15:08:46 2022 +0100
Branches: master
https://developer.blender.org/rB74c293863ded1c052601b4caed07efe3453d697d

Cycles: Remove use of sprintf() in MD5 code

The new Xcode declares the `sprintf()` function deprecated and
suggests to sue `snprintf()` as a safer alternative.

This change actually moves away from any formatted printing and
uses inlined byte-to-hex-string conversion which is also safe
and is (unmesurably) faster.

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

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

M	intern/cycles/util/md5.cpp

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

diff --git a/intern/cycles/util/md5.cpp b/intern/cycles/util/md5.cpp
index 1c7e6b9bf3e..3342d7a509a 100644
--- a/intern/cycles/util/md5.cpp
+++ b/intern/cycles/util/md5.cpp
@@ -347,13 +347,18 @@ void MD5Hash::finish(uint8_t digest[16])
 
 string MD5Hash::get_hex()
 {
+  constexpr char kHexDigits[] = {
+      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
   uint8_t digest[16];
   char buf[16 * 2 + 1];
 
   finish(digest);
 
-  for (int i = 0; i < 16; i++)
-    sprintf(buf + i * 2, "%02X", (unsigned int)digest[i]);
+  for (int i = 0; i < 16; i++) {
+    buf[i * 2 + 0] = kHexDigits[digest[i] / 0x10];
+    buf[i * 2 + 1] = kHexDigits[digest[i] % 0x10];
+  }
   buf[sizeof(buf) - 1] = '\0';
 
   return string(buf);



More information about the Bf-blender-cvs mailing list