Skip to content

Commit 5100773

Browse files
ParadoxV5vuvova
authored andcommitted
Tag my_vsnprintf.c with ATTRIBUTE_FORMAT
[Breaking] Good news: GCC now checks your `my_snprintf` (direct) calls (`-Wformat` was on); Bad news: The build process no longer lets your incorrect formats/arguments sneak past (`-Werror` was also on). As such, this commit also migrates all direct `my_snprintf` calls from the old specifiers to MDEV-21978’s new `-Wformat`-compatible suffixes. The next commits will cover non-direct calls to `my_snprintf`. (I call them “`my_snprintf` descendants”.) This commit does not update the ABI records because there’re more ABI “changes” to come – half a dozen `include/mysql/plugin_*.h.pp`s are now missing the new `__attribute__`s.
1 parent f361798 commit 5100773

File tree

10 files changed

+36
-27
lines changed

10 files changed

+36
-27
lines changed

client/mysql_upgrade.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ static void find_tool(char *tool_executable_name, const char *tool_name,
556556

557557
len= (int)(last_fn_libchar - self_name);
558558

559-
my_snprintf(tool_executable_name, FN_REFLEN, "%.*b%c%s",
559+
my_snprintf(tool_executable_name, FN_REFLEN, "%.*sB%c%s",
560560
len, self_name, FN_LIBCHAR, tool_name);
561561
}
562562

client/mysqlbinlog.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ print_use_stmt(PRINT_EVENT_INFO* pinfo, const Query_log_event *ev)
631631
return;
632632

633633
// In case of rewrite rule print USE statement for db_to
634-
my_fprintf(result_file, "use %`s%s\n", db_to, pinfo->delimiter);
634+
my_fprintf(result_file, "use %sQ%s\n", db_to, pinfo->delimiter);
635635

636636
// Copy the *original* db to pinfo to suppress emitting
637637
// of USE stmts by log_event print-functions.

client/mysqlcheck.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static int is_view(const char *table)
551551
int view;
552552
DBUG_ENTER("is_view");
553553

554-
my_snprintf(query, sizeof(query), "SHOW CREATE TABLE %`s", table);
554+
my_snprintf(query, sizeof(query), "SHOW CREATE TABLE %sQ", table);
555555
if (mysql_query(sock, query))
556556
{
557557
fprintf(stderr, "Failed to %s\n", query);
@@ -800,7 +800,7 @@ static int fix_table_storage_name(const char *name)
800800

801801
if (strncmp(name, "#mysql50#", 9))
802802
DBUG_RETURN(1);
803-
my_snprintf(qbuf, sizeof(qbuf), "RENAME TABLE %`s TO %`s",
803+
my_snprintf(qbuf, sizeof(qbuf), "RENAME TABLE %sQ TO %sQ",
804804
name, name + 9);
805805

806806
rc= run_query(qbuf, 1);
@@ -817,7 +817,7 @@ static int fix_database_storage_name(const char *name)
817817

818818
if (strncmp(name, "#mysql50#", 9))
819819
DBUG_RETURN(1);
820-
my_snprintf(qbuf, sizeof(qbuf), "ALTER DATABASE %`s UPGRADE DATA DIRECTORY "
820+
my_snprintf(qbuf, sizeof(qbuf), "ALTER DATABASE %sQ UPGRADE DATA DIRECTORY "
821821
"NAME", name);
822822
rc= run_query(qbuf, 1);
823823
if (!opt_silent)
@@ -1026,7 +1026,7 @@ static void insert_table_name(DYNAMIC_ARRAY *arr, char *in, size_t dblen)
10261026
{
10271027
char buf[NAME_LEN*2+2];
10281028
in[dblen]= 0;
1029-
my_snprintf(buf, sizeof(buf), "%`s.%`s", in, in + dblen + 1);
1029+
my_snprintf(buf, sizeof(buf), "%sQ.%sQ", in, in + dblen + 1);
10301030
insert_dynamic(arr, (uchar*) buf);
10311031
}
10321032

include/m_ctype.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,8 @@ const MY_CONTRACTIONS *my_charset_get_contractions(CHARSET_INFO *cs,
17761776
int level);
17771777

17781778
extern size_t my_vsnprintf_ex(CHARSET_INFO *cs, char *to, size_t n,
1779-
const char* fmt, va_list ap);
1779+
const char* fmt, va_list ap)
1780+
ATTRIBUTE_FORMAT(printf, 4, 0);
17801781

17811782
/*
17821783
Convert a string between two character sets.

include/my_sys.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,11 @@ extern FILE *my_fopen(const char *FileName,int Flags,myf MyFlags);
723723
extern FILE *my_fdopen(File Filedes,const char *name, int Flags,myf MyFlags);
724724
extern FILE *my_freopen(const char *path, const char *mode, FILE *stream);
725725
extern int my_fclose(FILE *fd,myf MyFlags);
726-
extern int my_vfprintf(FILE *stream, const char* format, va_list args);
726+
extern int my_vfprintf(FILE *stream, const char* format, va_list args)
727+
ATTRIBUTE_FORMAT(printf, 2, 0);
727728
extern const char* my_strerror(char *buf, size_t len, int nr);
728-
extern int my_fprintf(FILE *stream, const char* format, ...);
729+
extern int my_fprintf(FILE *stream, const char* format, ...)
730+
ATTRIBUTE_FORMAT(printf, 2, 3);
729731
extern File my_fileno(FILE *fd);
730732
extern int my_chsize(File fd,my_off_t newlength, int filler, myf MyFlags);
731733
extern int my_chmod(const char *name, mode_t mode, myf my_flags);

include/mysql/service_my_snprintf.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ extern "C" {
9393
#include <stdarg.h>
9494
#include <stdlib.h>
9595
#endif
96+
#include <my_attribute.h>
9697

9798
extern struct my_snprintf_service_st {
98-
size_t (*my_snprintf_type)(char*, size_t, const char*, ...);
99-
size_t (*my_vsnprintf_type)(char *, size_t, const char*, va_list);
99+
size_t (*my_snprintf_type)(char*, size_t, const char*, ...)
100+
ATTRIBUTE_FORMAT_FPTR(printf, 3, 4);
101+
size_t (*my_vsnprintf_type)(char *, size_t, const char*, va_list)
102+
ATTRIBUTE_FORMAT_FPTR(printf, 3, 0);
100103
} *my_snprintf_service;
101104

102105
#ifdef MYSQL_DYNAMIC_PLUGIN
@@ -106,8 +109,10 @@ extern struct my_snprintf_service_st {
106109

107110
#else
108111

109-
size_t my_snprintf(char* to, size_t n, const char* fmt, ...);
110-
size_t my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap);
112+
size_t my_snprintf(char* to, size_t n, const char* fmt, ...)
113+
ATTRIBUTE_FORMAT(printf, 3, 4);
114+
size_t my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
115+
ATTRIBUTE_FORMAT(printf, 3, 0);
111116

112117
#endif
113118

sql/log_event_client.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,7 +3303,7 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
33033303
{
33043304
switch (type) {
33053305
case MYSQL_TYPE_LONG:
3306-
my_snprintf(typestr, typestr_length, "%s", "INT");
3306+
my_snprintf(typestr, typestr_length, "INT");
33073307
break;
33083308
case MYSQL_TYPE_TINY:
33093309
my_snprintf(typestr, typestr_length, "TINYINT");
@@ -3373,20 +3373,21 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
33733373
break;
33743374
case MYSQL_TYPE_BLOB:
33753375
{
3376-
bool is_text= (cs && cs->number != my_charset_bin.number);
3377-
const char *names[5][2] = {
3378-
{"INVALID_BLOB(%d)", "INVALID_TEXT(%d)"},
3379-
{"TINYBLOB", "TINYTEXT"},
3380-
{"BLOB", "TEXT"},
3381-
{"MEDIUMBLOB", "MEDIUMTEXT"},
3382-
{"LONGBLOB", "LONGTEXT"}
3376+
const char *type_name=
3377+
(cs && cs->number != my_charset_bin.number) ? "TEXT" : "BLOB";
3378+
const char *names[5]= {
3379+
NullS,
3380+
"TINY",
3381+
"",
3382+
"MEDIUM",
3383+
"LONG"
33833384
};
33843385
unsigned char size= **meta_ptr;
33853386

33863387
if (size == 0 || size > 4)
3387-
my_snprintf(typestr, typestr_length, names[0][is_text], size);
3388+
my_snprintf(typestr, typestr_length, "INVALID_%s(%d)", type_name, size);
33883389
else
3389-
my_snprintf(typestr, typestr_length, names[**meta_ptr][is_text]);
3390+
my_snprintf(typestr, typestr_length, "%s%s", names[size], type_name);
33903391

33913392
(*meta_ptr)++;
33923393
}
@@ -3423,7 +3424,7 @@ static void get_type_name(uint type, unsigned char** meta_ptr,
34233424
"MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"
34243425
};
34253426
if (geometry_type < 8)
3426-
my_snprintf(typestr, typestr_length, names[geometry_type]);
3427+
my_snprintf(typestr, typestr_length, "%s", names[geometry_type]);
34273428
else
34283429
my_snprintf(typestr, typestr_length, "INVALID_GEOMETRY_TYPE(%u)",
34293430
geometry_type);

sql/sql_admin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
711711
protocol->store(&table_name, system_charset_info);
712712
protocol->store(operator_name, system_charset_info);
713713
protocol->store(&error_clex_str, system_charset_info);
714-
length= my_snprintf(buff, sizeof(buff),
714+
length= my_snprintf(buff, sizeof(buff), "%s",
715715
ER_THD(thd, ER_PARTITION_DOES_NOT_EXIST));
716716
protocol->store(buff, length, system_charset_info);
717717
if(protocol->write())

sql/sql_db.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ mysql_rm_db_internal(THD *thd, const Lex_ident_db &db, bool if_exists,
12371237
if (ha_table_exists(thd, &tbl->db, &tbl->table_name))
12381238
continue;
12391239

1240-
tbl_name_len= my_snprintf(quoted_name, sizeof(quoted_name), "%`s",
1240+
tbl_name_len= my_snprintf(quoted_name, sizeof(quoted_name), "%sQ",
12411241
tbl->table_name.str);
12421242
tbl_name_len++; /* +1 for the comma */
12431243
if (query_pos + tbl_name_len + 1 >= query_end)

storage/example/ha_example.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ static int show_func_example(MYSQL_THD thd, struct st_mysql_show_var *var,
10701070
var->value= buf; // it's of SHOW_VAR_FUNC_BUFF_SIZE bytes
10711071
my_snprintf(buf, SHOW_VAR_FUNC_BUFF_SIZE,
10721072
"enum_var is %lu, ulong_var is %lu, int_var is %d, "
1073-
"double_var is %f, %.6b", // %b is a MariaDB/MySQL extension
1073+
"double_var is %f, %.6sB", // %sB is a MariaDB extension
10741074
srv_enum_var, srv_ulong_var, THDVAR(thd, int_var),
10751075
srv_double_var, "really");
10761076
return 0;

0 commit comments

Comments
 (0)