From patchwork Wed Jul 11 22:10:00 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1185791 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id CC8B33FC8F for ; Wed, 11 Jul 2012 22:10:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030327Ab2GKWKE (ORCPT ); Wed, 11 Jul 2012 18:10:04 -0400 Received: from mail-gg0-f174.google.com ([209.85.161.174]:56851 "EHLO mail-gg0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030301Ab2GKWKC (ORCPT ); Wed, 11 Jul 2012 18:10:02 -0400 Received: by gglu4 with SMTP id u4so1797248ggl.19 for ; Wed, 11 Jul 2012 15:10:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=r+9PdZEhixOnC2gswLrPGS2aMUXsir821Zdya0mIMHg=; b=Q+Nn9zdrGALJN65Hz+A9bR2AB49mH0ndMzfld9ri99DOw1iOmyiF5w31QibivltHtw 2rtHpJSki9SUxozK6f59WSxSKdHWreh0kmLbdL2uZynGjvvyqOiLznMe15uPEhYKrZnD pvuoi8S/SkZ9kUmpnByD7e+EFGVhBiiDCi6TQ8hElC+pdbS/92boi98obeC/dgCpoUlI RHhChRo1iBswpLRgUeEkAv+DVs8ef8IHOES63bYmyiPrH9r4RV7WH51xHtl6BtT8rpdm jJSmI+vj1H/7dtmwBFKG+fPNNgOt5Gf1Pf+Q6ro8GZeZqCi79SH7s1gZjhL35PgAMzyF U6rQ== Received: by 10.236.117.210 with SMTP id j58mr161972yhh.107.1342044602185; Wed, 11 Jul 2012 15:10:02 -0700 (PDT) Received: from [172.22.22.4] (c-71-195-31-37.hsd1.mn.comcast.net. [71.195.31.37]) by mx.google.com with ESMTPS id c70sm5523960yhk.12.2012.07.11.15.10.01 (version=SSLv3 cipher=OTHER); Wed, 11 Jul 2012 15:10:01 -0700 (PDT) Message-ID: <4FFDF9B8.5040401@inktank.com> Date: Wed, 11 Jul 2012 17:10:00 -0500 From: Alex Elder User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: ceph-devel@vger.kernel.org Subject: [PATCH v2 04/16] libceph: define ceph_extract_encoded_string() References: <4FFD847C.7070205@inktank.com> <4FFD8727.7050106@inktank.com> In-Reply-To: <4FFD8727.7050106@inktank.com> X-Gm-Message-State: ALoCoQkJvy6RUWioODqjdFbWG9asdNH9oXs2QB68sMhV3GfSKRJ/ggLe/UtOwX3X9lflOkr3VIPV Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org This adds a new utility routine which will return a dynamically- allocated buffer containing a string that has been decoded from ceph over-the-wire format. It also returns the length of the string if the address of a size variable is supplied to receive it. Signed-off-by: Alex Elder --- v2: Made the function safe from overrunning the source memory, and and added a gfp argument to pass to kmalloc() include/linux/ceph/decode.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: b/include/linux/ceph/decode.h =================================================================== --- a/include/linux/ceph/decode.h +++ b/include/linux/ceph/decode.h @@ -130,6 +130,44 @@ bad: } /* + * Allocate a buffer big enough to hold the wire-encoded string, and + * decode the string into it. The resulting string will always be + * terminated with '\0'. If successful, *p will be advanced + * past the decoded data. Also, if lenp is not a null pointer, the + * length (not including the terminating '\0') will be recorded in + * it. Note that a zero-length string is a valid return value. + * + * Returns a pointer to the newly-allocated string buffer, or a null + * pointer if an error occurs. Neither "p" nor "end" will be updated + * if NULL is returned. + * + * There are two possible failures: + * - memory could not be allocated for the result + * - converting the string would require accessing memory at or + * beyond the "end" pointer provided + */ +static inline char *ceph_extract_encoded_string(void **p, void *end, + size_t *lenp, gfp_t gfp) +{ + ssize_t len; + char *buf; + + len = ceph_decode_string(p, end, NULL, 0); + if (len < 0) + return NULL; + + buf = kmalloc(len + 1, gfp); + if (!buf) + return NULL; + + (void) ceph_decode_string(p, end, buf, len + 1); + if (lenp) + *lenp = (size_t) len; + + return buf; +} + +/* * struct ceph_timespec <-> struct timespec */ static inline void ceph_decode_timespec(struct timespec *ts,