From 85125d04ef488d96dd319b552be8632f4c984792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Nohlg=C3=A5rd?= Date: Mon, 12 Feb 2018 08:46:44 +0100 Subject: [PATCH] unittests/vfs: Add test for vfs_bind invalid FD argument --- tests/unittests/tests-vfs/tests-vfs-bind.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unittests/tests-vfs/tests-vfs-bind.c b/tests/unittests/tests-vfs/tests-vfs-bind.c index ddce730fe3..8efa809537 100644 --- a/tests/unittests/tests-vfs/tests-vfs-bind.c +++ b/tests/unittests/tests-vfs/tests-vfs-bind.c @@ -107,11 +107,25 @@ static void test_vfs_bind__leak_fds(void) test_vfs_bind(); } +static void test_vfs_bind__allocate_invalid_fd(void) +{ + /* Check that fds >= VFS_MAX_OPEN_FILES fails with -ENFILE, to avoid out of + * bounds array access */ + /* This test assumes FD numbering begins at 0, update this test if + * VFS_MAX_OPEN_FILES is a valid fd number */ + int fd; + fd = vfs_bind(VFS_MAX_OPEN_FILES, O_RDONLY, &_test_bind_ops, NULL); + TEST_ASSERT_EQUAL_INT(-ENFILE, fd); + fd = vfs_bind((VFS_MAX_OPEN_FILES + 1), O_RDONLY, &_test_bind_ops, NULL); + TEST_ASSERT_EQUAL_INT(-ENFILE, fd); +} + Test *tests_vfs_bind_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_vfs_bind), new_TestFixture(test_vfs_bind__leak_fds), + new_TestFixture(test_vfs_bind__allocate_invalid_fd), }; EMB_UNIT_TESTCALLER(vfs_bind_tests, NULL, NULL, fixtures);