blob: 3a2d3c4c1751f37529c38269c1d577929154fc91 [file] [log] [blame]
Dan Albert2fbb1b62014-10-08 11:21:32 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
19#include <stdio.h>
20
21void test_sprintf() {
22 char buf[4];
23
24 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070025 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070026 // clang should emit a warning, but doesn't
27 sprintf(buf, "foobar"); // NOLINT(runtime/printf)
28
29 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070030 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070031 // clang should emit a warning, but doesn't
32 sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf)
33}
34
35void test_snprintf() {
36 char buf[4];
37
38 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070039 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070040 // clang should emit a warning, but doesn't
41 snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
42
43 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070044 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070045 // clang should emit a warning, but doesn't
46 snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
47
48 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070049 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070050 // clang should emit a warning, but doesn't
51 snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
52
53 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070054 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070055 // clang should emit a warning, but doesn't
56 snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
57}