blob: 2333c3e23e4443bb412a858cca0a594ead178108 [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)
25 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, unsigned int, const char*, ...) will always overflow destination buffer
26 // clang should emit a warning, but doesn't
27 sprintf(buf, "foobar"); // NOLINT(runtime/printf)
28
29 // NOLINTNEXTLINE(whitespace/line_length)
30 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, unsigned int, const char*, ...) will always overflow destination buffer
31 // 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)
39 // GCC: warning: call to int __builtin___snprintf_chk(char*, unsigned int, int, unsigned int, const char*, ...) will always overflow destination buffer
40 // clang should emit a warning, but doesn't
41 snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
42
43 // NOLINTNEXTLINE(whitespace/line_length)
44 // GCC: warning: call to int __builtin___snprintf_chk(char*, unsigned int, int, unsigned int, const char*, ...) will always overflow destination buffer
45 // clang should emit a warning, but doesn't
46 snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
47
48 // NOLINTNEXTLINE(whitespace/line_length)
49 // GCC: warning: call to int __builtin___snprintf_chk(char*, unsigned int, int, unsigned int, const char*, ...) will always overflow destination buffer
50 // clang should emit a warning, but doesn't
51 snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
52
53 // NOLINTNEXTLINE(whitespace/line_length)
54 // GCC: warning: call to int __builtin___snprintf_chk(char*, unsigned int, int, unsigned int, const char*, ...) will always overflow destination buffer
55 // clang should emit a warning, but doesn't
56 snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
57}