From nobody Mon Feb 9 08:10:05 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 152086850675977.71995781077885; Mon, 12 Mar 2018 08:28:26 -0700 (PDT) Received: from localhost ([::1]:59544 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evPNB-0006k8-Up for importer@patchew.org; Mon, 12 Mar 2018 11:28:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52197) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evPGY-0000sP-9D for qemu-devel@nongnu.org; Mon, 12 Mar 2018 11:21:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evPGU-0006cK-VD for qemu-devel@nongnu.org; Mon, 12 Mar 2018 11:21:34 -0400 Received: from relay.sw.ru ([185.231.240.75]:55744) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1evPGU-0006ZV-KT; Mon, 12 Mar 2018 11:21:30 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.89) (envelope-from ) id 1evPGR-0005yw-5i; Mon, 12 Mar 2018 18:21:27 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Mon, 12 Mar 2018 18:21:20 +0300 Message-Id: <20180312152126.286890-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180312152126.286890-1-vsementsov@virtuozzo.com> References: <20180312152126.286890-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 2/8] nbd/server: add nbd_read_opt_name helper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add helper to read name in format: uint32 len (<=3D NBD_MAX_NAME_SIZE) len bytes string (not 0-terminated) The helper would be reused in following patch. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- v2: splitted and changed a lot helper from larger patch of v1 nbd/server.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index d163964cf9..085e14afbf 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -273,6 +273,47 @@ static int nbd_opt_read(NBDClient *client, void *buffe= r, size_t size, return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EI= O : 1; } =20 +/* nbd_opt_read_name + * + * Read string in format: + * uint32_t len (<=3D NBD_MAX_NAME_SIZE) + * len bytes string (not 0-terminated) + * + * @name should be enough to store NBD_MAX_NAME_SIZE+1. + * if @length is non-zero, it would be set to read string length. + * + * Return -errno on I/O error, 0 if option was completely handled by + * sending a reply about inconsistent lengths, or 1 on success. */ +static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *leng= th, + Error **errp) +{ + int ret; + uint32_t len; + + ret =3D nbd_opt_read(client, &len, sizeof(len), errp); + if (ret <=3D 0) { + return ret; + } + cpu_to_be32s(&len); + + if (len > NBD_MAX_NAME_SIZE) { + return nbd_opt_invalid(client, errp, + "Invalid name length: %" PRIu32, len); + } + + ret =3D nbd_opt_read(client, name, len, errp); + if (ret <=3D 0) { + return ret; + } + name[len] =3D '\0'; + + if (length) { + *length =3D len; + } + + return 1; +} + /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, @@ -455,19 +496,7 @@ static int nbd_negotiate_handle_info(NBDClient *client= , uint16_t myflags, 2 bytes: N, number of requests (can be 0) N * 2 bytes: N requests */ - rc =3D nbd_opt_read(client, &namelen, sizeof(namelen), errp); - if (rc <=3D 0) { - return rc; - } - be32_to_cpus(&namelen); - if (namelen >=3D sizeof(name)) { - return nbd_opt_invalid(client, errp, "name too long for qemu"); - } - rc =3D nbd_opt_read(client, name, namelen, errp); - if (rc <=3D 0) { - return rc; - } - name[namelen] =3D '\0'; + rc =3D nbd_opt_read_name(client, name, &namelen, errp); trace_nbd_negotiate_handle_export_name_request(name); =20 rc =3D nbd_opt_read(client, &requests, sizeof(requests), errp); --=20 2.11.1