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().
pull/15431/head
alrvid 2023-06-14 23:29:38 +02:00 committed by GitHub
parent f347b89d0d
commit 7e206a90db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 0 deletions

View File

@ -656,6 +656,11 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh)
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
extern "C" int close(int fildes)
{
if (fildes < 0)
{
errno = EBADF;
return -1;
}
FileHandle *fhc = mbed_file_handle(fildes);
filehandles[fildes] = NULL;
if (fhc == NULL) {