Skip to content

Commit 7e206a9

Browse files
authored
Handle negative values passed to close()
Calling close() with negative numbers causes out-of-bounds indexing of the filehandles array. For example, this can happen if open() returns an error and the value is later passed to close().
1 parent f347b89 commit 7e206a9

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

platform/source/mbed_retarget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,11 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh)
656656
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
657657
extern "C" int close(int fildes)
658658
{
659+
if (fildes < 0)
660+
{
661+
errno = EBADF;
662+
return -1;
663+
}
659664
FileHandle *fhc = mbed_file_handle(fildes);
660665
filehandles[fildes] = NULL;
661666
if (fhc == NULL) {

0 commit comments

Comments
 (0)