From 19af1890b56c6c147c296479bb6a4ad00fa59dbb Mon Sep 17 00:00:00 2001 From: Mikhail Chalov Date: Tue, 19 Jul 2022 19:06:55 +0000 Subject: Use memory safe snprintf() in Connect Engine This commit replaces sprintf(buf, ...) with snprintf(buf, sizeof(buf), ...), specifically in the "easy" cases where buf is allocated with a size known at compile time. The changes make sure we are not write outside array/string bounds which will lead to undefined behaviour. In case the code is trying to write outside bounds - safe version of functions simply cut the string messages so we process this gracefully. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc. bsonudf.cpp warnings cleanup by Daniel Black Reviewer: Daniel Black --- storage/connect/libdoc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'storage/connect/libdoc.cpp') diff --git a/storage/connect/libdoc.cpp b/storage/connect/libdoc.cpp index 0966477cbfd..aef7b551d65 100644 --- a/storage/connect/libdoc.cpp +++ b/storage/connect/libdoc.cpp @@ -661,7 +661,7 @@ xmlNodeSetPtr LIBXMLDOC::GetNodeList(PGLOBAL g, xmlNodePtr np, char *xp) if (xmlXPathRegisterNs(Ctxp, BAD_CAST nsp->Prefix, BAD_CAST nsp->Uri)) { - sprintf(g->Message, MSG(REGISTER_ERR), nsp->Prefix, nsp->Uri); + snprintf(g->Message, sizeof(g->Message), MSG(REGISTER_ERR), nsp->Prefix, nsp->Uri); if (trace(1)) htrc("Ns error: %s\n", g->Message); @@ -703,7 +703,7 @@ xmlNodeSetPtr LIBXMLDOC::GetNodeList(PGLOBAL g, xmlNodePtr np, char *xp) // Evaluate table xpath if (!(Xop = xmlXPathEval(BAD_CAST xp, Ctxp))) { - sprintf(g->Message, MSG(XPATH_EVAL_ERR), xp); + snprintf(g->Message, sizeof(g->Message), MSG(XPATH_EVAL_ERR), xp); if (trace(1)) htrc("Path error: %s\n", g->Message); @@ -882,7 +882,7 @@ RCODE XML2NODE::GetContent(PGLOBAL g, char *buf, int len) } // endif p1 } else { - sprintf(g->Message, "Truncated %s content", Nodep->name); + snprintf(g->Message, sizeof(g->Message), "Truncated %s content", Nodep->name); rc = RC_INFO; } // endif len @@ -1260,7 +1260,7 @@ RCODE XML2ATTR::GetText(PGLOBAL g, char *buf, int len) if (strlen((char*)txt) >= (unsigned)len) { memcpy(buf, txt, len - 1); buf[len - 1] = 0; - sprintf(g->Message, "Truncated %s content", Atrp->name); + snprintf(g->Message, sizeof(g->Message), "Truncated %s content", Atrp->name); rc = RC_INFO; } else strcpy(buf, (const char*)txt); -- cgit v1.2.1