From patchwork Thu Jul 12 22:47:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1191641 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 E87FA40D0E for ; Thu, 12 Jul 2012 22:47:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162412Ab2GLWrk (ORCPT ); Thu, 12 Jul 2012 18:47:40 -0400 Received: from mail-gg0-f174.google.com ([209.85.161.174]:49685 "EHLO mail-gg0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162395Ab2GLWri (ORCPT ); Thu, 12 Jul 2012 18:47:38 -0400 Received: by mail-gg0-f174.google.com with SMTP id u4so3017125ggl.19 for ; Thu, 12 Jul 2012 15:47:38 -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=DIHUOTmfJ75D1ieuuOHE9KE8E/UCOeLRZksrqvRkCB8=; b=FGNVDncy4HmmSpBzJSgz+XqZaaBuRzpdG3g6EYM82hpKVozbS7iPGGFPCJtsIsvcHO mmtsgPTGqkszisJJc99p3T34rsSOWEVy12+fPrEo3MGaE7thezlxvrvUBhmDTXl/KcvU f1LXDGGJ7X4PE2mKRK5b32EtgDULMj6m7XAvBAD6AWILvaE6JpEzHgE52PASibl55Vgt CumySqi48j5pHXRs0uCycraNCiATc+T7pASHSbLUGbCodq3p5cPKPjwwZ/baiXoyOBem TqsgFvCja7uIsC1Rn6KujSnlC0sUCz/WPlHjOR5MBtfQncQ+jOD4LAscS0If2suH1n5s YSlg== Received: by 10.236.76.233 with SMTP id b69mr64730yhe.52.1342133258462; Thu, 12 Jul 2012 15:47:38 -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 g22sm11242248yhh.20.2012.07.12.15.47.37 (version=SSLv3 cipher=OTHER); Thu, 12 Jul 2012 15:47:37 -0700 (PDT) Message-ID: <4FFF5408.5010607@inktank.com> Date: Thu, 12 Jul 2012 17:47:36 -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 v4 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: ALoCoQmq8JwVxB1lLS9rQNjGUCVZivhpZcJ40i+NZeQQT6tLz1u8HFXT50AAgDZMaMgxgVFicbjd 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 --- v4: Last final version. Caller can now distinguish between the two error conditions. v3: Final version. Sage convinced me that there was no need for ceph_decode_string() other than its use in this function, so now this implements what that function had been doing directly. 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 | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 47 insertions(+), 0 deletions(-) static inline void ceph_decode_timespec(struct timespec *ts, diff --git a/include/linux/ceph/decode.h b/include/linux/ceph/decode.h index bcbd66c..4bbf2db 100644 --- a/include/linux/ceph/decode.h +++ b/include/linux/ceph/decode.h @@ -1,6 +1,7 @@ #ifndef __CEPH_DECODE_H #define __CEPH_DECODE_H +#include #include #include #include @@ -85,6 +86,52 @@ static inline int ceph_has_room(void **p, void *end, size_t n) } while (0) /* + * 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 + * *lenp. Note that a zero-length string is a valid return value. + * + * Returns a pointer to the newly-allocated string buffer, or a + * pointer-coded errno if an error occurs. Neither *p nor *lenp + * will have been updated if an error is returned. + * + * There are two possible failures: + * - converting the string would require accessing memory at or + * beyond the "end" pointer provided (-E + * - memory could not be allocated for the result + */ +static inline char *ceph_extract_encoded_string(void **p, void *end, + size_t *lenp, gfp_t gfp) +{ + u32 len; + void *sp = *p; + char *buf; + + ceph_decode_32_safe(&sp, end, len, bad); + if (!ceph_has_room(&sp, end, len)) + goto bad; + + buf = kmalloc(len + 1, gfp); + if (!buf) + return ERR_PTR(-ENOMEM); + + if (len) + memcpy(buf, sp, len); + buf[len] = '\0'; + + *p = (char *) *p + sizeof (u32) + len; + if (lenp) + *lenp = (size_t) len; + + return buf; + +bad: + return ERR_PTR(-ERANGE); +} + +/* * struct ceph_timespec <-> struct timespec */