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

@@ -360,7 +360,7 @@ PVAL AllocateValue(PGLOBAL g, void *value, short type, short prec)
valp = new(g) TYPVAL<char>(*(char *)value, TYPE_TINY);
break;
default:
sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
snprintf(g->Message, sizeof(g->Message), MSG(BAD_VALUE_TYPE), type);
return NULL;
} // endswitch Type
@@ -421,7 +421,7 @@ PVAL AllocateValue(PGLOBAL g, int type, int len, int prec,
valp = new(g) BINVAL(g, (void*)NULL, len, prec);
break;
default:
sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
snprintf(g->Message, sizeof(g->Message), MSG(BAD_VALUE_TYPE), type);
return NULL;
} // endswitch type
@@ -493,7 +493,7 @@ PVAL AllocateValue(PGLOBAL g, PVAL valp, int newtype, int uns)
break;
default:
sprintf(g->Message, MSG(BAD_VALUE_TYPE), newtype);
snprintf(g->Message, sizeof(g->Message), MSG(BAD_VALUE_TYPE), newtype);
return NULL;
} // endswitch type
@@ -1189,7 +1189,7 @@ bool TYPVAL<TYPE>::Compall(PGLOBAL g, PVAL *vp, int np, OPVAL op)
Tval = MY_MAX(val[0], val[1]);
break;
default:
// sprintf(g->Message, MSG(BAD_EXP_OPER), op);
// snprintf(g->Message, sizeof(g->Message), MSG(BAD_EXP_OPER), op);
strcpy(g->Message, "Function not supported");
return true;
} // endswitch op
@@ -1435,7 +1435,7 @@ void TYPVAL<PSZ>::SetValue(int n)
int k = sprintf(buf, "%d", n);
if (k > Len) {
sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
snprintf(g->Message, sizeof(g->Message), MSG(VALSTR_TOO_LONG), buf, Len);
throw 138;
} else
SetValue_psz(buf);
@@ -1453,7 +1453,7 @@ void TYPVAL<PSZ>::SetValue(uint n)
int k = sprintf(buf, "%u", n);
if (k > Len) {
sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
snprintf(g->Message, sizeof(g->Message), MSG(VALSTR_TOO_LONG), buf, Len);
throw 138;
} else
SetValue_psz(buf);
@@ -1489,7 +1489,7 @@ void TYPVAL<PSZ>::SetValue(longlong n)
int k = sprintf(buf, "%lld", n);
if (k > Len) {
sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
snprintf(g->Message, sizeof(g->Message), MSG(VALSTR_TOO_LONG), buf, Len);
throw 138;
} else
SetValue_psz(buf);
@@ -1507,7 +1507,7 @@ void TYPVAL<PSZ>::SetValue(ulonglong n)
int k = sprintf(buf, "%llu", n);
if (k > Len) {
sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
snprintf(g->Message, sizeof(g->Message), MSG(VALSTR_TOO_LONG), buf, Len);
throw 138;
} else
SetValue_psz(buf);
@@ -1532,7 +1532,7 @@ void TYPVAL<PSZ>::SetValue(double f)
break;
if (k > Len) {
sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
snprintf(g->Message, sizeof(g->Message), MSG(VALSTR_TOO_LONG), buf, Len);
throw 138;
} else
SetValue_psz(buf);
@@ -1700,7 +1700,7 @@ bool TYPVAL<PSZ>::Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op)
SetValue_psz((strcmp(p[0], p[1]) > 0) ? p[0] : p[1]);
break;
default:
// sprintf(g->Message, MSG(BAD_EXP_OPER), op);
// snprintf(g->Message, sizeof(g->Message), MSG(BAD_EXP_OPER), op);
strcpy(g->Message, "Function not supported");
return true;
} // endswitch op
@@ -2398,7 +2398,7 @@ bool DTVAL::SetFormat(PGLOBAL g, PVAL valp)
DTVAL *vp;
if (valp->GetType() != TYPE_DATE) {
sprintf(g->Message, MSG(NO_FORMAT_TYPE), valp->GetType());
snprintf(g->Message, sizeof(g->Message), MSG(NO_FORMAT_TYPE), valp->GetType());
return true;
} else
vp = (DTVAL*)valp;