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

@@ -712,7 +712,7 @@ bool TDBMYSQL::MakeCommand(PGLOBAL g)
strlwr(strcpy(qrystr, Query->GetStr()));
} else {
sprintf(g->Message, "Cannot use this %s command",
snprintf(g->Message, sizeof(g->Message), "Cannot use this %s command",
(Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
return true;
} // endif p
@@ -922,7 +922,7 @@ bool TDBMYSQL::OpenDB(PGLOBAL g)
#if 0
if (!Myc.m_Res || !Myc.m_Fields) {
sprintf(g->Message, "%s result", (Myc.m_Res) ? "Void" : "No");
snprintf(g->Message, sizeof(g->Message), "%s result", (Myc.m_Res) ? "Void" : "No");
Myc.Close();
return true;
} // endif m_Res
@@ -1011,7 +1011,7 @@ PCOL TDBMYSQL::MakeFieldColumn(PGLOBAL g, char *name)
} // endfor n
if (!colp)
sprintf(g->Message, "Column %s is not in view", name);
snprintf(g->Message, sizeof(g->Message), "Column %s is not in view", name);
return colp;
} // end of MakeFieldColumn
@@ -1048,7 +1048,7 @@ int TDBMYSQL::SendCommand(PGLOBAL g)
if (Myc.ExecSQLcmd(g, Query->GetStr(), &w) == RC_NF) {
AftRows = Myc.m_Afrw;
sprintf(g->Message, "%s: %d affected rows", TableName, AftRows);
snprintf(g->Message, sizeof(g->Message), "%s: %d affected rows", TableName, AftRows);
PushWarning(g, this, 0); // 0 means a Note
if (trace(1))
@@ -1057,7 +1057,7 @@ int TDBMYSQL::SendCommand(PGLOBAL g)
if (w && Myc.ExecSQL(g, "SHOW WARNINGS") == RC_OK) {
// We got warnings from the remote server
while (Myc.Fetch(g, -1) == RC_OK) {
sprintf(g->Message, "%s: (%s) %s", TableName,
snprintf(g->Message, sizeof(g->Message), "%s: (%s) %s", TableName,
Myc.GetCharField(1), Myc.GetCharField(2));
PushWarning(g, this);
} // endwhile Fetch
@@ -1310,7 +1310,7 @@ bool MYSQLCOL::FindRank(PGLOBAL g)
return false;
} // endif Name
sprintf(g->Message, "Column %s not in result set", Name);
snprintf(g->Message, sizeof(g->Message), "Column %s not in result set", Name);
return true;
} // end of FindRank
@@ -1320,7 +1320,7 @@ bool MYSQLCOL::FindRank(PGLOBAL g)
bool MYSQLCOL::SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check)
{
if (!(To_Val = value)) {
sprintf(g->Message, MSG(VALUE_ERROR), Name);
snprintf(g->Message, sizeof(g->Message), MSG(VALUE_ERROR), Name);
return true;
} else if (Buf_Type == value->GetType()) {
// Values are of the (good) column type
@@ -1339,7 +1339,7 @@ bool MYSQLCOL::SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check)
} else {
// Values are not of the (good) column type
if (check) {
sprintf(g->Message, MSG(TYPE_VALUE_ERR), Name,
snprintf(g->Message, sizeof(g->Message), MSG(TYPE_VALUE_ERR), Name,
GetTypeName(Buf_Type), GetTypeName(value->GetType()));
return true;
} // endif check
@@ -1401,7 +1401,7 @@ void MYSQLCOL::ReadColumn(PGLOBAL g)
if (!tdbp->Fetched)
if ((rc = tdbp->Myc.Fetch(g, tdbp->N)) != RC_OK) {
if (rc == RC_EF)
sprintf(g->Message, MSG(INV_DEF_READ), rc);
snprintf(g->Message, sizeof(g->Message), MSG(INV_DEF_READ), rc);
throw 11;
} else
@@ -1419,7 +1419,7 @@ void MYSQLCOL::ReadColumn(PGLOBAL g)
p = buf;
if (Value->SetValue_char(p, strlen(p))) {
sprintf(g->Message, "Out of range value for column %s at row %d",
snprintf(g->Message, sizeof(g->Message), "Out of range value for column %s at row %d",
Name, tdbp->RowNumber(g));
PushWarning(g, tdbp);
} // endif SetValue_char