Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset d16fc78 in mainline


Ignore:
Timestamp:
2010-11-26T01:07:42Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master
Children:
855e0d8
Parents:
1e27d85
Message:
  • verify the correctness of printf()'s and friends' attributes in compile time (use NVERIFY_PRINTF define to suppress the checks)
  • separate non-C99 compliant printf() checks to a standalone 'print5' test and u

se NVERIFY_PRINTF for it

  • improve 'print2' test (use portable constant macros and explicit types)
Files:
4 added
8 edited

Legend:

Unmodified
Added
Removed
  • boot/generic/include/printf.h

    r1e27d85 rd16fc78  
    3636#include <stdarg.h>
    3737
     38#ifndef NVERIFY_PRINTF
     39
     40#define PRINTF_ATTRIBUTE(start, end) \
     41        __attribute__((format(gnu_printf, start, end)))
     42
     43#else /* NVERIFY_PRINTF */
     44
     45#define PRINTF_ATTRIBUTE(start, end)
     46
     47#endif /* NVERIFY_PRINTF */
     48
    3849#define EOF  (-1)
    3950
    4051extern int puts(const char *);
    41 extern int printf(const char *, ...);
     52extern int printf(const char *, ...)
     53    PRINTF_ATTRIBUTE(1, 2);
    4254extern int vprintf(const char *, va_list);
    4355
  • kernel/generic/include/print.h

    r1e27d85 rd16fc78  
    3939#include <stdarg.h>
    4040
     41#ifndef NVERIFY_PRINTF
     42
     43#define PRINTF_ATTRIBUTE(start, end) \
     44        __attribute__((format(gnu_printf, start, end)))
     45
     46#else /* NVERIFY_PRINTF */
     47
     48#define PRINTF_ATTRIBUTE(start, end)
     49
     50#endif /* NVERIFY_PRINTF */
     51
    4152#define EOF  (-1)
    4253
    4354extern int puts(const char *s);
    44 extern int printf(const char *fmt, ...);
    45 extern int snprintf(char *str, size_t size, const char *fmt, ...);
     55extern int printf(const char *fmt, ...)
     56    PRINTF_ATTRIBUTE(1, 2);
     57extern int snprintf(char *str, size_t size, const char *fmt, ...)
     58    PRINTF_ATTRIBUTE(3, 4);
    4659
    4760extern int vprintf(const char *fmt, va_list ap);
  • kernel/test/print/print1.c

    r1e27d85 rd16fc78  
    4848        TPRINTF("Real output:     \"%8.10s\"\n\n", "very long text");
    4949       
    50         TPRINTF("Testing printf(\"%%s\", NULL):\n");
    51         TPRINTF("Expected output: \"(NULL)\"\n");
    52         TPRINTF("Real output:     \"%s\"\n\n", NULL);
    53        
    5450        return NULL;
    5551}
  • kernel/test/print/print2.c

    r1e27d85 rd16fc78  
    3232const char *test_print2(void)
    3333{
    34         TPRINTF("Testing printf(\"%%c %%3.2c %%-3.2c %%2.3c %%-2.3c\", 'a', 'b', 'c', 'd', 'e'):\n");
    35         TPRINTF("Expected output: [a] [  b] [c  ] [ d] [e ]\n");
    36         TPRINTF("Real output:     [%c] [%3.2c] [%-3.2c] [%2.3c] [%-2.3c]\n\n", 'a', 'b', 'c', 'd', 'e');
     34        TPRINTF("Testing printf(\"%%c\", 'a'):\n");
     35        TPRINTF("Expected output: [a]\n");
     36        TPRINTF("Real output:     [%c]\n\n", 'a');
    3737       
    3838        TPRINTF("Testing printf(\"%%d %%3.2d %%-3.2d %%2.3d %%-2.3d\", 1, 2, 3, 4, 5):\n");
     
    4848        TPRINTF("Real output:     [%#x] [%#5.3x] [%#-5.3x] [%#3.5x] [%#-3.5x]\n\n", 17, 18, 19, 20, 21);
    4949       
    50         unative_t nat = 0x12345678u;
     50        unative_t nat = UINTN_C(0x12345678);
    5151       
    52         TPRINTF("Testing printf(\"%%#" PRIx64 " %%#" PRIx32 " %%#" PRIx16 " %%#" PRIx8 " %%#" PRIxn " %%#" PRIx64 " %%s\", 0x1234567887654321ll, 0x12345678, 0x1234, 0x12, nat, 0x1234567887654321ull, \"Lovely string\"):\n");
     52        TPRINTF("Testing printf(\"%%#" PRIx64 " %%#" PRIx32 " %%#" PRIx16 " %%#" PRIx8 " %%#" PRIxn " %%#" PRIx64 " %%s\", (uint64_t) UINT64_C(0x1234567887654321), (uint32_t) UINT32_C(0x12345678), (uint16_t) UINT16_C(0x1234), (uint8_t) UINT8_C(0x12), nat, (uint64_t) UINT64_C(0x1234567887654321), \"Lovely string\"):\n");
    5353        TPRINTF("Expected output: [0x1234567887654321] [0x12345678] [0x1234] [0x12] [0x12345678] [0x1234567887654321] \"Lovely string\"\n");
    54         TPRINTF("Real output:     [%#" PRIx64 "] [%#" PRIx32 "] [%#" PRIx16 "] [%#" PRIx8 "] [%#" PRIxn "] [%#" PRIx64 "] \"%s\"\n\n", 0x1234567887654321ll, 0x12345678, 0x1234, 0x12, nat, 0x1234567887654321ull, "Lovely string");
     54        TPRINTF("Real output:     [%#" PRIx64 "] [%#" PRIx32 "] [%#" PRIx16 "] [%#" PRIx8 "] [%#" PRIxn "] [%#" PRIx64 "] \"%s\"\n\n", (uint64_t) UINT64_C(0x1234567887654321), (uint32_t) UINT32_C(0x12345678), (uint16_t) UINT16_C(0x1234), (uint8_t) UINT8_C(0x12), nat, (uint64_t) UINT64_C(0x1234567887654321), "Lovely string");
    5555       
    5656        return NULL;
  • kernel/test/print/print4.c

    r1e27d85 rd16fc78  
    4444                TPRINTF("  ");
    4545                for (index = 0; index < 32; index++)
    46                         TPRINTF("%lc", (wchar_t) ((group << 5) + index));
     46                        TPRINTF("%lc", (wint_t) ((group << 5) + index));
    4747               
    4848                TPRINTF("\n");
     
    5656                uint8_t index;
    5757                for (index = 0; index < 32; index++)
    58                         TPRINTF("%lc", (wchar_t) ((group << 5) + index));
     58                        TPRINTF("%lc", (wint_t) ((group << 5) + index));
    5959               
    6060                TPRINTF("\n");
  • uspace/app/tester/print/print1.c

    r1e27d85 rd16fc78  
    4949        TPRINTF("Real output:     \"%8.10s\"\n\n", "very long text");
    5050       
    51         TPRINTF("Testing printf(\"%%s\", NULL):\n");
    52         TPRINTF("Expected output: \"(NULL)\"\n");
    53         TPRINTF("Real output:     \"%s\"\n\n", NULL);
    54        
    5551        return NULL;
    5652}
  • uspace/app/tester/print/print2.c

    r1e27d85 rd16fc78  
    3333const char *test_print2(void)
    3434{
    35         TPRINTF("Testing printf(\"%%c %%3.2c %%-3.2c %%2.3c %%-2.3c\", 'a', 'b', 'c', 'd', 'e'):\n");
    36         TPRINTF("Expected output: [a] [  b] [c  ] [ d] [e ]\n");
    37         TPRINTF("Real output:     [%c] [%3.2c] [%-3.2c] [%2.3c] [%-2.3c]\n\n", 'a', 'b', 'c', 'd', 'e');
     35        TPRINTF("Testing printf(\"%%c\", 'a'):\n");
     36        TPRINTF("Expected output: [a]\n");
     37        TPRINTF("Real output:     [%c]\n\n", 'a');
    3838       
    3939        TPRINTF("Testing printf(\"%%d %%3.2d %%-3.2d %%2.3d %%-2.3d\", 1, 2, 3, 4, 5):\n");
  • uspace/app/tester/print/print4.c

    r1e27d85 rd16fc78  
    4545                TPRINTF("  ");
    4646                for (index = 0; index < 32; index++)
    47                         TPRINTF("%lc", (wchar_t) ((group << 5) + index));
     47                        TPRINTF("%lc", (wint_t) ((group << 5) + index));
    4848               
    4949                TPRINTF("\n");
     
    5757                uint8_t index;
    5858                for (index = 0; index < 32; index++)
    59                         TPRINTF("%lc", (wchar_t) ((group << 5) + index));
     59                        TPRINTF("%lc", (wint_t) ((group << 5) + index));
    6060               
    6161                TPRINTF("\n");
Note: See TracChangeset for help on using the changeset viewer.