@@ -35,7 +35,7 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
35
35
{
36
36
// Reset the buffer
37
37
memset (destination.data (), ' \0 ' , destination.size ());
38
-
38
+ int snprintf_len = 0 ;
39
39
if (source.size () == 0 )
40
40
{
41
41
return destination.data ();
@@ -70,7 +70,8 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
70
70
ChipLogError (DataManagement, " Certificate size is greater than 400 bytes" );
71
71
}
72
72
73
- snprintf (destination.data (), destination.size (), " %s" , str.Get ());
73
+ snprintf_len = snprintf (destination.data (), destination.size (), " %s" , str.Get ());
74
+ VerifyOrExit (snprintf_len >= 0 , ChipLogError (DataManagement, " Failed to write certificate" ););
74
75
}
75
76
else
76
77
{
@@ -83,15 +84,23 @@ const char * ToCertificate(const chip::ByteSpan & source, chip::MutableCharSpan
83
84
size_t inIndex = 0 ;
84
85
size_t outIndex = strlen (header) + 1 ;
85
86
86
- snprintf (destination.data (), destination.size (), " %s\n " , header);
87
+ snprintf_len = snprintf (destination.data (), destination.size (), " %s\n " , header);
88
+ VerifyOrExit (snprintf_len >= 0 , ChipLogError (DataManagement, " Failed to write header" ););
87
89
for (; inIndex < base64DataLen; inIndex += 64 )
88
90
{
89
- auto charsPrinted = snprintf (&destination.data ()[outIndex], destination.size () - outIndex, " %.64s\n " , &str[inIndex]);
90
- outIndex += static_cast <size_t >(charsPrinted);
91
+ snprintf_len = snprintf (&destination.data ()[outIndex], destination.size () - outIndex, " %.64s\n " , &str[inIndex]);
92
+ VerifyOrExit (snprintf_len >= 0 , ChipLogError (DataManagement, " Failed to write certificate" ););
93
+
94
+ outIndex += static_cast <size_t >(snprintf_len);
91
95
}
92
- snprintf (&destination.data ()[outIndex], destination.size () - outIndex, " %s" , footer);
96
+ snprintf_len = snprintf (&destination.data ()[outIndex], destination.size () - outIndex, " %s" , footer);
97
+ VerifyOrExit (snprintf_len >= 0 , ChipLogError (DataManagement, " Failed to write footer" ););
98
+ }
99
+ exit :
100
+ if (snprintf_len < 0 )
101
+ {
102
+ memset (destination.data (), ' \0 ' , destination.size ());
93
103
}
94
-
95
104
return destination.data ();
96
105
}
97
106
0 commit comments