From patchwork Wed Jul 11 22:09:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1185781 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 E3CA03FC8F for ; Wed, 11 Jul 2012 22:09:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030324Ab2GKWJ5 (ORCPT ); Wed, 11 Jul 2012 18:09:57 -0400 Received: from mail-yx0-f174.google.com ([209.85.213.174]:64713 "EHLO mail-yx0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030301Ab2GKWJ5 (ORCPT ); Wed, 11 Jul 2012 18:09:57 -0400 Received: by yenl2 with SMTP id l2so1790353yen.19 for ; Wed, 11 Jul 2012 15:09:56 -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=y+pNU2XiYDeAbAshwKrOBg59+Afeg2WCcmoe6CMhAiw=; b=IppxPnyA9sji52AmvfCJb5FcDa3JFXvP0LY6g4sJb6I4ti29/5g9Pd/h/yRkDDO1GT vogpyTpIAmwMyG87olHcTW1qPYKUstssu+lO1SQLdJXIoLcYG/GcqQSLomzTqxu12B66 sTlQtGXvE3AQEncBfO1wyusyK7qK2eW+j19ePtyNwvyHb/k9bWSrXtaP7s7UJX8KMTd5 iSFKvTcBH5LsdVG609w3JfQ63ALAW4CXNXO9N30KuD980V9UhWo64elXeoPRI79JFXF6 347bHMYySJmcIgFqZcreN5JAVmnNQObX6TiuZ3NbtBlPv+3+1pyA5lQCg16+oeXDmlyg ouPg== Received: by 10.236.154.69 with SMTP id g45mr58068915yhk.47.1342044596687; Wed, 11 Jul 2012 15:09:56 -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 j17sm2862839anl.5.2012.07.11.15.09.55 (version=SSLv3 cipher=OTHER); Wed, 11 Jul 2012 15:09:56 -0700 (PDT) Message-ID: <4FFDF9B3.3080909@inktank.com> Date: Wed, 11 Jul 2012 17:09:55 -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 03/16] libceph: define ceph_decode_string() References: <4FFD847C.7070205@inktank.com> <4FFD871B.6020704@inktank.com> In-Reply-To: <4FFD871B.6020704@inktank.com> X-Gm-Message-State: ALoCoQna6DG2R1fJf8wIEGGkhnSth335wKw+2fxxyfJvnXUjrUr02Ki983XWT1HrvdvaSSZyp/jO Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org There is no string decoding function defined in , 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 --- v2: Made the function safe from overrunning the source memory include/linux/ceph/decode.h | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 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 @@ -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,