1 | // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t |
2 | // RUN: %clangxx_msan -O0 -g -DPOSITIVE %s -o %t && not %run %t 2>&1 | 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 | |
11 | #if defined(__NetBSD__) |
12 | #include <sys/param.h> |
13 | #if __NetBSD_Prereq__(9,99,17) |
14 | #define NETBSD_POSIX_ICONV 1 |
15 | #else |
16 | #define NETBSD_POSIX_ICONV 0 |
17 | #endif |
18 | #endif |
19 | |
20 | int main(void) { |
21 | iconv_t cd = iconv_open(tocode: "ASCII" , fromcode: "ASCII" ); |
22 | assert(cd != (iconv_t)-1); |
23 | |
24 | char inbuf_[100]; |
25 | strcpy(dest: inbuf_, src: "sample text" ); |
26 | char outbuf_[100]; |
27 | #if defined(__NetBSD__) && !NETBSD_POSIX_ICONV |
28 | // Some OSes expect the 2nd argument of iconv(3) to be of type const char ** |
29 | const char *inbuf = inbuf_; |
30 | #else |
31 | char *inbuf = inbuf_; |
32 | #endif |
33 | char *outbuf = outbuf_; |
34 | size_t inbytesleft = strlen(s: inbuf_); |
35 | size_t outbytesleft = sizeof(outbuf_); |
36 | |
37 | #ifdef POSITIVE |
38 | { |
39 | char u; |
40 | char *volatile p = &u; |
41 | inbuf_[5] = *p; |
42 | } |
43 | #endif |
44 | |
45 | size_t res; |
46 | res = iconv(cd: cd, inbuf: 0, inbytesleft: 0, outbuf: 0, outbytesleft: 0); |
47 | assert(res != (size_t)-1); |
48 | |
49 | res = iconv(cd: cd, inbuf: 0, inbytesleft: 0, outbuf: &outbuf, outbytesleft: &outbytesleft); |
50 | assert(res != (size_t)-1); |
51 | |
52 | res = iconv(cd: cd, inbuf: &inbuf, inbytesleft: &inbytesleft, outbuf: &outbuf, outbytesleft: &outbytesleft); |
53 | // CHECK: MemorySanitizer: use-of-uninitialized-value |
54 | // CHECK: #0 {{.*}} in main {{.*}}iconv.cpp:[[@LINE-2]] |
55 | assert(res != (size_t)-1); |
56 | assert(inbytesleft == 0); |
57 | |
58 | assert(memcmp(inbuf_, outbuf_, strlen(inbuf_)) == 0); |
59 | |
60 | iconv_close(cd: cd); |
61 | return 0; |
62 | } |
63 | |