From nobody Sun Feb 8 19:56:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1628075103; cv=none; d=zohomail.com; s=zohoarc; b=LSbicCJl2KViTb1JtDWSqRc3TNnU1G2/NzDN79DBf1NVuIZ547Fi6nnAeqpYZqZQras//it8BlHPX8D2e5x/eeIfMrlUl91qcHOzT9XZ/3o2cpCjmbsthBympZq5P1phGhX5szB53UUqlhKBxjhNxfvdwM6UHge3eqTwrj5Wlhs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628075103; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2o1hjneh60tqpRW/NsNxH3y5R6pBszzWc3b4WvA/dRY=; b=hoBpXxuWDnW/xUMlYGQ3TS3Y/0EMQpjxI/IcSovfhoAfhxll7Cr3aQfsc5XIiLodTx2Rc6crNW8isqlCX7AYoaJScXAjxxa7VNbGRPwKV//2yPdgsS3nq8raUpFrG+xhRzssGT++6w/f3f8nWjvZrY78eOg4s28NZ/Jjjn8222U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1628075103849266.0270494157346; Wed, 4 Aug 2021 04:05:03 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-377-v0E_2GOiO_u31pRGjhX8tw-1; Wed, 04 Aug 2021 07:03:46 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3CF541074677; Wed, 4 Aug 2021 11:03:38 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 16BE11002D71; Wed, 4 Aug 2021 11:03:38 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id C7F94180BAD0; Wed, 4 Aug 2021 11:03:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 174B2pW3014205 for ; Wed, 4 Aug 2021 07:02:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6D9B81383C; Wed, 4 Aug 2021 11:02:51 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id CCFC55D9DC for ; Wed, 4 Aug 2021 11:02:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628075102; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=2o1hjneh60tqpRW/NsNxH3y5R6pBszzWc3b4WvA/dRY=; b=CrgsF5WW9NBl8jC07ObK8mRX2Lvw7AW0HZhPslhzbbcW4TBSPwv0PR3FZVn2r9SfXTVP2U bUjLBO0TwTuVEMgnwOgmd1mxaos8V+jdHqtZskAgTADsFVAKl9+xyDrETz6RbFlDfWi+a+ OmYxrX+JQkwlZPmNIhOlKJu7F2+9MfA= X-MC-Unique: v0E_2GOiO_u31pRGjhX8tw-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 02/23] virInsertElementsN: Split out actual insertion code Date: Wed, 4 Aug 2021 13:02:22 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1628075105564100002 Content-Type: text/plain; charset="utf-8" Split out the code doing the movement of the elements and insertion from the range checks. This will help in adding an error-free version for appension. Signed-off-by: Peter Krempa --- src/util/viralloc.c | 78 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/src/util/viralloc.c b/src/util/viralloc.c index 6c76da8537..c1211a5f23 100644 --- a/src/util/viralloc.c +++ b/src/util/viralloc.c @@ -148,7 +148,6 @@ void virShrinkN(void *ptrptr, size_t size, size_t *coun= tptr, size_t toremove) * @size: the size of one element in bytes * @at: index within array where new elements should be added, -1 fo= r end * @countptr: variable tracking number of elements currently allocated - * @typematchDummy: helper variable to consume results of compile time che= cks * @newelems: pointer to array of one or more new elements to move into * place (the originals will be zeroed out if successful * and if clearOriginal is true) @@ -168,24 +167,17 @@ void virShrinkN(void *ptrptr, size_t size, size_t *co= untptr, size_t toremove) * with zero. at must be between [0,*countptr], except that -1 is * treated the same as *countptr for convenience. * - * Returns -1 on failure, 0 on success + * Aborts on OOM failure. */ -int -virInsertElementsN(void *ptrptr, size_t size, size_t at, - size_t *countptr, - size_t typematchDummy G_GNUC_UNUSED, - void *newelems, - bool clearOriginal, bool inPlace) +static void +virInsertElementInternal(void *ptrptr, + size_t size, + size_t at, + size_t *countptr, + void *newelems, + bool clearOriginal, + bool inPlace) { - if (at =3D=3D -1) { - at =3D *countptr; - } else if (at > *countptr) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("out of bounds index - count %zu at %zu"), - *countptr, at); - return -1; - } - if (inPlace) { *countptr +=3D 1; } else { @@ -212,10 +204,62 @@ virInsertElementsN(void *ptrptr, size_t size, size_t = at, /* NB: if inPlace, assume memory at the end wasn't initialized */ memset(*(char**)ptrptr + (size * at), 0, size); } +} + + +/** + * virInsertElementsN: + * @ptrptr: pointer to hold address of allocated memory + * @size: the size of one element in bytes + * @at: index within array where new elements should be added, -1 fo= r end + * @countptr: variable tracking number of elements currently allocated + * @typematchDummy: helper variable to consume results of compile time che= cks + * @newelems: pointer to array of one or more new elements to move into + * place (the originals will be zeroed out if successful + * and if clearOriginal is true) + * @clearOriginal: false if the new item in the array should be copied + * from the original, and the original left intact. + * true if the original should be 0'd out on success. + * @inPlace: false if we should expand the allocated memory before + * moving, true if we should assume someone else *has + * already* done that. + * + * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes + * long, to be *countptr elements long, then appropriately move + * the elements starting at ptrptr[at] up by 1 elements, copy the + * items from newelems into ptrptr[at], then store the address of + * allocated memory in *ptrptr and the new size in *countptr. If + * newelems is NULL, the new elements at ptrptr[at] are instead filled + * with zero. at must be between [0,*countptr], except that -1 is + * treated the same as *countptr for convenience. + * + * Returns -1 on failure, 0 on success + */ +int +virInsertElementsN(void *ptrptr, + size_t size, + size_t at, + size_t *countptr, + size_t typematchDummy G_GNUC_UNUSED, + void *newelems, + bool clearOriginal, + bool inPlace) +{ + if (at =3D=3D -1) { + at =3D *countptr; + } else if (at > *countptr) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("out of bounds index - count %zu at %zu"), + *countptr, at); + return -1; + } + + virInsertElementInternal(ptrptr, size, at, countptr, newelems, clearOr= iginal, inPlace); return 0; } + /** * virDeleteElementsN: * @ptrptr: pointer to hold address of allocated memory --=20 2.31.1