summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorSzymon Kłos <szymon.klos@collabora.com>2021-12-08 11:53:35 +0100
committerAndras Timar <andras.timar@collabora.com>2022-02-14 11:25:40 +0100
commitb1cd29c42e47a434bdb54b744f9db711ac5ef95d (patch)
tree0d413e3da1dcbe6756fe33f97db0564a2a50e954 /include
parent05bac006ea18bcbc24a86035664f096787fff613 (diff)
jsonwriter: ensure correct number of bytes is available
In some functions author forgot that addCommaBeforeField() can add additional two characters. I didn't change cases where more bytes than needed are requested. Additional change is that in debug mode there is a marker at the end of allocated buffer - we check that after every write to detect overflow. No need to request more space for a marker as we always allocate "needed size * 2". Change-Id: I28066797b0ba833e408b0a731abc01b7fd989da3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126535 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129163 Tested-by: Jenkins Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'include')
-rw-r--r--include/tools/json_writer.hxx18
1 files changed, 18 insertions, 0 deletions
diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx
index 72ed59edadc5..700e82b9a2c6 100644
--- a/include/tools/json_writer.hxx
+++ b/include/tools/json_writer.hxx
@@ -89,6 +89,24 @@ private:
void writeEscapedOUString(const OUString& rPropVal);
std::pair<char*, int> extractDataImpl();
void ensureSpace(int noMoreBytesRequired);
+
+ // overflow validation in debug mode
+ static constexpr unsigned char JSON_WRITER_DEBUG_MARKER = 0xde;
+
+ inline void addValidationMark()
+ {
+#ifndef NDEBUG
+ *(mpBuffer + mSpaceAllocated - 1) = JSON_WRITER_DEBUG_MARKER;
+#endif
+ }
+
+ inline void validate()
+ {
+#ifndef NDEBUG
+ unsigned char c = *(mpBuffer + mSpaceAllocated - 1);
+ assert(c == JSON_WRITER_DEBUG_MARKER);
+#endif
+ }
};
/**