1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

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
This commit is contained in:
Mikhail Chalov
2022-07-19 19:06:55 +00:00
committed by Daniel Black
parent 95eb5e5a12
commit 19af1890b5
61 changed files with 931 additions and 919 deletions

View File

@@ -33,7 +33,7 @@ using namespace MSXML2;
inline bool TestHr(PGLOBAL g, HRESULT hr)
{
if FAILED(hr) {
sprintf(g->Message, "%s, hr=%d", MSG(COM_ERROR), hr);
snprintf(g->Message, sizeof(g->Message), "%s, hr=%d", MSG(COM_ERROR), hr);
return true;
} else
return false;
@@ -65,7 +65,7 @@ void CloseXMLFile(PGLOBAL g, PFBLOCK fp, bool all)
} catch(_com_error e) {
char *p = _com_util::ConvertBSTRToString(e.Description());
sprintf(g->Message, "%s %s", MSG(COM_ERROR), p);
snprintf(g->Message, sizeof(g->Message), "%s %s", MSG(COM_ERROR), p);
delete[] p;
} catch(...) {}
@@ -242,7 +242,7 @@ int DOMDOC::DumpDoc(PGLOBAL g, char *ofn)
try {
Docp->save(ofn);
} catch(_com_error e) {
sprintf(g->Message, "%s: %s", MSG(COM_ERROR),
snprintf(g->Message, sizeof(g->Message), "%s: %s", MSG(COM_ERROR),
_com_util::ConvertBSTRToString(e.Description()));
rc = -1;
} catch(...) {}
@@ -332,16 +332,16 @@ RCODE DOMNODE::GetContent(PGLOBAL g, char *buf, int len)
switch (lsr) {
case 0:
case ERROR_INSUFFICIENT_BUFFER: // 122L
sprintf(g->Message, "Truncated %s content", GetName(g));
snprintf(g->Message, sizeof(g->Message), "Truncated %s content", GetName(g));
rc = RC_INFO;
break;
case ERROR_NO_UNICODE_TRANSLATION: // 1113L
sprintf(g->Message, "Invalid character(s) in %s content",
snprintf(g->Message, sizeof(g->Message), "Invalid character(s) in %s content",
GetName(g));
rc = RC_INFO;
break;
default:
sprintf(g->Message, "System error getting %s content",
snprintf(g->Message, sizeof(g->Message), "System error getting %s content",
GetName(g));
rc = RC_FX;
break;
@@ -370,7 +370,7 @@ bool DOMNODE::SetContent(PGLOBAL g, char *txtp, int len)
if (!MultiByteToWideChar(CP_UTF8, 0, txtp, strlen(txtp) + 1,
Ws, Len + 1)) {
sprintf(g->Message, MSG(WS_CONV_ERR), txtp);
snprintf(g->Message, sizeof(g->Message), MSG(WS_CONV_ERR), txtp);
return true;
} // endif
@@ -451,7 +451,7 @@ PXNODE DOMNODE::SelectSingleNode(PGLOBAL g, char *xp, PXNODE np)
} // endif dnp
} catch(_com_error e) {
sprintf(g->Message, "%s: %s", MSG(COM_ERROR),
snprintf(g->Message, sizeof(g->Message), "%s: %s", MSG(COM_ERROR),
_com_util::ConvertBSTRToString(e.Description()));
} catch(...) {}
@@ -710,16 +710,16 @@ RCODE DOMATTR::GetText(PGLOBAL g, char *buf, int len)
switch (lsr) {
case 0:
case ERROR_INSUFFICIENT_BUFFER: // 122L
sprintf(g->Message, "Truncated %s content", GetName(g));
snprintf(g->Message, sizeof(g->Message), "Truncated %s content", GetName(g));
rc = RC_INFO;
break;
case ERROR_NO_UNICODE_TRANSLATION: // 1113L
sprintf(g->Message, "Invalid character(s) in %s content",
snprintf(g->Message, sizeof(g->Message), "Invalid character(s) in %s content",
GetName(g));
rc = RC_INFO;
break;
default:
sprintf(g->Message, "System error getting %s content",
snprintf(g->Message, sizeof(g->Message), "System error getting %s content",
GetName(g));
rc = RC_FX;
break;
@@ -745,7 +745,7 @@ bool DOMATTR::SetText(PGLOBAL g, char *txtp, int len)
if (!MultiByteToWideChar(CP_UTF8, 0, txtp, strlen(txtp) + 1,
Ws, Len + 1)) {
sprintf(g->Message, MSG(WS_CONV_ERR), txtp);
snprintf(g->Message, sizeof(g->Message), MSG(WS_CONV_ERR), txtp);
return true;
} // endif