fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

test_mbsnrtowcs_dynamic.c (590B)


      1 #include "common.h"
      2 
      3 #include <wchar.h>
      4 #include <string.h>
      5 
      6 int main(int argc, char** argv) {
      7   wchar_t buffer[4] = {0};
      8   const char *src = "ABCDEFGHIJ";
      9   const char *srcp = src;
     10   mbstate_t st;
     11   memset(&st, 0, sizeof(st));
     12 
     13   /* Safe: convert up to 2 source bytes into at most 2 wide chars */
     14   srcp = src;
     15   mbsnrtowcs(buffer, &srcp, 2, 2, &st);
     16 
     17   /* Unsafe: ask to write argc (10) wide chars into 4-element buffer. */
     18   srcp = src;
     19   memset(&st, 0, sizeof(st));
     20   CHK_FAIL_START
     21   mbsnrtowcs(buffer, &srcp, 10, argc, &st);
     22   CHK_FAIL_END
     23 
     24   printf("%ls\n", buffer);
     25   return ret;
     26 }