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