diff --git a/subsys/storage/stream/stream_flash.c b/subsys/storage/stream/stream_flash.c index d7b0cd8e19f..1095660def4 100644 --- a/subsys/storage/stream/stream_flash.c +++ b/subsys/storage/stream/stream_flash.c @@ -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; diff --git a/tests/subsys/storage/stream/stream_flash/src/main.c b/tests/subsys/storage/stream/stream_flash/src/main.c index 82e36c228c7..13920f4e185 100644 --- a/tests/subsys/storage/stream/stream_flash/src/main.c +++ b/tests/subsys/storage/stream/stream_flash/src/main.c @@ -280,6 +280,11 @@ static int fake_write(const struct device *dev, off_t off, const void *data, siz return 0; } +static int bad_write(const struct device *dev, off_t off, const void *data, size_t len) +{ + return -EINVAL; +} + static void test_stream_flash_buffered_write_callback(void) { int rc; @@ -338,6 +343,25 @@ static void test_stream_flash_buffered_write_callback(void) zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc); zassert_equal(ctx.buf_bytes, BUF_LEN, "Expected bytes to be left in buffer"); + /* Pretend flashed context and attempt write write block - 1 bytes to trigger unaligned + * write; the write needs to fail so that we could check that context does not get modified. + */ + fake_api.write = bad_write; + bad_ctx.callback = NULL; + bad_ctx.buf_bytes = 0; + cmp_ctx = bad_ctx; + size_t wblock = flash_get_write_block_size(ctx.fdev); + size_t tow = (wblock == 1) ? 1 : wblock - 1; + + rc = stream_flash_buffered_write(&bad_ctx, write_buf, tow, true); + zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc); + zassert_equal(cmp_ctx.bytes_written, bad_ctx.bytes_written, + "Expected bytes_written not modified"); + /* The write failed but bytes have already been added to buffer and buffer offset + * increased. + */ + zassert_equal(bad_ctx.buf_bytes, cmp_ctx.buf_bytes + tow, + "Expected %d bytes added to buffer", tow); } static void test_stream_flash_flush(void)