Skip to content

Standard library/filesystem functions with NULL arguments #2469

@aamcampbell

Description

@aamcampbell

Several standard library functions and POSIX directory functions cause hard faults when given NULL arguments.

I know it's a bad idea to ever allow a NULL argument to be passed to these functions, but it seems to me it should fail gracefully instead of causing a hard fault.

The following calls result in a hard fault:

freopen("path/to/file", "mode", NULL)
fread(NULL, nsize, nitems, pFile)
fwrite(NULL, nsize, nitems, pFile)
closedir(NULL)
readdir(NULL)
rewinddir(NULL)
fseek(NULL, offset, whence)

If I remember right, I think calling fclose() twice on the same handle or calling closedir() twice on the same directory handle may cause a hard fault as well, but that may be more difficult to protect against.

Tested on FRDM-K64F with mbed OS 3 (and possibly mbed 2 as well, I can't remember).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions