From b3a680a47abfdd5fa4086bab53891f3a4e798d74 Mon Sep 17 00:00:00 2001
From: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Date: Thu, 10 Nov 2022 08:40:30 +0100
Subject: [PATCH] sandbox: check lseek return value in handle_ufi_command

Invoking lseek() may result in an error. Handle it.

Addresses-Coverity-ID: 376212 ("Error handling issues  (CHECKED_RETURN)")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 drivers/usb/emul/sandbox_flash.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/emul/sandbox_flash.c b/drivers/usb/emul/sandbox_flash.c
index 6e8cfe1650..01ccc4bc17 100644
--- a/drivers/usb/emul/sandbox_flash.c
+++ b/drivers/usb/emul/sandbox_flash.c
@@ -188,15 +188,19 @@ static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
 	struct scsi_emul_info *info = &priv->eminfo;
 	const struct scsi_cmd *req = buff;
 	int ret;
+	off_t offset;
 
 	ret = sb_scsi_emul_command(info, req, len);
 	if (!ret) {
 		setup_response(priv);
 	} else if ((ret == SCSI_EMUL_DO_READ || ret == SCSI_EMUL_DO_WRITE) &&
 		   priv->fd != -1) {
-		os_lseek(priv->fd, info->seek_block * info->block_size,
-			 OS_SEEK_SET);
-		setup_response(priv);
+		offset = os_lseek(priv->fd, info->seek_block * info->block_size,
+				  OS_SEEK_SET);
+		if (offset == (off_t)-1)
+			setup_fail_response(priv);
+		else
+			setup_response(priv);
 	} else {
 		setup_fail_response(priv);
 	}
-- 
2.39.5