Skip to content

Commit e9f1a6c

Browse files
committed
Merge branch 'jk/gcc-function-attributes'
Use the function attributes extension to catch mistakes in use of our own variadic functions that use NULL sentinel at the end (i.e. like execl(3)) and format strings (i.e. like printf(3)). * jk/gcc-function-attributes: Add the LAST_ARG_MUST_BE_NULL macro wt-status: use "format" function attribute for status_printf use "sentinel" function attribute for variadic lists add missing "format" function attributes
2 parents d0b3fa8 + 9fe3edc commit e9f1a6c

File tree

10 files changed

+21
-5
lines changed

10 files changed

+21
-5
lines changed

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern int advice_object_name_warning;
2121
extern int advice_rm_hints;
2222

2323
int git_default_advice_config(const char *var, const char *value);
24+
__attribute__((format (printf, 1, 2)))
2425
void advise(const char *advice, ...);
2526
int error_resolve_conflict(const char *me);
2627
extern void NORETURN die_resolve_conflict(const char *me);

argv-array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void argv_array_init(struct argv_array *);
1515
void argv_array_push(struct argv_array *, const char *);
1616
__attribute__((format (printf,2,3)))
1717
void argv_array_pushf(struct argv_array *, const char *fmt, ...);
18+
LAST_ARG_MUST_BE_NULL
1819
void argv_array_pushl(struct argv_array *, ...);
1920
void argv_array_pop(struct argv_array *);
2021
void argv_array_clear(struct argv_array *);

builtin/revert.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static int option_parse_x(const struct option *opt,
5454
return 0;
5555
}
5656

57+
LAST_ARG_MUST_BE_NULL
5758
static void verify_opt_compatible(const char *me, const char *base_opt, ...)
5859
{
5960
const char *this_opt;
@@ -70,6 +71,7 @@ static void verify_opt_compatible(const char *me, const char *base_opt, ...)
7071
die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
7172
}
7273

74+
LAST_ARG_MUST_BE_NULL
7375
static void verify_opt_mutually_compatible(const char *me, ...)
7476
{
7577
const char *opt1, *opt2 = NULL;

exec_cmd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern const char *git_exec_path(void);
77
extern void setup_path(void);
88
extern const char **prepare_git_cmd(const char **argv);
99
extern int execv_git_cmd(const char **argv); /* NULL terminated */
10+
LAST_ARG_MUST_BE_NULL
1011
extern int execl_git_cmd(const char *cmd, ...);
1112
extern const char *system_path(const char *path);
1213

git-compat-util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ extern char *gitbasename(char *);
303303
#endif
304304
#endif
305305

306+
/* The sentinel attribute is valid from gcc version 4.0 */
307+
#if defined(__GNUC__) && (__GNUC__ >= 4)
308+
#define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
309+
#else
310+
#define LAST_ARG_MUST_BE_NULL
311+
#endif
312+
306313
#include "compat/bswap.h"
307314

308315
#ifdef USE_WILDMATCH

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int finish_command(struct child_process *);
4646
int run_command(struct child_process *);
4747

4848
extern char *find_hook(const char *name);
49+
LAST_ARG_MUST_BE_NULL
4950
extern int run_hook(const char *index_file, const char *name, ...);
5051

5152
#define RUN_COMMAND_NO_STDIN 1

trace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static void trace_vprintf(const char *key, const char *fmt, va_list ap)
7575
strbuf_release(&buf);
7676
}
7777

78+
__attribute__((format (printf, 2, 3)))
7879
static void trace_printf_key(const char *key, const char *fmt, ...)
7980
{
8081
va_list ap;

transport-helper.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ int transport_helper_init(struct transport *transport, const char *name)
982982
#define PBUFFERSIZE 8192
983983

984984
/* Print bidirectional transfer loop debug message. */
985+
__attribute__((format (printf, 1, 2)))
985986
static void transfer_debug(const char *fmt, ...)
986987
{
987988
va_list args;
@@ -1067,7 +1068,7 @@ static int udt_do_read(struct unidirectional_transfer *t)
10671068
return -1;
10681069
} else if (bytes == 0) {
10691070
transfer_debug("%s EOF (with %i bytes in buffer)",
1070-
t->src_name, t->bufuse);
1071+
t->src_name, (int)t->bufuse);
10711072
t->state = SSTATE_FLUSHING;
10721073
} else if (bytes > 0) {
10731074
t->bufuse += bytes;

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int utf8_strwidth(const char *string);
1010
int is_utf8(const char *text);
1111
int is_encoding_utf8(const char *name);
1212
int same_encoding(const char *, const char *);
13+
__attribute__((format (printf, 2, 3)))
1314
int utf8_fprintf(FILE *, const char *, ...);
1415

1516
void strbuf_add_wrapped_text(struct strbuf *buf,

wt-status.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ void wt_status_get_state(struct wt_status_state *state, int get_detached_from);
9696
void wt_shortstatus_print(struct wt_status *s);
9797
void wt_porcelain_print(struct wt_status *s);
9898

99-
void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...)
100-
;
101-
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...)
102-
;
99+
__attribute__((format (printf, 3, 4)))
100+
void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...);
101+
__attribute__((format (printf, 3, 4)))
102+
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
103103

104104
#endif /* STATUS_H */

0 commit comments

Comments
 (0)