| 1 | // RUN: %clang_dfsan %s -o %t && %run %t |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | int main(int argc, char *argv[]) { |
| 7 | { |
| 8 | char buf[256] = "10000000000-100000000000 rw-p 00000000 00:00 0" ; |
| 9 | long = 0; |
| 10 | // This test exposes a bug in DFSan's sscanf, that leads to flakiness |
| 11 | // in release_shadow_space.c (see |
| 12 | // https://github.com/llvm/llvm-project/issues/91287) |
| 13 | int r = sscanf(s: buf, format: "Garbage text before, %ld, Garbage text after" , &rss); |
| 14 | assert(r == 0); |
| 15 | } |
| 16 | |
| 17 | // Testing other variations of sscanf behavior. |
| 18 | { |
| 19 | int a = 0; |
| 20 | int b = 0; |
| 21 | int r = sscanf(s: "abc42 cat 99" , format: "abc%d cat %d" , &a, &b); |
| 22 | assert(a == 42); |
| 23 | assert(b == 99); |
| 24 | assert(r == 2); |
| 25 | } |
| 26 | |
| 27 | { |
| 28 | int a = 0; |
| 29 | int b = 0; |
| 30 | int r = sscanf(s: "abc42 cat 99" , format: "abc%d dog %d" , &a, &b); |
| 31 | assert(a == 42); |
| 32 | assert(r == 1); |
| 33 | } |
| 34 | |
| 35 | { |
| 36 | int a = 0; |
| 37 | int b = 0; |
| 38 | int r = sscanf(s: "abx42 dog 99" , format: "abc%d dog %d" , &a, &b); |
| 39 | assert(r == 0); |
| 40 | } |
| 41 | |
| 42 | { |
| 43 | int r = sscanf(s: "abx" , format: "abc" ); |
| 44 | assert(r == 0); |
| 45 | } |
| 46 | |
| 47 | { |
| 48 | int r = sscanf(s: "abc" , format: "abc" ); |
| 49 | assert(r == 0); |
| 50 | } |
| 51 | |
| 52 | { |
| 53 | int n = 0; |
| 54 | int r = sscanf(s: "abc" , format: "abc%n" , &n); |
| 55 | assert(n == 3); |
| 56 | assert(r == 0); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | int n = 1234; |
| 61 | int r = sscanf(s: "abxy" , format: "abcd%n" , &n); |
| 62 | assert(n == 1234); |
| 63 | assert(r == 0); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | int a = 0; |
| 68 | int n = 1234; |
| 69 | int r = sscanf(s: "abcd99" , format: "abcd%d%n" , &a, &n); |
| 70 | assert(a == 99); |
| 71 | assert(n == 6); |
| 72 | assert(r == 1); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | int n = 1234; |
| 77 | int r = sscanf(s: "abcdsuffix" , format: "abcd%n" , &n); |
| 78 | assert(n == 4); |
| 79 | assert(r == 0); |
| 80 | } |
| 81 | |
| 82 | { |
| 83 | int n = 1234; |
| 84 | int r = sscanf(s: "abxxsuffix" , format: "abcd%n" , &n); |
| 85 | assert(n == 1234); |
| 86 | assert(r == 0); |
| 87 | } |
| 88 | |
| 89 | { |
| 90 | int a = 0; |
| 91 | int b = 0; |
| 92 | int n = 1234; |
| 93 | int r = sscanf(s: "abcd99 xy100" , format: "abcd%d xy%d%n" , &a, &b, &n); |
| 94 | assert(a == 99); |
| 95 | assert(b == 100); |
| 96 | assert(n == 12); |
| 97 | assert(r == 2); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | int a = 0; |
| 102 | int b = 0; |
| 103 | int n = 1234; |
| 104 | int r = sscanf(s: "abcd99 xy100" , format: "abcd%d zz%d%n" , &a, &b, &n); |
| 105 | assert(a == 99); |
| 106 | assert(b == 0); |
| 107 | assert(n == 1234); |
| 108 | assert(r == 1); |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |