Skip to content

[FATFileSystem] Bug in FATFileSystem::file_truncate() causes deadlock #14613

Open
@horeich

Description

@horeich

Description of defect

Program runs into deadlock once FATFileSystem::file_truncate(...) is called: any subsequent member function call from a different thread context is blocked as mutex is not returned.

int FATFileSystem::file_truncate(fs_file_t file, off_t length)
{
    FIL *fh = static_cast<FIL *>(file);

    lock();
    // save current position
    FSIZE_t oldoff = f_tell(fh);

    // seek to new file size and truncate
    FRESULT res = f_lseek(fh, length);
    if (res) {
        unlock();
        return fat_error_remap(res);
    }

    res = f_truncate(fh);
    if (res) {
        unlock();
        return fat_error_remap(res);
    }

    // restore old position
    res = f_lseek(fh, oldoff);
    if (res) {
        unlock();
        return fat_error_remap(res);
    }

    ****missing unlock() here****
    return 0;
}

Target(s) affected by this defect ?

All targets

Toolchain(s) (name and version) displaying this defect ?

arm-none-eabi-gcc (1.70201.0)
pio version 5.1.1

What version of Mbed-os are you using (tag or sha) ?

mbed 6.6.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

n/a

How is this defect reproduced ?

The defect can only be reproduced when member functions are accessed from different threads; problem does not occur if member functions are called from the same thread as mutex is already owned by this thread.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions