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

@@ -208,7 +208,7 @@ PJSON ParseJson(PGLOBAL g, char* s, size_t len, int* ptyp, bool* comma)
break;
} // endif pretty
sprintf(g->Message, "Unexpected ',' (pretty=%d)", pretty);
snprintf(g->Message, sizeof(g->Message), "Unexpected ',' (pretty=%d)", pretty);
throw 3;
case '(':
b = true;
@@ -229,7 +229,7 @@ PJSON ParseJson(PGLOBAL g, char* s, size_t len, int* ptyp, bool* comma)
}; // endswitch s[i]
if (!jsp)
sprintf(g->Message, "Invalid Json string '%.*s'", MY_MIN((int)len, 50), s);
snprintf(g->Message, sizeof(g->Message), "Invalid Json string '%.*s'", MY_MIN((int)len, 50), s);
else if (ptyp && pretty == 3) {
*ptyp = 3; // Not recognized pretty
@@ -278,7 +278,7 @@ PSZ Serialize(PGLOBAL g, PJSON jsp, char* fn, int pretty) {
b = pretty == 1;
} else {
if (!(fs = fopen(fn, "wb"))) {
sprintf(g->Message, MSG(OPEN_MODE_ERROR),
snprintf(g->Message, sizeof(g->Message), MSG(OPEN_MODE_ERROR),
"w", (int)errno, fn);
strcat(strcat(g->Message, ": "), strerror(errno));
throw 2;
@@ -571,7 +571,7 @@ PJAR JDOC::ParseArray(PGLOBAL g, int& i)
switch (s[i]) {
case ',':
if (level < 2) {
sprintf(g->Message, "Unexpected ',' near %.*s",ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected ',' near %.*s",ARGS);
throw 1;
} else
level = 1;
@@ -579,7 +579,7 @@ PJAR JDOC::ParseArray(PGLOBAL g, int& i)
break;
case ']':
if (level == 1) {
sprintf(g->Message, "Unexpected ',]' near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected ',]' near %.*s", ARGS);
throw 1;
} // endif level
@@ -594,7 +594,7 @@ PJAR JDOC::ParseArray(PGLOBAL g, int& i)
break;
default:
if (level == 2) {
sprintf(g->Message, "Unexpected value near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected value near %.*s", ARGS);
throw 1;
} else
jarp->AddArrayValue(g, ParseValue(g, i));
@@ -630,7 +630,7 @@ PJOB JDOC::ParseObject(PGLOBAL g, int& i)
jpp = jobp->AddPair(g, key);
level = 1;
} else {
sprintf(g->Message, "misplaced string near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "misplaced string near %.*s", ARGS);
throw 2;
} // endif level
@@ -640,14 +640,14 @@ PJOB JDOC::ParseObject(PGLOBAL g, int& i)
jpp->Val = ParseValue(g, ++i);
level = 2;
} else {
sprintf(g->Message, "Unexpected ':' near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected ':' near %.*s", ARGS);
throw 2;
} // endif level
break;
case ',':
if (level < 2) {
sprintf(g->Message, "Unexpected ',' near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected ',' near %.*s", ARGS);
throw 2;
} else
level = 0;
@@ -655,7 +655,7 @@ PJOB JDOC::ParseObject(PGLOBAL g, int& i)
break;
case '}':
if (level == 0 || level == 1) {
sprintf(g->Message, "Unexpected '}' near %.*s", ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected '}' near %.*s", ARGS);
throw 2;
} // endif level
@@ -667,7 +667,7 @@ PJOB JDOC::ParseObject(PGLOBAL g, int& i)
case '\t':
break;
default:
sprintf(g->Message, "Unexpected character '%c' near %.*s",
snprintf(g->Message, sizeof(g->Message), "Unexpected character '%c' near %.*s",
s[i], ARGS);
throw 2;
}; // endswitch s[i]
@@ -750,7 +750,7 @@ PJVAL JDOC::ParseValue(PGLOBAL g, int& i)
return jvp;
err:
sprintf(g->Message, "Unexpected character '%c' near %.*s", s[i], ARGS);
snprintf(g->Message, sizeof(g->Message), "Unexpected character '%c' near %.*s", s[i], ARGS);
throw 3;
} // end of ParseValue
@@ -1764,7 +1764,7 @@ void JVALUE::SetValue(PGLOBAL g, PVAL valp)
DataType = TYPE_BINT;
break;
default:
sprintf(g->Message, "Unsupported typ %d\n", valp->GetType());
snprintf(g->Message, sizeof(g->Message), "Unsupported typ %d\n", valp->GetType());
throw(777);
} // endswitch Type