Skip to main content
bug fix: semicolon
Source Link
#define ARRAY_SIZE(a)       (sizeof(a) / sizeof((a)[0]))

#define sbprintf(buff, written, fmt, ...)                               \
        swnprintf(buff, written, ARRAY_SIZE(buff), fmt, ##__VA_ARGS__);
#define ARRAY_SIZE(a)       (sizeof(a) / sizeof((a)[0]))

#define sbprintf(buff, written, fmt, ...)                               \
        swnprintf(buff, written, ARRAY_SIZE(buff), fmt, ##__VA_ARGS__);
#define ARRAY_SIZE(a)       (sizeof(a) / sizeof((a)[0]))

#define sbprintf(buff, written, fmt, ...)                               \
        swnprintf(buff, written, ARRAY_SIZE(buff), fmt, ##__VA_ARGS__)
warn_unused_result
Source Link

EDIT:

When using __attribute__((warn_unused_result)), the macro sbprintf silences the warning because of the way it works; to warn the user, the following code can be used:

#define ARRAY_SIZE(a)       (sizeof(a) / sizeof((a)[0]))

#define sbprintf(buff, written, fmt, ...)                               \
        swnprintf(buff, written, ARRAY_SIZE(buff), fmt, ##__VA_ARGS__);

Note: GCC 8 warns (-Werror=sizeof-pointer-div) if buff is not an array without needing a static_assert, when using ARRAY_SIZE defined this way. If using an old version of GCC, you will need to use some tricks to block compilation.

EDIT:

When using __attribute__((warn_unused_result)), the macro sbprintf silences the warning because of the way it works; to warn the user, the following code can be used:

#define ARRAY_SIZE(a)       (sizeof(a) / sizeof((a)[0]))

#define sbprintf(buff, written, fmt, ...)                               \
        swnprintf(buff, written, ARRAY_SIZE(buff), fmt, ##__VA_ARGS__);

Note: GCC 8 warns (-Werror=sizeof-pointer-div) if buff is not an array without needing a static_assert, when using ARRAY_SIZE defined this way. If using an old version of GCC, you will need to use some tricks to block compilation.

added 191 characters in body; deleted 1 character in body
Source Link

The return value should always be used:

__attribute__((warn_unused_result))

Add checks to the format and varargs:

__attribute__((format(printf, 4, 5)))

The return value should always be used:

__attribute__((warn_unused_result))

Add checks to the format and varargs:

__attribute__((format(printf, 4, 5)))
Fix bug; deleted 57 characters in body; added 29 characters in body; added 86 characters in body; added 22 characters in body
Source Link
Loading
ptrdiff_t
Source Link
Loading
ptrdiff_t
Source Link
Loading
rename
Source Link
Loading
rename
Source Link
Loading
forgot const
Source Link
Loading
rename
Source Link
Loading
Fix includes
Source Link
Loading
add include
Source Link
Loading
Move to function; set errno; include others suggestions
Source Link
Loading
Rollback to Revision 5
Source Link
Loading
Rollback to Revision 8
Source Link
Loading
ptrdiff_t
Source Link
Loading
Rollback to Revision 8
Source Link
Loading
actual length
Source Link
Loading
unsigned
Source Link
Loading
unsigned
Source Link
Loading
new code: add chux suggestion, and set errno
Source Link
Loading
added 229 characters in body
Source Link
Loading
Safety around buff; deleted 1 character in body
Source Link
Loading
added 4 characters in body
Source Link
Loading
added 5 characters in body
Source Link
Loading