[stgt] [PATCH 02/13] spc_memcpy: safe memcpy for SPC-type cmds tracking remaining space

nezhinsky at gmail.com nezhinsky at gmail.com
Mon Feb 11 23:18:07 CET 2013


From: Alexander Nezhinsky <nezhinsky at gmail.com>

To assist safe generation of data-in buffer for SPC-type read commands
a new utility function is defined:

int spc_memcpy(uint8_t *dst, uint32_t *dst_remain_len,
               uint8_t *src, uint32_t src_len)

Copies up to src_len bytes from src, making sure not to exceed *dst_remain_len
bytes available at dst. Returns actually copied length, updates remaining space
in *dst_remain_len.

Signed-off-by: Alexander Nezhinsky <nezhinsky at gmail.com>
---
 usr/util.c |   18 ++++++++++++++++++
 usr/util.h |    2 ++
 2 files changed, 20 insertions(+)

diff --git a/usr/util.c b/usr/util.c
index 6c9bad7..1500dec 100644
--- a/usr/util.c
+++ b/usr/util.c
@@ -196,3 +196,21 @@ int get_blk_shift(unsigned int size)
 	return shift;
 }
 
+/*
+ * memory copy for spc-style scsi commands.
+ * Copy up to src_len bytes from src,
+ * not exceeding *dst_remain_len bytes available at dst.
+ * Reflect decreased space in *dst_remain_len.
+ * Return actually copied length.
+ */
+int spc_memcpy(uint8_t *dst, uint32_t *dst_remain_len,
+	       uint8_t *src, uint32_t src_len)
+{
+	int copy_len = min_t(uint32_t, *dst_remain_len, src_len);
+
+	if (copy_len) {
+		memcpy(dst, src, copy_len);
+		*dst_remain_len -= copy_len;
+	}
+	return copy_len;
+}
diff --git a/usr/util.h b/usr/util.h
index 76cff2a..93f9c89 100644
--- a/usr/util.h
+++ b/usr/util.h
@@ -67,6 +67,8 @@ extern int backed_file_open(char *path, int oflag, uint64_t *size,
 extern int set_non_blocking(int fd);
 extern int str_to_open_flags(char *buf);
 extern char *open_flags_to_str(char *dest, int flags);
+extern int spc_memcpy(uint8_t *dst, uint32_t *dst_remain_len,
+		      uint8_t *src, uint32_t src_len);
 
 #define zalloc(size)			\
 ({					\
-- 
1.7.9.6

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list