summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorNoel Grandin <noelgrandin@gmail.com>2021-10-02 13:18:37 +0200
committerMichael Meeks <michael.meeks@collabora.com>2021-10-02 22:36:17 +0200
commit9946a2fef07840ff4ca842928afbeeb52ece3603 (patch)
treea9f454ca5001800d3e8feecba23a89cb012d0834 /tools
parent897192f07c1f7863a50e09cdd29e6631550b83ff (diff)
fix buffer overruns in JsonWriter::put with UTF-8 values
Change-Id: I694585a1a540bfefc0e59bd58d8033a96ca35acb Signed-off-by: Michael Meeks <michael.meeks@collabora.com> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122996 Tested-by: Jenkins
Diffstat (limited to 'tools')
-rw-r--r--tools/source/misc/json_writer.cxx11
1 files changed, 8 insertions, 3 deletions
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
index f002ddc391aa..30ad911f9754 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -200,7 +200,10 @@ void JsonWriter::writeEscapedOUString(const OUString& rPropVal)
void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
{
auto nPropNameLength = strlen(pPropName);
- auto nWorstCasePropValLength = rPropVal.getLength() * 2;
+ // But values can be any UTF-8,
+ // see rtl_ImplGetFastUTF8ByteLen in sal/rtl/string.cxx for why a factor 3
+ // is the worst case
+ auto nWorstCasePropValLength = rPropVal.getLength() * 3;
ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
addCommaBeforeField();
@@ -220,8 +223,10 @@ void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
void JsonWriter::put(const char* pPropName, const OString& rPropVal)
{
+ // we assume property names are ascii
auto nPropNameLength = strlen(pPropName);
- auto nWorstCasePropValLength = rPropVal.getLength();
+ // escaping can double the length
+ auto nWorstCasePropValLength = rPropVal.getLength() * 2;
ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
addCommaBeforeField();
@@ -372,7 +377,7 @@ void JsonWriter::put(const char* pPropName, bool nPropVal)
void JsonWriter::putSimpleValue(const OUString& rPropVal)
{
- auto nWorstCasePropValLength = rPropVal.getLength() * 2;
+ auto nWorstCasePropValLength = rPropVal.getLength() * 3;
ensureSpace(nWorstCasePropValLength + 4);
addCommaBeforeField();