1// RUN: %clangxx_msan -O0 %s -o %t && %run %t
2// RUN: not %run %t NTOP_READ 2>&1 | FileCheck %s --check-prefix=NTOP_READ
3// RUN: not %run %t PTON_READ 2>&1 | FileCheck %s --check-prefix=PTON_READ
4
5#include <assert.h>
6#include <resolv.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/types.h>
10
11#include <sanitizer/msan_interface.h>
12
13int main(int iArgc, char *szArgv[]) {
14 char* test = nullptr;
15 if (iArgc > 1) {
16 test = szArgv[1];
17 }
18
19 if (test == nullptr) {
20 // Check NTOP writing
21 const char *src = "base64 test data";
22 size_t src_len = strlen(s: src);
23 size_t dst_len = ((src_len + 2) / 3) * 4 + 1;
24 char dst[dst_len];
25 int res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
26 dst, dst_len);
27 assert(res >= 0);
28 __msan_check_mem_is_initialized(x: dst, size: res);
29
30 // Check PTON writing
31 unsigned char target[dst_len];
32 res = b64_pton(dst, target, dst_len);
33 assert(res >= 0);
34 __msan_check_mem_is_initialized(x: target, size: res);
35
36 // Check NTOP writing for zero length src
37 src = "";
38 src_len = strlen(s: src);
39 assert(((src_len + 2) / 3) * 4 + 1 < dst_len);
40 res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
41 dst, dst_len);
42 assert(res >= 0);
43 __msan_check_mem_is_initialized(x: dst, size: res);
44
45 // Check PTON writing for zero length src
46 dst[0] = '\0';
47 res = b64_pton(dst, target, dst_len);
48 assert(res >= 0);
49 __msan_check_mem_is_initialized(x: target, size: res);
50
51 return 0;
52 }
53
54 if (strcmp(s1: test, s2: "NTOP_READ") == 0) {
55 // Check NTOP reading
56 size_t src_len = 80;
57 char src[src_len];
58 __msan_poison(a: src, size: src_len);
59 size_t dst_len = ((src_len + 2) / 3) * 4 + 1;
60 char dst[dst_len];
61 int res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
62 dst, dst_len);
63 // NTOP_READ: Uninitialized bytes in __b64_ntop
64 return 0;
65 }
66
67 if (strcmp(s1: test, s2: "PTON_READ") == 0) {
68 // Check PTON reading
69 size_t src_len = 80;
70 char src[src_len];
71 strcpy(dest: src, src: "junk longer than zero");
72 // pretend it is uninitialized
73 __msan_poison(a: src, size: src_len);
74 unsigned char target[src_len];
75 int res = b64_pton(src, target, src_len);
76 // PTON_READ: Uninitialized bytes in __b64_pton
77 return 0;
78 }
79
80 return 0;
81}
82

source code of compiler-rt/test/msan/Linux/b64.cpp