From 6b61617133b75d7300b20c25a98806f1ee10c0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Wed, 25 Jun 2014 13:55:18 +0200 Subject: [PATCH] Added a test that adds a string with a backslash in it --- JsonGeneratorTests/JsonArrayTests.cpp | 7 +++++++ JsonGeneratorTests/StringBuilder.cpp | 30 +++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 6d1c9cbf..0199939e 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -37,6 +37,13 @@ namespace JsonGeneratorTests AssertJsonIs("[\"\\\"\"]"); } + TEST_METHOD(AddOneStringContainingBackslash) + { + arr.add("\\"); + + AssertJsonIs("[\"\\\\\"]"); + } + TEST_METHOD(AddTwoStrings) { arr.add("hello"); diff --git a/JsonGeneratorTests/StringBuilder.cpp b/JsonGeneratorTests/StringBuilder.cpp index 5da8a7ee..a888e097 100644 --- a/JsonGeneratorTests/StringBuilder.cpp +++ b/JsonGeneratorTests/StringBuilder.cpp @@ -5,7 +5,7 @@ void StringBuilder::append(const char* s) { char* tail = buffer + length; - strcpy(tail, s); + strncpy(tail, s, capacity - length); length += strlen(tail); } @@ -16,17 +16,35 @@ void StringBuilder::appendEscaped(const char* s) buffer[length++] = '"'; - while (*s && length