| Alexey Samsonov | d06aa3d | 2015-03-02 19:34:27 +0000 | [diff] [blame] | 1 | // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t |
| Evgeniy Stepanov | 74cf034 | 2016-12-06 22:02:21 +0000 | [diff] [blame] | 2 | // RUN: %clangxx_msan -O0 -g -DPOSITIVE %s -o %t && not %run %t 2>&1 | FileCheck %s |
| Evgeniy Stepanov | 9c1f832 | 2013-11-28 14:14:48 +0000 | [diff] [blame] | 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 | |
| 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]; |
| Viktor Kutuzov | 78206c5 | 2015-04-25 11:07:05 +0000 | [diff] [blame] | 18 | #if defined(__FreeBSD__) |
| 19 | // FreeBSD's iconv() expects the 2nd argument be of type 'const char**'. |
| 20 | const char *inbuf = inbuf_; |
| 21 | #else |
| Evgeniy Stepanov | 9c1f832 | 2013-11-28 14:14:48 +0000 | [diff] [blame] | 22 | char *inbuf = inbuf_; |
| Viktor Kutuzov | 78206c5 | 2015-04-25 11:07:05 +0000 | [diff] [blame] | 23 | #endif |
| Evgeniy Stepanov | 9c1f832 | 2013-11-28 14:14:48 +0000 | [diff] [blame] | 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 | } |