Skip to content

Commit 304726a

Browse files
committedSep 7, 2022
Move gcc-style __attribute__ macros to config.h.in
Renamed __malloc -> sudo_malloclike, __printflike -> sudo_printflike, __printf0like -> sudo_printf0like. Add sudo_noreturn instead of __attribute__((__noreturn__)). We do not use stdnoreturn.h since it has been deprecated in C23 in favor of the [[noreturn]] attribute.
1 parent c341608 commit 304726a

21 files changed

+185
-134
lines changed
 

‎config.h.in

+47-1
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,53 @@
14511451
code using `volatile' can become incorrect without. Disable with care. */
14521452
#undef volatile
14531453

1454-
/* Symbol visibility controls */
1454+
#ifndef __GNUC_PREREQ__
1455+
# ifdef __GNUC__
1456+
# define __GNUC_PREREQ__(ma, mi) \
1457+
((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
1458+
# else
1459+
# define __GNUC_PREREQ__(ma, mi) 0
1460+
# endif
1461+
#endif
1462+
1463+
/* Define away __attribute__ for non-gcc or old gcc. */
1464+
#if !defined(__attribute__) && !__GNUC_PREREQ__(2, 5)
1465+
# define __attribute__(x)
1466+
#endif
1467+
1468+
/* For functions that call exit() directly. */
1469+
#if __GNUC_PREREQ__(2, 5)
1470+
# define sudo_noreturn __attribute__((__noreturn__))
1471+
#else
1472+
# define sudo_noreturn
1473+
#endif
1474+
1475+
/* For malloc-like functions that return uninitialized or zeroed memory. */
1476+
#if __GNUC_PREREQ__(2, 96)
1477+
# define sudo_malloclike __attribute__((__malloc__))
1478+
#else
1479+
# define sudo_malloclike
1480+
#endif
1481+
1482+
/* Compile-time checking for function arguments that must not be NULL. */
1483+
#if __GNUC_PREREQ__(3, 3)
1484+
# define sudo_attr_nonnull(_a) __attribute__((__nonnull__ (_a)))
1485+
#else
1486+
# define sudo_attr_nonnull(_a)
1487+
#endif
1488+
1489+
/* For catching format string mismatches. */
1490+
#if __GNUC_PREREQ__(2, 7)
1491+
# define sudo_printflike(_f, _v) __attribute__((__format__ (__printf__, _f, _v))) sudo_attr_nonnull(_f)
1492+
# define sudo_printf0like(_f, _v) __attribute__((__format__ (__printf__, _f, _v)))
1493+
# define sudo_attr_fmt_arg(_f) __attribute__((__format_arg__ (_f)))
1494+
#else
1495+
# define sudo_printflike(_f, _v)
1496+
# define sudo_printf0like(_f, _v)
1497+
# define sudo_attr_fmt_arg(_f)
1498+
#endif
1499+
1500+
/* Symbol visibility controls. */
14551501
#ifdef HAVE_DSO_VISIBILITY
14561502
# if defined(__GNUC__)
14571503
# define sudo_dso_public __attribute__((__visibility__("default")))

‎configure.ac

+47-1
Original file line numberDiff line numberDiff line change
@@ -5533,7 +5533,53 @@ dnl
55335533
AH_TOP([#ifndef SUDO_CONFIG_H
55345534
#define SUDO_CONFIG_H])
55355535

5536-
AH_BOTTOM([/* Symbol visibility controls */
5536+
AH_BOTTOM([#ifndef __GNUC_PREREQ__
5537+
# ifdef __GNUC__
5538+
# define __GNUC_PREREQ__(ma, mi) \
5539+
((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
5540+
# else
5541+
# define __GNUC_PREREQ__(ma, mi) 0
5542+
# endif
5543+
#endif
5544+
5545+
/* Define away __attribute__ for non-gcc or old gcc. */
5546+
#if !defined(__attribute__) && !__GNUC_PREREQ__(2, 5)
5547+
# define __attribute__(x)
5548+
#endif
5549+
5550+
/* For functions that call exit() directly. */
5551+
#if __GNUC_PREREQ__(2, 5)
5552+
# define sudo_noreturn __attribute__((__noreturn__))
5553+
#else
5554+
# define sudo_noreturn
5555+
#endif
5556+
5557+
/* For malloc-like functions that return uninitialized or zeroed memory. */
5558+
#if __GNUC_PREREQ__(2, 96)
5559+
# define sudo_malloclike __attribute__((__malloc__))
5560+
#else
5561+
# define sudo_malloclike
5562+
#endif
5563+
5564+
/* Compile-time checking for function arguments that must not be NULL. */
5565+
#if __GNUC_PREREQ__(3, 3)
5566+
# define sudo_attr_nonnull(_a) __attribute__((__nonnull__ (_a)))
5567+
#else
5568+
# define sudo_attr_nonnull(_a)
5569+
#endif
5570+
5571+
/* For catching format string mismatches. */
5572+
#if __GNUC_PREREQ__(2, 7)
5573+
# define sudo_printflike(_f, _v) __attribute__((__format__ (__printf__, _f, _v))) sudo_attr_nonnull(_f)
5574+
# define sudo_printf0like(_f, _v) __attribute__((__format__ (__printf__, _f, _v)))
5575+
# define sudo_attr_fmt_arg(_f) __attribute__((__format_arg__ (_f)))
5576+
#else
5577+
# define sudo_printflike(_f, _v)
5578+
# define sudo_printf0like(_f, _v)
5579+
# define sudo_attr_fmt_arg(_f)
5580+
#endif
5581+
5582+
/* Symbol visibility controls. */
55375583
#ifdef HAVE_DSO_VISIBILITY
55385584
# if defined(__GNUC__)
55395585
# define sudo_dso_public __attribute__((__visibility__("default")))

‎include/sudo_compat.h

+4-52
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,6 @@
3737
* Macros and functions that may be missing on some operating systems.
3838
*/
3939

40-
#ifndef __GNUC_PREREQ__
41-
# ifdef __GNUC__
42-
# define __GNUC_PREREQ__(ma, mi) \
43-
((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
44-
# else
45-
# define __GNUC_PREREQ__(ma, mi) 0
46-
# endif
47-
#endif
48-
49-
/* Define away __attribute__ for non-gcc or old gcc */
50-
#if !defined(__attribute__) && !__GNUC_PREREQ__(2, 5)
51-
# define __attribute__(x)
52-
#endif
53-
54-
/* For malloc-like functions that return uninitialized or zeroed memory. */
55-
#ifndef __malloc
56-
# if __GNUC_PREREQ__(2, 96)
57-
# define __malloc __attribute__((__malloc__))
58-
# else
59-
# define __malloc
60-
# endif
61-
#endif
62-
63-
/* For catching format string mismatches */
64-
#ifndef __printflike
65-
# if __GNUC_PREREQ__(3, 3)
66-
# define __printflike(f, v) __attribute__((__format__ (__printf__, f, v))) __attribute__((__nonnull__ (f)))
67-
# elif __GNUC_PREREQ__(2, 7)
68-
# define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
69-
# else
70-
# define __printflike(f, v)
71-
# endif
72-
#endif
73-
#ifndef __printf0like
74-
# if __GNUC_PREREQ__(2, 7)
75-
# define __printf0like(f, v) __attribute__((__format__ (__printf__, f, v)))
76-
# else
77-
# define __printf0like(f, v)
78-
# endif
79-
#endif
80-
#ifndef __format_arg
81-
# if __GNUC_PREREQ__(2, 7)
82-
# define __format_arg(f) __attribute__((__format_arg__ (f)))
83-
# else
84-
# define __format_arg(f)
85-
# endif
86-
#endif
87-
8840
#ifdef HAVE_FALLTHROUGH_ATTRIBUTE
8941
# define FALLTHROUGH __attribute__((__fallthrough__))
9042
#else
@@ -493,22 +445,22 @@ sudo_dso_public int sudo_futimens(int fd, const struct timespec *times);
493445
# define futimens(_a, _b) sudo_futimens((_a), (_b))
494446
#endif /* HAVE_FUTIMENS */
495447
#if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
496-
sudo_dso_public int sudo_snprintf(char *str, size_t n, char const *fmt, ...) __printflike(3, 4);
448+
sudo_dso_public int sudo_snprintf(char *str, size_t n, char const *fmt, ...) sudo_printflike(3, 4);
497449
# undef snprintf
498450
# define snprintf sudo_snprintf
499451
#endif /* HAVE_SNPRINTF */
500452
#if !defined(HAVE_VSNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
501-
sudo_dso_public int sudo_vsnprintf(char *str, size_t n, const char *fmt, va_list ap) __printflike(3, 0);
453+
sudo_dso_public int sudo_vsnprintf(char *str, size_t n, const char *fmt, va_list ap) sudo_printflike(3, 0);
502454
# undef vsnprintf
503455
# define vsnprintf sudo_vsnprintf
504456
#endif /* HAVE_VSNPRINTF */
505457
#if !defined(HAVE_ASPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
506-
sudo_dso_public int sudo_asprintf(char **str, char const *fmt, ...) __printflike(2, 3);
458+
sudo_dso_public int sudo_asprintf(char **str, char const *fmt, ...) sudo_printflike(2, 3);
507459
# undef asprintf
508460
# define asprintf sudo_asprintf
509461
#endif /* HAVE_ASPRINTF */
510462
#if !defined(HAVE_VASPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
511-
sudo_dso_public int sudo_vasprintf(char **str, const char *fmt, va_list ap) __printflike(2, 0);
463+
sudo_dso_public int sudo_vasprintf(char **str, const char *fmt, va_list ap) sudo_printflike(2, 0);
512464
# undef vasprintf
513465
# define vasprintf sudo_vasprintf
514466
#endif /* HAVE_VASPRINTF */

‎include/sudo_debug.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ sudo_dso_public int sudo_debug_get_active_instance_v1(void);
265265
sudo_dso_public int sudo_debug_get_fds_v1(unsigned char **fds);
266266
sudo_dso_public int sudo_debug_get_instance_v1(const char *program);
267267
sudo_dso_public int sudo_debug_parse_flags_v1(struct sudo_conf_debug_file_list *debug_files, const char *entry);
268-
sudo_dso_public void sudo_debug_printf2_v1(const char *func, const char *file, int line, int level, const char *fmt, ...) __printf0like(5, 6);
269-
sudo_dso_public void sudo_debug_printf_nvm_v1(int pri, const char *fmt, ...) __printf0like(2, 3);
268+
sudo_dso_public void sudo_debug_printf2_v1(const char *func, const char *file, int line, int level, const char *fmt, ...) sudo_printf0like(5, 6);
269+
sudo_dso_public void sudo_debug_printf_nvm_v1(int pri, const char *fmt, ...) sudo_printf0like(2, 3);
270270
sudo_dso_public int sudo_debug_register_v1(const char *program, const char *const subsystems[], unsigned int ids[], struct sudo_conf_debug_file_list *debug_files);
271271
sudo_dso_public int sudo_debug_register_v2(const char *program, const char *const subsystems[], unsigned int ids[], struct sudo_conf_debug_file_list *debug_files, int minfd);
272272
sudo_dso_public int sudo_debug_set_active_instance_v1(int inst);
273273
sudo_dso_public void sudo_debug_update_fd_v1(int ofd, int nfd);
274-
sudo_dso_public void sudo_debug_vprintf2_v1(const char *func, const char *file, int line, int level, const char *fmt, va_list ap) __printf0like(5, 0);
274+
sudo_dso_public void sudo_debug_vprintf2_v1(const char *func, const char *file, int line, int level, const char *fmt, va_list ap) sudo_printf0like(5, 0);
275275
sudo_dso_public void sudo_debug_write2_v1(int fd, const char *func, const char *file, int line, const char *str, int len, int errnum);
276276
sudo_dso_public bool sudo_debug_needed_v1(int level);
277277

‎include/sudo_fatal.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,20 @@ typedef bool (*sudo_warn_setlocale_t)(bool, int *);
164164

165165
sudo_dso_public int sudo_fatal_callback_deregister_v1(sudo_fatal_callback_t func);
166166
sudo_dso_public int sudo_fatal_callback_register_v1(sudo_fatal_callback_t func);
167-
sudo_dso_public char *sudo_warn_gettext_v1(const char *domainname, const char *msgid) __format_arg(2);
167+
sudo_dso_public char *sudo_warn_gettext_v1(const char *domainname, const char *msgid) sudo_attr_fmt_arg(2);
168168
sudo_dso_public void sudo_warn_set_locale_func_v1(sudo_warn_setlocale_t func);
169-
sudo_dso_public void sudo_fatal_nodebug_v1(const char *fmt, ...) __printf0like(1, 2) __attribute__((__noreturn__));
170-
sudo_dso_public void sudo_fatalx_nodebug_v1(const char *fmt, ...) __printflike(1, 2) __attribute__((__noreturn__));
171-
sudo_dso_public void sudo_gai_fatal_nodebug_v1(int errnum, const char *fmt, ...) __printflike(2, 3) __attribute__((__noreturn__));
172-
sudo_dso_public void sudo_vfatal_nodebug_v1(const char *fmt, va_list ap) __printf0like(1, 0) __attribute__((__noreturn__));
173-
sudo_dso_public void sudo_vfatalx_nodebug_v1(const char *fmt, va_list ap) __printflike(1, 0) __attribute__((__noreturn__));
174-
sudo_dso_public void sudo_gai_vfatal_nodebug_v1(int errnum, const char *fmt, va_list ap) __printflike(2, 0) __attribute__((__noreturn__));
175-
sudo_dso_public void sudo_warn_nodebug_v1(const char *fmt, ...) __printf0like(1, 2);
176-
sudo_dso_public void sudo_warnx_nodebug_v1(const char *fmt, ...) __printflike(1, 2);
177-
sudo_dso_public void sudo_gai_warn_nodebug_v1(int errnum, const char *fmt, ...) __printflike(2, 3);
178-
sudo_dso_public void sudo_vwarn_nodebug_v1(const char *fmt, va_list ap) __printf0like(1, 0);
179-
sudo_dso_public void sudo_vwarnx_nodebug_v1(const char *fmt, va_list ap) __printflike(1, 0);
180-
sudo_dso_public void sudo_gai_vwarn_nodebug_v1(int errnum, const char *fmt, va_list ap) __printflike(2, 0);
169+
sudo_dso_public sudo_noreturn void sudo_fatal_nodebug_v1(const char *fmt, ...) sudo_printf0like(1, 2);
170+
sudo_dso_public sudo_noreturn void sudo_fatalx_nodebug_v1(const char *fmt, ...) sudo_printflike(1, 2);
171+
sudo_dso_public sudo_noreturn void sudo_gai_fatal_nodebug_v1(int errnum, const char *fmt, ...) sudo_printflike(2, 3);
172+
sudo_dso_public sudo_noreturn void sudo_vfatal_nodebug_v1(const char *fmt, va_list ap) sudo_printf0like(1, 0);
173+
sudo_dso_public sudo_noreturn void sudo_vfatalx_nodebug_v1(const char *fmt, va_list ap) sudo_printflike(1, 0);
174+
sudo_dso_public sudo_noreturn void sudo_gai_vfatal_nodebug_v1(int errnum, const char *fmt, va_list ap) sudo_printflike(2, 0);
175+
sudo_dso_public void sudo_warn_nodebug_v1(const char *fmt, ...) sudo_printf0like(1, 2);
176+
sudo_dso_public void sudo_warnx_nodebug_v1(const char *fmt, ...) sudo_printflike(1, 2);
177+
sudo_dso_public void sudo_gai_warn_nodebug_v1(int errnum, const char *fmt, ...) sudo_printflike(2, 3);
178+
sudo_dso_public void sudo_vwarn_nodebug_v1(const char *fmt, va_list ap) sudo_printf0like(1, 0);
179+
sudo_dso_public void sudo_vwarnx_nodebug_v1(const char *fmt, va_list ap) sudo_printflike(1, 0);
180+
sudo_dso_public void sudo_gai_vwarn_nodebug_v1(int errnum, const char *fmt, va_list ap) sudo_printflike(2, 0);
181181
sudo_dso_public void sudo_warn_set_conversation_v1(sudo_conv_t conv);
182182

183183
#define sudo_fatal_callback_deregister(_a) sudo_fatal_callback_deregister_v1((_a))

‎include/sudo_lbuf.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ typedef int (*sudo_lbuf_output_t)(const char *);
3838

3939
sudo_dso_public void sudo_lbuf_init_v1(struct sudo_lbuf *lbuf, sudo_lbuf_output_t output, int indent, const char *continuation, int cols);
4040
sudo_dso_public void sudo_lbuf_destroy_v1(struct sudo_lbuf *lbuf);
41-
sudo_dso_public bool sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...) __printflike(2, 3);
42-
sudo_dso_public bool sudo_lbuf_append_quoted_v1(struct sudo_lbuf *lbuf, const char *set, const char *fmt, ...) __printflike(3, 4);
41+
sudo_dso_public bool sudo_lbuf_append_v1(struct sudo_lbuf *lbuf, const char *fmt, ...) sudo_printflike(2, 3);
42+
sudo_dso_public bool sudo_lbuf_append_quoted_v1(struct sudo_lbuf *lbuf, const char *set, const char *fmt, ...) sudo_printflike(3, 4);
4343
sudo_dso_public void sudo_lbuf_print_v1(struct sudo_lbuf *lbuf);
4444
sudo_dso_public bool sudo_lbuf_error_v1(struct sudo_lbuf *lbuf);
4545
sudo_dso_public void sudo_lbuf_clearerr_v1(struct sudo_lbuf *lbuf);

‎include/sudo_util.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ sudo_dso_public bool sudo_mkdir_parents_v1(const char *path, uid_t uid, gid_t gi
249249
#define sudo_mkdir_parents(_a, _b, _c, _d, _e) sudo_mkdir_parents_v1((_a), (_b), (_c), (_d), (_e))
250250

251251
/* mmap_alloc.c */
252-
sudo_dso_public void *sudo_mmap_alloc_v1(size_t size) __malloc;
252+
sudo_dso_public void *sudo_mmap_alloc_v1(size_t size) sudo_malloclike;
253253
#define sudo_mmap_alloc(_a) sudo_mmap_alloc_v1(_a)
254-
sudo_dso_public void *sudo_mmap_allocarray_v1(size_t count, size_t size) __malloc;
254+
sudo_dso_public void *sudo_mmap_allocarray_v1(size_t count, size_t size) sudo_malloclike;
255255
#define sudo_mmap_allocarray(_a, _b) sudo_mmap_allocarray_v1((_a), (_b))
256256
sudo_dso_public char *sudo_mmap_strdup_v1(const char *str);
257257
#define sudo_mmap_strdup(_a) sudo_mmap_strdup_v1(_a)
@@ -271,7 +271,7 @@ sudo_dso_public void initprogname2(const char *, const char * const *);
271271

272272
/* rcstr.c */
273273
sudo_dso_public char *sudo_rcstr_dup(const char *src);
274-
sudo_dso_public char *sudo_rcstr_alloc(size_t len) __malloc;
274+
sudo_dso_public char *sudo_rcstr_alloc(size_t len) sudo_malloclike;
275275
sudo_dso_public char *sudo_rcstr_addref(const char *s);
276276
sudo_dso_public void sudo_rcstr_delref(const char *s);
277277

‎lib/eventlog/eventlog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ closefrom_nodebug(int lowfd)
349349

350350
#define MAX_MAILFLAGS 63
351351

352-
static void __attribute__((__noreturn__))
352+
static sudo_noreturn void
353353
exec_mailer(int pipein)
354354
{
355355
const struct eventlog_config *evl_conf = eventlog_getconf();

‎plugins/sudoers/check_aliases.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct alias_warned {
3838
};
3939
SLIST_HEAD(alias_warned_list, alias_warned);
4040

41-
static bool alias_warnx(const char *file, int line, int column, bool strict, bool quiet, const char *fmt, ...) __printflike(6, 7);
41+
static bool alias_warnx(const char *file, int line, int column, bool strict, bool quiet, const char *fmt, ...) sudo_printflike(6, 7);
4242

4343
static bool
4444
alias_warned(struct alias_warned_list *warned, char *name)

0 commit comments

Comments
 (0)