Skip to content

Commit

Permalink
static: add format string and its args checking
Browse files Browse the repository at this point in the history
tt_sprintf, tt_snprintf, and tt_vsnprintf now have compile-time
checks for their arguments fitting the format string.
  • Loading branch information
Gerold103 committed Nov 30, 2021
1 parent fe61004 commit 871f5a7
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/box/box.cc
Expand Up @@ -1115,7 +1115,7 @@ box_check_memory_quota(const char *quota_name)
return size;
diag_set(ClientError, ER_CFG, quota_name,
tt_sprintf("must be >= 0 and <= %zu, but it is %lld",
QUOTA_MAX, size));
QUOTA_MAX, (long long)size));
return -1;
}

Expand Down
5 changes: 3 additions & 2 deletions src/box/memtx_rtree.c
Expand Up @@ -409,8 +409,9 @@ memtx_rtree_index_new(struct memtx_engine *memtx, struct index_def *def)
def->opts.dimension > RTREE_MAX_DIMENSION) {
diag_set(UnsupportedIndexFeature, def,
tt_sprintf("dimension (%lld): must belong to "
"range [%u, %u]", def->opts.dimension,
1, RTREE_MAX_DIMENSION));
"range [%u, %u]",
(long long)def->opts.dimension, 1,
RTREE_MAX_DIMENSION));
return NULL;
}

Expand Down
7 changes: 5 additions & 2 deletions src/box/sql/func.c
Expand Up @@ -992,8 +992,11 @@ func_round(struct sql_context *ctx, int argc, const struct Mem *argv)

double d = argv[0].u.r;
struct Mem *res = ctx->pOut;
if (n != 0)
return mem_set_double(res, atof(tt_sprintf("%.*f", n, d)));
if (n != 0) {
int precision = MIN(n, INT_MAX);
return mem_set_double(res, atof(tt_sprintf(
"%.*lf", precision, d)));
}
/*
* DOUBLE values greater than 2^53 or less than -2^53 have no digits
* after the decimal point.
Expand Down
4 changes: 2 additions & 2 deletions src/box/sql/mem.c
Expand Up @@ -685,9 +685,9 @@ int_to_str0(struct Mem *mem)
assert((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0);
const char *str;
if (mem->type == MEM_TYPE_UINT)
str = tt_sprintf("%llu", mem->u.u);
str = tt_sprintf("%llu", (unsigned long long)mem->u.u);
else
str = tt_sprintf("%lld", mem->u.i);
str = tt_sprintf("%lld", (long long)mem->u.i);
return mem_copy_str0(mem, str);
}

Expand Down
2 changes: 1 addition & 1 deletion src/box/sql/select.c
Expand Up @@ -5091,7 +5091,7 @@ selectExpander(Walker * pWalker, Select * p)
/*
* Rewrite old name with correct pointer.
*/
name = tt_sprintf("sql_sq_%llX", (void *)space);
name = tt_sprintf("sql_sq_%llX", (long long)space);
sprintf(space->def->name, "%s", name);
while (pSel->pPrior) {
pSel = pSel->pPrior;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/core/tt_static.h
Expand Up @@ -31,6 +31,7 @@
* SUCH DAMAGE.
*/
#include "small/static.h"
#include "trivia/util.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -72,6 +73,7 @@ tt_cstr(const char *str, size_t len)
* Wrapper around vsnprintf() that prints the result to
* the static buffer.
*/
CFORMAT(printf, 2, 0)
static inline const char *
tt_vsnprintf(size_t size, const char *format, va_list ap)
{
Expand All @@ -89,6 +91,7 @@ tt_vsnprintf(size_t size, const char *format, va_list ap)
}

/** @copydoc tt_vsnprintf() */
CFORMAT(printf, 1, 2)
static inline const char *
tt_sprintf(const char *format, ...)
{
Expand All @@ -103,6 +106,7 @@ tt_sprintf(const char *format, ...)
* The same as tt_sprintf() but allows to specify more precise
* string limits.
*/
CFORMAT(printf, 2, 3)
static inline const char *
tt_snprintf(size_t size, const char *format, ...)
{
Expand Down

0 comments on commit 871f5a7

Please sign in to comment.