FORTIFY_SOURCE assertions replacement

mordae mordae at thirdcms.org
Sat Dec 9 11:51:21 PST 2006


Hi,

        I'm not sure whether exiting app whenever fwrite() or something fails.
It could be non-critical for application run and if it decided to ignore it,
we shouldn't enforce author to add unneeded checking. We should, however, be
able to learn it. So, what about creating small header file like this:

#ifndef _DEBUG_FORTIFY
#define _DEBUG_FORTIFY 1

#include <stdio.h>

/*
 * Original functions:
 *   int fputc(int c, FILE *stream);
 *   int fputs(const char *s, FILE *stream);
 *   int putc(int c, FILE *stream);
 *   int putchar(int c);
 *   int puts(const char *s);
 *
 * All negative results means errors.
 */

/* Static storage of results, required for checks. */
#ifndef __DEBUG_FORTIFY_RESULT
# define __DEBUG_FORTIFY_RESULT 1
  static int __debug_fortify_result;
#endif

#define __DEBUG_FORTIFY_MSG(MSG) \
        (puts("<fortify> [" __PRETTY_FUNCTION__ "] " MSG, stderr) * 0 || \
                __debug_fortify_result)

#define putchar(C) \
        ((__debug_fortify_result = putchar(C)) < 0
                ? __DEBUG_FORTIFY_MSG("putchar(" #C ")")
                : __debug_fortify_result)

#define putc(C, F) \
        ((__debug_fortify_result = putc(C, F)) < 0
                ? __DEBUG_FORTIFY_MSG("putc(" #C ", " #F ")")
                : __debug_fortify_result)

#define puts(S) \
        ((__debug_fortify_result = puts(S)) < 0
                ? __DEBUG_FORTIFY_MSG("puts(" #S ")")
                : __debug_fortify_result)


#define fputc(C, F) \
        ((__debug_fortify_result = fputc(C, F)) < 0
                ? __DEBUG_FORTIFY_MSG("fputc(" #C ", " #F ")")
                : __debug_fortify_result)

#define fputs(S) \
        ((__debug_fortify_result = fputs(S, F)) < 0
                ? __DEBUG_FORTIFY_MSG("fputs(" #S ", " #F ")")
                : __debug_fortify_result)

#endif

And then just include it from files giving FORTIFY_SOURCE warnings about these
functions. It won't break anything, but will still make it possible to report
these to be fixed.



More information about the hlfs-dev mailing list