subsys/fs: Fix fs_* operations crashing when file closed

Attempt to perform fs_read, fs_write, fs_seek, fs_tell, fs_truncate
and fs_sync on file that has been closed, prior to attempt, would cause
NULL pointer dereference.
With this fix, such operations would instead return -EBADF.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2020-07-03 09:28:36 +00:00 committed by Anas Nashif
commit a5b3e86339

View file

@ -130,6 +130,10 @@ ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->read != NULL) {
rc = zfp->mp->fs->read(zfp, ptr, size);
if (rc < 0) {
@ -144,6 +148,10 @@ ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->write != NULL) {
rc = zfp->mp->fs->write(zfp, ptr, size);
if (rc < 0) {
@ -158,6 +166,10 @@ int fs_seek(struct fs_file_t *zfp, off_t offset, int whence)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->lseek != NULL) {
rc = zfp->mp->fs->lseek(zfp, offset, whence);
if (rc < 0) {
@ -172,6 +184,10 @@ off_t fs_tell(struct fs_file_t *zfp)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->tell != NULL) {
rc = zfp->mp->fs->tell(zfp);
if (rc < 0) {
@ -186,6 +202,10 @@ int fs_truncate(struct fs_file_t *zfp, off_t length)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->truncate != NULL) {
rc = zfp->mp->fs->truncate(zfp, length);
if (rc < 0) {
@ -200,6 +220,10 @@ int fs_sync(struct fs_file_t *zfp)
{
int rc = -EINVAL;
if (zfp->mp == NULL) {
return -EBADF;
}
if (zfp->mp->fs->sync != NULL) {
rc = zfp->mp->fs->sync(zfp);
if (rc < 0) {