1 /*
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 */
17 #undef _FORTIFY_SOURCE
18 #define _FORTIFY_SOURCE 2
19 #include <stdio.h>
21 void test_sprintf() {
22 char buf[4];
24 // NOLINTNEXTLINE(whitespace/line_length)
25 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}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)
29 // NOLINTNEXTLINE(whitespace/line_length)
30 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}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 }
35 void test_snprintf() {
36 char buf[4];
38 // NOLINTNEXTLINE(whitespace/line_length)
39 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}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)
43 // NOLINTNEXTLINE(whitespace/line_length)
44 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}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)
48 // NOLINTNEXTLINE(whitespace/line_length)
49 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}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)
53 // NOLINTNEXTLINE(whitespace/line_length)
54 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}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 }