• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t
2 // RUN: %clangxx_msan -O0 -g -DPOSITIVE %s -o %t && not %run %t |& FileCheck %s
3 
4 #include <assert.h>
5 #include <iconv.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <errno.h>
10 
main(void)11 int main(void) {
12   iconv_t cd = iconv_open("ASCII", "ASCII");
13   assert(cd != (iconv_t)-1);
14 
15   char inbuf_[100];
16   strcpy(inbuf_, "sample text");
17   char outbuf_[100];
18 #if defined(__FreeBSD__)
19   // FreeBSD's iconv() expects the 2nd argument be of type 'const char**'.
20   const char *inbuf = inbuf_;
21 #else
22   char *inbuf = inbuf_;
23 #endif
24   char *outbuf = outbuf_;
25   size_t inbytesleft = strlen(inbuf_);
26   size_t outbytesleft = sizeof(outbuf_);
27 
28 #ifdef POSITIVE
29   {
30     char u;
31     char *volatile p = &u;
32     inbuf_[5] = *p;
33   }
34 #endif
35 
36   size_t res;
37   res = iconv(cd, 0, 0, 0, 0);
38   assert(res != (size_t)-1);
39 
40   res = iconv(cd, 0, 0, &outbuf, &outbytesleft);
41   assert(res != (size_t)-1);
42 
43   res = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
44   // CHECK: MemorySanitizer: use-of-uninitialized-value
45   // CHECK: #0 {{.*}} in main {{.*}}iconv.cc:[[@LINE-2]]
46   assert(res != (size_t)-1);
47   assert(inbytesleft == 0);
48 
49   assert(memcmp(inbuf_, outbuf_, strlen(inbuf_)) == 0);
50 
51   iconv_close(cd);
52   return 0;
53 }
54