Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efcd29d

Browse files
author
Gaël Écorchard
committed
Use snprintf instead of sprintf
- Augment the buffer size on doc error. - Let sprintf in switch_node.h since the max. string length is known. Signed-off-by: Gaël Écorchard <[email protected]>
1 parent 574a34f commit efcd29d

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/xml_parsing.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1111
*/
1212

13+
#include <cstdio>
1314
#include <cstring>
1415
#include <functional>
1516
#include <iostream>
@@ -23,10 +24,6 @@
2324
#pragma GCC diagnostic ignored "-Wattributes"
2425
#endif
2526

26-
#ifdef _MSC_VER
27-
#pragma warning(disable : 4996) // do not complain about sprintf
28-
#endif
29-
3027
#include <map>
3128
#include "behaviortree_cpp/xml_parsing.h"
3229
#include "tinyxml2/tinyxml2.h"
@@ -239,8 +236,8 @@ void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
239236
{
240237
if (doc->Error())
241238
{
242-
char buffer[200];
243-
sprintf(buffer, "Error parsing the XML: %s", doc->ErrorStr());
239+
char buffer[512];
240+
snprintf(buffer, sizeof buffer, "Error parsing the XML: %s", doc->ErrorStr());
244241
throw RuntimeError(buffer);
245242
}
246243

@@ -350,14 +347,14 @@ void VerifyXML(const std::string& xml_text,
350347
if (xml_error)
351348
{
352349
char buffer[512];
353-
sprintf(buffer, "Error parsing the XML: %s", doc.ErrorName());
350+
snprintf(buffer, sizeof buffer, "Error parsing the XML: %s", doc.ErrorName());
354351
throw RuntimeError(buffer);
355352
}
356353

357354
//-------- Helper functions (lambdas) -----------------
358355
auto ThrowError = [&](int line_num, const std::string& text) {
359356
char buffer[512];
360-
sprintf(buffer, "Error at line %d: -> %s", line_num, text.c_str());
357+
snprintf(buffer, sizeof buffer, "Error at line %d: -> %s", line_num, text.c_str());
361358
throw RuntimeError(buffer);
362359
};
363360

tests/test_helper.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef TEST_HELPER_HPP
22
#define TEST_HELPER_HPP
33

4+
#include <cstdio>
5+
46
#include "behaviortree_cpp/bt_factory.h"
57

68
inline BT::NodeStatus TestTick(int* tick_counter)
@@ -17,7 +19,7 @@ void RegisterTestTick(BT::BehaviorTreeFactory& factory, const std::string& name_
1719
{
1820
tick_counters[i] = false;
1921
char str[100];
20-
sprintf(str, "%s%c", name_prefix.c_str(), char('A'+i ) );
22+
snprintf(str, sizeof str, "%s%c", name_prefix.c_str(), char('A'+i ) );
2123
int* counter_ptr = &(tick_counters[i]);
2224
factory.registerSimpleAction(str, std::bind(&TestTick, counter_ptr));
2325
}

0 commit comments

Comments
 (0)