storage/stream/flash: Failed write of reminder rewinds buffer offset

The stream_flash_buffered_write, when invoked to do flush write, will
attempt to write the tail bytes from the buffer, filling the required
minimal write block size with erase value bytes; after write it rewinds
the buffer offset, bytes_written, by number of the "filler bytes".
Doe to lack of return code processing from flash_sync call, two things
would happen to context in case of failure:
 1) the ctx->bytes_written would be rewind pass the value it had before
    function call as it gets decremented by "filler bytes" even if write
    failed;
 2) the ctx->buf_bytes offset would be accounting for added "filler
    bytes" which should not be counted as data in buffer.

Proper processing of return code has been added to remove effects
described above.

Unit tests have been expended to cover the scenarios.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2021-02-24 14:59:12 +00:00 committed by Ioannis Glaropoulos
commit 32451230e2
2 changed files with 29 additions and 1 deletions

View file

@ -161,7 +161,11 @@ int stream_flash_buffered_write(struct stream_flash_ctx *ctx, const uint8_t *dat
}
rc = flash_sync(ctx);
ctx->bytes_written -= fill_length;
if (rc == 0) {
ctx->bytes_written -= fill_length;
} else {
ctx->buf_bytes -= fill_length;
}
}
return rc;