2014-10-08 20:21:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-08-24 02:32:48 +02:00
|
|
|
/*
|
|
|
|
* If this test fails, you can see the compiler's output by erasing a few args from the failing
|
|
|
|
* command. Specifically, delete everything before the path/to/the/compiler, then delete the first
|
|
|
|
* arg after the path/to/the/compiler. For example, given the following command:
|
|
|
|
*
|
|
|
|
* bionic/tests/file-check-cxx out/host/linux-x86/bin/FileCheck \
|
|
|
|
* prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++ CLANG -I bionic/tests -I ...
|
|
|
|
*
|
2018-07-24 21:36:51 +02:00
|
|
|
* If you delete everything before clang++ and delete "CLANG", then you'll end up with:
|
2017-08-24 02:32:48 +02:00
|
|
|
*
|
|
|
|
* prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++ -I bionic/tests -I ...
|
|
|
|
*
|
|
|
|
* Which is the command that FileCheck executes.
|
|
|
|
*/
|
|
|
|
|
2014-10-08 20:21:32 +02:00
|
|
|
#undef _FORTIFY_SOURCE
|
|
|
|
#define _FORTIFY_SOURCE 2
|
2015-03-04 05:27:58 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdarg.h>
|
2014-10-08 20:21:32 +02:00
|
|
|
#include <stdio.h>
|
2015-07-21 03:37:29 +02:00
|
|
|
#include <stdlib.h>
|
2015-03-04 05:27:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-10-08 20:21:32 +02:00
|
|
|
|
|
|
|
void test_sprintf() {
|
|
|
|
char buf[4];
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
2017-02-09 09:00:31 +01:00
|
|
|
// CLANG: error: call to unavailable function 'sprintf': format string will always overflow destination buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
sprintf(buf, "foobar"); // NOLINT(runtime/printf)
|
2014-10-08 20:21:32 +02:00
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf)
|
2014-10-08 20:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_snprintf() {
|
|
|
|
char buf[4];
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
2017-02-09 09:00:31 +01:00
|
|
|
// CLANG: error: call to unavailable function 'snprintf': format string will always overflow destination buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
|
2014-10-08 20:21:32 +02:00
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
|
2014-10-08 20:21:32 +02:00
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
|
2014-10-08 20:21:32 +02:00
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_memcpy() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: error: 'memcpy' called with size bigger than buffer
|
2017-02-09 09:00:31 +01:00
|
|
|
memcpy(buf, "foobar", sizeof("foobar") + 100);
|
2015-03-04 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_memmove() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: error: 'memmove' called with size bigger than buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
memmove(buf, "foobar", sizeof("foobar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_memset() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: error: 'memset' called with size bigger than buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
memset(buf, 0, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_strcpy() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: error: 'strcpy' called with string bigger than buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
strcpy(buf, "foobar"); // NOLINT(runtime/printf)
|
2017-08-01 06:29:42 +02:00
|
|
|
|
|
|
|
// CLANG: error: 'strcpy' called with string bigger than buffer
|
|
|
|
strcpy(buf, "quux");
|
2015-03-04 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_stpcpy() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: error: 'stpcpy' called with string bigger than buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
stpcpy(buf, "foobar");
|
2017-08-01 06:29:42 +02:00
|
|
|
|
|
|
|
// CLANG: error: 'stpcpy' called with string bigger than buffer
|
|
|
|
stpcpy(buf, "quux");
|
2015-03-04 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_strncpy() {
|
|
|
|
char buf[4];
|
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
strncpy(buf, "foobar", sizeof("foobar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_strcat() {
|
|
|
|
char buf[4] = "";
|
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
strcat(buf, "foobar"); // NOLINT(runtime/printf)
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_strncat() {
|
|
|
|
char buf[4] = "";
|
|
|
|
|
2018-07-24 21:36:51 +02:00
|
|
|
// TODO: clang should emit a warning, but doesn't
|
2015-03-04 05:27:58 +01:00
|
|
|
strncat(buf, "foobar", sizeof("foobar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_vsprintf(const char* fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
char buf[4];
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
|
|
|
// clang should emit a warning, but doesn't
|
|
|
|
vsprintf(buf, "foobar", va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_vsnprintf(const char* fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
char buf[4];
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
|
|
|
// clang should emit a warning, but doesn't
|
|
|
|
vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf)
|
|
|
|
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fgets() {
|
|
|
|
char buf[4];
|
|
|
|
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fgets', size should not be negative
|
2015-03-04 05:27:58 +01:00
|
|
|
fgets(buf, -1, stdin);
|
|
|
|
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fgets', size is larger than the destination buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
fgets(buf, 6, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_recvfrom() {
|
|
|
|
char buf[4];
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
2017-08-01 06:21:10 +02:00
|
|
|
// CLANG: error: 'recvfrom' called with size bigger than buffer
|
2018-08-03 02:31:13 +02:00
|
|
|
recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), nullptr);
|
2015-03-04 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 06:21:10 +02:00
|
|
|
void test_recv() {
|
|
|
|
char buf[4] = {0};
|
|
|
|
|
|
|
|
// CLANG: error: 'recv' called with size bigger than buffer
|
|
|
|
recv(0, buf, 6, 0);
|
|
|
|
}
|
|
|
|
|
2015-03-04 05:27:58 +01:00
|
|
|
void test_umask() {
|
2017-08-01 06:16:05 +02:00
|
|
|
// CLANG: error: 'umask' called with invalid mode
|
2015-03-04 05:27:58 +01:00
|
|
|
umask(01777);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read() {
|
|
|
|
char buf[4];
|
2017-08-01 06:30:47 +02:00
|
|
|
// CLANG: error: in call to 'read', 'count' bytes overflows the given object
|
2015-03-04 05:27:58 +01:00
|
|
|
read(0, buf, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_open() {
|
2017-08-04 18:34:19 +02:00
|
|
|
// CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode
|
2015-03-04 05:27:58 +01:00
|
|
|
open("/dev/null", O_CREAT);
|
|
|
|
|
2017-08-04 18:34:19 +02:00
|
|
|
// CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode
|
|
|
|
open("/dev/null", O_TMPFILE);
|
|
|
|
|
2017-02-09 09:00:31 +01:00
|
|
|
// CLANG: error: call to unavailable function 'open': too many arguments
|
2015-03-04 05:27:58 +01:00
|
|
|
open("/dev/null", O_CREAT, 0, 0);
|
2017-08-04 02:11:35 +02:00
|
|
|
|
2017-08-04 18:34:19 +02:00
|
|
|
// CLANG: error: call to unavailable function 'open': too many arguments
|
|
|
|
open("/dev/null", O_TMPFILE, 0, 0);
|
|
|
|
|
2017-08-04 02:11:35 +02:00
|
|
|
// CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT?
|
|
|
|
open("/dev/null", O_RDONLY, 0644);
|
|
|
|
|
|
|
|
// CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT?
|
|
|
|
open("/dev/null", O_DIRECTORY, 0644);
|
2015-03-04 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_poll() {
|
|
|
|
pollfd fds[1];
|
2017-08-01 06:16:05 +02:00
|
|
|
// CLANG: error: in call to 'poll', fd_count is larger than the given buffer
|
2015-03-04 05:27:58 +01:00
|
|
|
poll(fds, 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_ppoll() {
|
|
|
|
pollfd fds[1];
|
|
|
|
timespec timeout;
|
2017-08-01 06:16:05 +02:00
|
|
|
// CLANG: error: in call to 'ppoll', fd_count is larger than the given buffer
|
2018-03-22 19:01:25 +01:00
|
|
|
ppoll(fds, 2, &timeout, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_ppoll64() {
|
|
|
|
pollfd fds[1];
|
|
|
|
timespec timeout;
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
|
|
|
// CLANG: error: in call to 'ppoll64', fd_count is larger than the given buffer
|
|
|
|
ppoll64(fds, 2, &timeout, nullptr);
|
2014-10-08 20:21:32 +02:00
|
|
|
}
|
2015-07-18 19:55:51 +02:00
|
|
|
|
|
|
|
void test_fread_overflow() {
|
|
|
|
char buf[4];
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fread', size * count overflows
|
2015-07-18 19:55:51 +02:00
|
|
|
fread(buf, 2, (size_t)-1, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fread_too_big() {
|
|
|
|
char buf[4];
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fread', size * count is too large for the given buffer
|
2015-07-18 19:55:51 +02:00
|
|
|
fread(buf, 1, 5, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fwrite_overflow() {
|
2015-07-21 03:37:29 +02:00
|
|
|
char buf[4] = {0};
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fwrite', size * count overflows
|
2015-07-18 19:55:51 +02:00
|
|
|
fwrite(buf, 2, (size_t)-1, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fwrite_too_big() {
|
|
|
|
char buf[4] = {0};
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
2017-08-01 06:23:34 +02:00
|
|
|
// CLANG: error: in call to 'fwrite', size * count is too large for the given buffer
|
2015-07-18 19:55:51 +02:00
|
|
|
fwrite(buf, 1, 5, stdout);
|
|
|
|
}
|
2015-05-20 21:31:26 +02:00
|
|
|
|
|
|
|
void test_getcwd() {
|
|
|
|
char buf[4];
|
2017-08-01 06:30:47 +02:00
|
|
|
// CLANG: error: in call to 'getcwd', 'size' bytes overflows the given object
|
2015-05-20 21:31:26 +02:00
|
|
|
getcwd(buf, 5);
|
|
|
|
}
|
2015-07-21 03:37:29 +02:00
|
|
|
|
|
|
|
void test_pwrite64_size() {
|
|
|
|
char buf[4] = {0};
|
2017-08-01 06:30:47 +02:00
|
|
|
// CLANG: error: in call to 'pwrite64', 'count' bytes overflows the given object
|
2015-07-21 03:37:29 +02:00
|
|
|
pwrite64(STDOUT_FILENO, buf, 5, 0);
|
|
|
|
}
|
|
|
|
|
2017-02-09 09:00:31 +01:00
|
|
|
void test_pwrite64_too_big_malloc() {
|
2015-07-21 03:37:29 +02:00
|
|
|
void *buf = calloc(atoi("5"), 1);
|
2017-02-09 09:00:31 +01:00
|
|
|
// clang should emit a warning, but probably never will.
|
|
|
|
pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_pwrite64_too_big() {
|
|
|
|
char buf[4] = {0};
|
2017-08-01 06:30:47 +02:00
|
|
|
// CLANG: error: in call to 'pwrite64', 'count' must be <= SSIZE_MAX
|
2015-07-21 03:37:29 +02:00
|
|
|
pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_size() {
|
|
|
|
char buf[4] = {0};
|
2017-08-01 06:30:47 +02:00
|
|
|
// CLANG: error: in call to 'write', 'count' bytes overflows the given object
|
2015-07-21 03:37:29 +02:00
|
|
|
write(STDOUT_FILENO, buf, 5);
|
|
|
|
}
|
2017-02-09 09:00:31 +01:00
|
|
|
|
|
|
|
void test_memset_args_flipped() {
|
|
|
|
char from[4] = {0};
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
2017-08-01 06:29:42 +02:00
|
|
|
// CLANG: 'memset' will set 0 bytes; maybe the arguments got flipped?
|
2018-09-21 07:58:01 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wmemset-transposed-args"
|
2017-02-09 09:00:31 +01:00
|
|
|
memset(from, sizeof(from), 0);
|
2018-09-21 07:58:01 +02:00
|
|
|
#pragma clang diagnostic pop
|
2017-02-09 09:00:31 +01:00
|
|
|
}
|
2017-02-14 02:27:59 +01:00
|
|
|
|
|
|
|
void test_sendto() {
|
|
|
|
char buf[4] = {0};
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
2017-08-01 06:21:10 +02:00
|
|
|
// CLANG: error: 'sendto' called with size bigger than buffer
|
2017-02-14 02:27:59 +01:00
|
|
|
sendto(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_in));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_send() {
|
|
|
|
char buf[4] = {0};
|
|
|
|
|
2017-08-01 06:21:10 +02:00
|
|
|
// CLANG: error: 'send' called with size bigger than buffer
|
2017-02-14 02:27:59 +01:00
|
|
|
send(0, buf, 6, 0);
|
|
|
|
}
|
2017-08-01 06:21:10 +02:00
|
|
|
|
|
|
|
void test_realpath() {
|
|
|
|
char buf[4] = {0};
|
|
|
|
// NOLINTNEXTLINE(whitespace/line_length)
|
|
|
|
// CLANG: error: 'realpath' output parameter must be NULL or a pointer to a buffer with >= PATH_MAX bytes
|
|
|
|
realpath(".", buf);
|
|
|
|
|
|
|
|
// This is fine.
|
2018-08-03 02:31:13 +02:00
|
|
|
realpath(".", nullptr);
|
2017-08-01 06:21:10 +02:00
|
|
|
|
2017-08-14 23:48:55 +02:00
|
|
|
char bigbuf[PATH_MAX];
|
|
|
|
// CLANG: error: 'realpath': NULL path is never correct; flipped arguments?
|
2018-08-03 02:31:13 +02:00
|
|
|
realpath(nullptr, bigbuf);
|
2017-08-01 06:21:10 +02:00
|
|
|
}
|