Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | #undef _FORTIFY_SOURCE |
| 18 | #define _FORTIFY_SOURCE 2 |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | void test_sprintf() { |
| 22 | char buf[4]; |
| 23 | |
| 24 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 25 | // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 26 | // clang should emit a warning, but doesn't |
| 27 | sprintf(buf, "foobar"); // NOLINT(runtime/printf) |
| 28 | |
| 29 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 30 | // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 31 | // clang should emit a warning, but doesn't |
| 32 | sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf) |
| 33 | } |
| 34 | |
| 35 | void test_snprintf() { |
| 36 | char buf[4]; |
| 37 | |
| 38 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 39 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 40 | // clang should emit a warning, but doesn't |
| 41 | snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf) |
| 42 | |
| 43 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 44 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 45 | // clang should emit a warning, but doesn't |
| 46 | snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf) |
| 47 | |
| 48 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 49 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 50 | // clang should emit a warning, but doesn't |
| 51 | snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf) |
| 52 | |
| 53 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 54 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 55 | // clang should emit a warning, but doesn't |
| 56 | snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf) |
| 57 | } |