All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Elder <elder@inktank.com>
To: ceph-devel@vger.kernel.org
Subject: [PATCH v2 03/16] libceph: define ceph_decode_string()
Date: Wed, 11 Jul 2012 17:09:55 -0500	[thread overview]
Message-ID: <4FFDF9B3.3080909@inktank.com> (raw)
In-Reply-To: <4FFD871B.6020704@inktank.com>

There is no string decoding function defined in <decode.h>, so this
defines one.

This function is a little different from the others in that the
length of the encoded string is not known a priori.  So the
interface is defined a bit like snprintf(), where the value returned
indicates the space required--even if it's more than the space
allotted.

The function also avoids overrunning the end of the memory range
being converted.

Signed-off-by: Alex Elder <elder@inktank.com>
---
v2: Made the function safe from overrunning the source memory

 include/linux/ceph/decode.h |   45
++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Index: b/include/linux/ceph/decode.h
===================================================================
--- a/include/linux/ceph/decode.h
+++ b/include/linux/ceph/decode.h
@@ -85,6 +85,51 @@ static inline int ceph_has_room(void **p
 	} while (0)

 /*
+ * Decode the wire-encoded string at *p into the buffer "s"
+ * provided, whose size is indicated by "size".  Note that "s" can
+ * be a null pointer if size is 0.  If it fits, the resulting string
+ * will always be terminated with '\0'; otherwise the buffer will
+ * be unchanged.
+ *
+ * Care is taken to ensure the result of decoding the string will
+ * not touch anything at or beyond the "end" address provided.  If
+ * it would, -ERANGE is returned.
+ *
+ * Otherwise, returns the length of the encoded string (which may be
+ * greater than or equal to the buffer size).  The return value does
+ * not include the terminating '\0'.
+ *
+ * If the the return value is not negative and is less than the size
+ * provided, *p will be advanced past the decoded data; otherwise it
+ * is unchanged.  This allows for a two call sequence to be used to
+ * allocate sufficient space for the string.
+ *
+ */
+static inline ssize_t ceph_decode_string(void **p, void *end,
+					char *s, size_t size)
+{
+	size_t len;
+	void *cp = *p;
+
+	ceph_decode_32_safe(&cp, end, len, bad);
+	if (!ceph_has_room(&cp, end, len))
+	    	goto bad;
+
+	if (size < len + 1)
+		return len;	/* Not enough room */
+
+	if (len)
+		memcpy(s, cp, len);
+	*(s + len) = '\0';
+
+	*p = (char *) *p + sizeof (u32) + len;
+
+    	return (ssize_t) len;
+bad:
+    	return (ssize_t) -ERANGE;
+}
+
+/*
  * struct ceph_timespec <-> struct timespec
  */
 static inline void ceph_decode_timespec(struct timespec *ts,


  parent reply	other threads:[~2012-07-11 22:09 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-11 13:49 [PATCH 00/16] rbd: updates and enhancements Alex Elder
2012-07-11 14:00 ` [PATCH 01/16] libceph: fix off-by-one bug in ceph_encode_filepath() Alex Elder
2012-07-11 16:59   ` Yehuda Sadeh
2012-07-11 18:35   ` Josh Durgin
2012-07-11 14:00 ` [PATCH 02/16] rbd: drop a useless local variable Alex Elder
2012-07-11 16:58   ` Yehuda Sadeh Weinraub
2012-07-11 18:36   ` Josh Durgin
2012-07-11 14:00 ` [PATCH 03/16] libceph: define ceph_decode_string() Alex Elder
2012-07-11 17:13   ` Yehuda Sadeh
2012-07-11 18:43   ` Josh Durgin
2012-07-11 22:09   ` Alex Elder [this message]
2012-07-12 17:13     ` [PATCH v2 " Alex Elder
2012-07-11 14:01 ` [PATCH 04/16] libceph: define ceph_extract_encoded_string() Alex Elder
2012-07-11 17:20   ` Yehuda Sadeh
2012-07-11 17:45     ` Sage Weil
2012-07-11 19:14     ` Alex Elder
2012-07-11 19:26       ` Yehuda Sadeh
2012-07-11 22:10   ` [PATCH v2 " Alex Elder
2012-07-12 17:13     ` [PATCH v3 " Alex Elder
2012-07-12 18:20       ` Sage Weil
2012-07-12 19:48         ` Alex Elder
2012-07-12 22:47         ` Alex Elder
2012-07-12 22:47   ` [PATCH v4 " Alex Elder
2012-07-11 14:01 ` [PATCH 05/16] rbd: define dup_token() Alex Elder
2012-07-11 17:48   ` Yehuda Sadeh
2012-07-11 21:50     ` Alex Elder
2012-07-11 18:50   ` Josh Durgin
2012-07-11 14:01 ` [PATCH 06/16] rbd: rename rbd_dev->block_name Alex Elder
2012-07-11 17:55   ` Yehuda Sadeh
2012-07-11 19:02   ` Josh Durgin
2012-07-11 22:13     ` Alex Elder
2012-07-11 14:01 ` [PATCH 07/16] rbd: dynamically allocate object prefix Alex Elder
2012-07-11 19:12   ` Josh Durgin
2012-07-11 19:17     ` Alex Elder
2012-07-12 17:24   ` [PATCH v2 " Alex Elder
2012-07-12 17:42     ` Josh Durgin
2012-07-11 14:02 ` [PATCH 08/16] rbd: don't store pool name in struct rbd_dev Alex Elder
2012-07-11 19:36   ` Josh Durgin
2012-07-11 20:19     ` Sage Weil
2012-07-11 22:25     ` Alex Elder
2012-07-11 23:32       ` Josh Durgin
2012-07-12  2:59     ` Alex Elder
2012-07-12  4:19       ` Josh Durgin
2012-07-12 17:05   ` Alex Elder
2012-07-12 17:05   ` [PATCH] rbd: create pool_id device attribute Alex Elder
2012-07-12 17:16     ` Josh Durgin
2012-07-12 17:35       ` Alex Elder
2012-07-12 17:05   ` [PATCH] rbd: dynamically allocate pool name Alex Elder
2012-07-12 17:21     ` Josh Durgin
2012-07-11 14:02 ` [PATCH 09/16] rbd: dynamically allocate image header name Alex Elder
2012-07-11 20:41   ` Josh Durgin
2012-07-11 14:02 ` [PATCH 10/16] rbd: dynamically allocate image name Alex Elder
2012-07-11 20:49   ` Josh Durgin
2012-07-11 20:52     ` Josh Durgin
2012-07-12 11:12       ` Alex Elder
2012-07-11 14:02 ` [PATCH 11/16] rbd: dynamically allocate snapshot name Alex Elder
2012-07-11 20:53   ` Josh Durgin
2012-07-11 14:02 ` [PATCH 12/16] rbd: use rbd_dev consistently Alex Elder
2012-07-11 20:56   ` Josh Durgin
2012-07-11 14:02 ` [PATCH 13/16] rbd: rename some fields in struct rbd_dev Alex Elder
2012-07-11 21:01   ` Josh Durgin
2012-07-12 11:14     ` Alex Elder
2012-07-11 14:02 ` [PATCH 14/16] rbd: more symbol renames Alex Elder
2012-07-11 21:03   ` Josh Durgin
2012-07-12 11:15     ` Alex Elder
2012-07-11 14:03 ` [PATCH 15/16] rbd: option " Alex Elder
2012-07-11 21:07   ` Josh Durgin
2012-07-11 14:03 ` [PATCH 16/16] rbd: kill num_reply parameters Alex Elder
2012-07-11 21:07   ` Josh Durgin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4FFDF9B3.3080909@inktank.com \
    --to=elder@inktank.com \
    --cc=ceph-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.