Skip to content

Commit

Permalink
Use strmake over my_snprintf to copy strings
Browse files Browse the repository at this point in the history
There is no need to parse a format string if we just need to clone
chars over. In fact, when #3360 enabled `ATTRIBUTE_FORMAT` on
`my_snprintf`, `-Werror=format-security` complained several of
these that “format not a string literal and no format arguments”.
  • Loading branch information
ParadoxV5 committed Aug 15, 2024
1 parent cf08f44 commit e365a24
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
3 changes: 1 addition & 2 deletions client/mysqlshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,7 @@ list_tables(MYSQL *mysql,const char *db,const char *table)
opt_table_type ? " full" : "", rows);
}
else
my_snprintf(query, sizeof(query), "show%s tables",
opt_table_type ? " full" : "");
strcpy(query, opt_table_type ? "show full tables" : "show tables");
if (mysql_query(mysql, query) || !(result= mysql_store_result(mysql)))
{
fprintf(stderr,"%s: Cannot list tables in %s: %s\n",my_progname,db,
Expand Down
8 changes: 4 additions & 4 deletions client/mysqltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ size_t print_file_stack(char *s, const char *end)
for (;;)
{
err_file--;
s+= my_snprintf(s, end - s, "included from %s at line %d:\n",
s+= my_snprintf(s, end - s, "included from %s at line %u:\n",
err_file->file_name, err_file->lineno);
if (err_file == file_stack)
break;
Expand All @@ -1607,7 +1607,7 @@ size_t print_file_stack(char *s, const char *end)
static void make_error_message(char *buf, size_t len, const char *fmt, va_list args)
{
char *s= buf, *end= buf + len;
s+= my_snprintf(s, end - s, "mysqltest: ");
s= strmake(s, "mysqltest: ", len-1);
if (cur_file && cur_file != file_stack)
{
s+= my_snprintf(s, end - s, "In included file \"%s\": \n",
Expand All @@ -1616,12 +1616,12 @@ static void make_error_message(char *buf, size_t len, const char *fmt, va_list a
}

if (start_lineno > 0)
s+= my_snprintf(s, end -s, "At line %u: ", start_lineno);
s+= my_snprintf(s, end - s, "At line %u: ", start_lineno);
if (!fmt)
fmt= "unknown error";

s+= my_vsnprintf(s, end - s, fmt, args);
s+= my_snprintf(s, end -s, "\n");
strmake(s, "\n", end - s - 1);
}

static void die(const char *fmt, ...)
Expand Down
30 changes: 15 additions & 15 deletions sql/log_event_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3284,31 +3284,31 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
{
switch (type) {
case MYSQL_TYPE_LONG:
my_snprintf(typestr, typestr_length, "%s", "INT");
strmake(typestr, "INT", typestr_length-1);
break;
case MYSQL_TYPE_TINY:
my_snprintf(typestr, typestr_length, "TINYINT");
strmake(typestr, "TINYINT", typestr_length-1);
break;
case MYSQL_TYPE_SHORT:
my_snprintf(typestr, typestr_length, "SMALLINT");
strmake(typestr, "SMALLINT", typestr_length-1);
break;
case MYSQL_TYPE_INT24:
my_snprintf(typestr, typestr_length, "MEDIUMINT");
strmake(typestr, "MEDIUMINT", typestr_length-1);
break;
case MYSQL_TYPE_LONGLONG:
my_snprintf(typestr, typestr_length, "BIGINT");
strmake(typestr, "BIGINT", typestr_length-1);
break;
case MYSQL_TYPE_NEWDECIMAL:
my_snprintf(typestr, typestr_length, "DECIMAL(%d,%d)",
(*meta_ptr)[0], (*meta_ptr)[1]);
(*meta_ptr)+= 2;
break;
case MYSQL_TYPE_FLOAT:
my_snprintf(typestr, typestr_length, "FLOAT");
strmake(typestr, "FLOAT", typestr_length-1);
(*meta_ptr)++;
break;
case MYSQL_TYPE_DOUBLE:
my_snprintf(typestr, typestr_length, "DOUBLE");
strmake(typestr, "DOUBLE", typestr_length-1);
(*meta_ptr)++;
break;
case MYSQL_TYPE_BIT:
Expand All @@ -3320,36 +3320,36 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
if (**meta_ptr != 0)
my_snprintf(typestr, typestr_length, "TIMESTAMP(%d)", **meta_ptr);
else
my_snprintf(typestr, typestr_length, "TIMESTAMP");
strmake(typestr, "TIMESTAMP", typestr_length-1);
(*meta_ptr)++;
break;
case MYSQL_TYPE_DATETIME2:
if (**meta_ptr != 0)
my_snprintf(typestr, typestr_length, "DATETIME(%d)", **meta_ptr);
else
my_snprintf(typestr, typestr_length, "DATETIME");
strmake(typestr, "DATETIME", typestr_length-1);
(*meta_ptr)++;
break;
case MYSQL_TYPE_TIME2:
if (**meta_ptr != 0)
my_snprintf(typestr, typestr_length, "TIME(%d)", **meta_ptr);
else
my_snprintf(typestr, typestr_length, "TIME");
strmake(typestr, "TIME", typestr_length-1);
(*meta_ptr)++;
break;
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_DATE:
my_snprintf(typestr, typestr_length, "DATE");
strmake(typestr, "DATE", typestr_length-1);
break;
case MYSQL_TYPE_YEAR:
my_snprintf(typestr, typestr_length, "YEAR");
strmake(typestr, "YEAR", typestr_length-1);
break;
case MYSQL_TYPE_ENUM:
my_snprintf(typestr, typestr_length, "ENUM");
strmake(typestr, "ENUM", typestr_length-1);
(*meta_ptr)+= 2;
break;
case MYSQL_TYPE_SET:
my_snprintf(typestr, typestr_length, "SET");
strmake(typestr, "SET", typestr_length-1);
(*meta_ptr)+= 2;
break;
case MYSQL_TYPE_BLOB:
Expand Down Expand Up @@ -3404,7 +3404,7 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
"MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"
};
if (geometry_type < 8)
my_snprintf(typestr, typestr_length, names[geometry_type]);
strmake(typestr, names[geometry_type], typestr_length-1);
else
my_snprintf(typestr, typestr_length, "INVALID_GEOMETRY_TYPE(%u)",
geometry_type);
Expand Down
4 changes: 2 additions & 2 deletions sql/sql_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
protocol->store(&table_name, system_charset_info);
protocol->store(operator_name, system_charset_info);
protocol->store(&error_clex_str, system_charset_info);
length= my_snprintf(buff, sizeof(buff),
ER_THD(thd, ER_PARTITION_DOES_NOT_EXIST));
length= strmake(buff, ER_THD(thd, ER_PARTITION_DOES_NOT_EXIST),
sizeof(buff)-1) - buff;
protocol->store(buff, length, system_charset_info);
if(protocol->write())
goto err;
Expand Down
2 changes: 1 addition & 1 deletion storage/connect/tabrest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int Xcurl(PGLOBAL g, PCSZ Http, PCSZ Uri, PCSZ filename)
my_snprintf(buf, sizeof(buf)-1, "%s/%s", Http, Uri);

} else
my_snprintf(buf, sizeof(buf)-1, "%s", Http);
strmake(buf, Http, sizeof(buf)-2);

#if defined(_WIN32)
char cmd[1024];
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17475,7 +17475,7 @@ static my_bool query_str_variable(MYSQL *con,
is_null= row[0] == NULL;

if (!is_null)
my_snprintf(str, len, "%s", row[0]);
strmake(str, row[0], len-1);

mysql_free_result(rs);

Expand Down

0 comments on commit e365a24

Please sign in to comment.