From nobody Sun Feb 8 19:59:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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 170.10.129.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=1639052558; cv=none; d=zohomail.com; s=zohoarc; b=iu1CGUKCIjOAOyuGd8qdyLsHdAvigzjURViqulrdzxPdt0c3oIfwxeBcdyZB1UhnoB0odd7KCFlwV+g1j/sj/3ys+pg5MphJRNnV6aek/N9PrQwX0TyQhvB6OxCUeOwvnTYNDohrwgVUJ/UBbqfDxGELWBJmVuN8J7gSYP4zngo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639052558; 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=xKr95M0f+pgobKSVeIGucpLhPMQstfbdrVIgK9/3wSY=; b=eHiE8EU0SJGrh3WL/HUsX7CbA3/NOTolhzs/JQTlOy8eKsh6F5NSNpDo+33t/4+A4XqOrI0bQRWnmIWnedVLvc7SLkHVe4HMhkQA7Qq9XEV0qi2Cht91khGFWGCZ6/xhR33ICVD9PBhvUSCG1f6JBccZkUHIfeX43691uJuCDiE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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 [170.10.129.124]) by mx.zohomail.com with SMTPS id 1639052558383366.4325894511552; Thu, 9 Dec 2021 04:22:38 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-365-lhBauNq6O0SSBOI_e3s4Hg-1; Thu, 09 Dec 2021 07:22:33 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3C6D8100C664; Thu, 9 Dec 2021 12:22:28 +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 1D16C60BF1; Thu, 9 Dec 2021 12:22:28 +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 E866C1802E2D; Thu, 9 Dec 2021 12:22:27 +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 1B9CMQRm015670 for ; Thu, 9 Dec 2021 07:22:26 -0500 Received: by smtp.corp.redhat.com (Postfix) id 90CF55ED2B; Thu, 9 Dec 2021 12:22:26 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5DCF5E24E for ; Thu, 9 Dec 2021 12:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639052557; 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=xKr95M0f+pgobKSVeIGucpLhPMQstfbdrVIgK9/3wSY=; b=T/VH2/GIIFKukUXQgVQqGgnWYTSw8dC4wtRj8uFpCCl69WmVO7zrB6ytM8t3tgkJPj77fZ 2miCEZvFx6fiLv8f/hw4ydJuuxSNXbcDrkHrj+qh77lWJ7v/uHq52aB2PXNql/wpsNVdlL xRFaKXN8S/qV1dDETfpjKLl0YiKLmPs= X-MC-Unique: lhBauNq6O0SSBOI_e3s4Hg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 27/36] util: bitmap: Unify parsing of bitmaps Date: Thu, 9 Dec 2021 13:18:32 +0100 Message-Id: <93c4e1f90c18454c3dc7ca7ec286e3e325116bbd.1639052248.git.pkrempa@redhat.com> 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.79 on 10.5.11.12 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: 1639052558988100001 Content-Type: text/plain; charset="utf-8" There were two separate instances of string->virBitmap code: virBitmapParseInternal and virBitmapParseUnlimited. By adding a flag to switch to expanding APIs we can merge the two implementations into one. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virbitmap.c | 115 ++++++++++--------------------------------- 1 file changed, 26 insertions(+), 89 deletions(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 4047870e04..5b9204cbd7 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -367,6 +367,7 @@ virBitmapFormat(virBitmap *bitmap) * virBitmapParseInternal: * @str: points to a string representing a human-readable bitmap * @bitmap: a bitmap populated from @str + * @limited: Don't use self-expanding APIs, report error if bit exceeds bi= tmap size * * This function is the counterpart of virBitmapFormat. This function crea= tes * a bitmap, in which bits are set according to the content of @str. @@ -379,7 +380,8 @@ virBitmapFormat(virBitmap *bitmap) */ static int virBitmapParseInternal(const char *str, - virBitmap *bitmap) + virBitmap *bitmap, + bool limited) { bool neg =3D false; const char *cur =3D str; @@ -421,11 +423,19 @@ virBitmapParseInternal(const char *str, if (*cur =3D=3D ',' || *cur =3D=3D 0) { if (neg) { - if (virBitmapClearBit(bitmap, start) < 0) - goto error; + if (limited) { + if (virBitmapClearBit(bitmap, start) < 0) + goto error; + } else { + virBitmapClearBitExpand(bitmap, start); + } } else { - if (virBitmapSetBit(bitmap, start) < 0) - goto error; + if (limited) { + if (virBitmapSetBit(bitmap, start) < 0) + goto error; + } else { + virBitmapSetBitExpand(bitmap, start); + } } } else if (*cur =3D=3D '-') { if (neg) @@ -442,8 +452,12 @@ virBitmapParseInternal(const char *str, cur =3D tmp; for (i =3D start; i <=3D last; i++) { - if (virBitmapSetBit(bitmap, i) < 0) - goto error; + if (limited) { + if (virBitmapSetBit(bitmap, i) < 0) + goto error; + } else { + virBitmapSetBitExpand(bitmap, i); + } } virSkipSpaces(&cur); @@ -491,7 +505,7 @@ virBitmapParse(const char *str, { g_autoptr(virBitmap) tmp =3D virBitmapNew(bitmapSize); - if (virBitmapParseInternal(str, tmp) < 0) + if (virBitmapParseInternal(str, tmp, true) < 0) return -1; *bitmap =3D g_steal_pointer(&tmp); @@ -518,89 +532,12 @@ virBitmapParse(const char *str, virBitmap * virBitmapParseUnlimited(const char *str) { - virBitmap *bitmap =3D virBitmapNew(0); - bool neg =3D false; - const char *cur =3D str; - char *tmp; - size_t i; - int start, last; - - if (!str) - goto error; - - virSkipSpaces(&cur); - - if (*cur =3D=3D '\0') - goto error; - - while (*cur !=3D 0) { - /* - * 3 constructs are allowed: - * - N : a single CPU number - * - N-M : a range of CPU numbers with N < M - * - ^N : remove a single CPU number from the current set - */ - if (*cur =3D=3D '^') { - cur++; - neg =3D true; - } - - if (!g_ascii_isdigit(*cur)) - goto error; - - if (virStrToLong_i(cur, &tmp, 10, &start) < 0) - goto error; - if (start < 0) - goto error; - - cur =3D tmp; - - virSkipSpaces(&cur); + g_autoptr(virBitmap) tmp =3D virBitmapNew(0); - if (*cur =3D=3D ',' || *cur =3D=3D 0) { - if (neg) { - virBitmapClearBitExpand(bitmap, start); - } else { - virBitmapSetBitExpand(bitmap, start); - } - } else if (*cur =3D=3D '-') { - if (neg) - goto error; - - cur++; - virSkipSpaces(&cur); - - if (virStrToLong_i(cur, &tmp, 10, &last) < 0) - goto error; - if (last < start) - goto error; - - cur =3D tmp; - - for (i =3D start; i <=3D last; i++) - virBitmapSetBitExpand(bitmap, i); - - virSkipSpaces(&cur); - } - - if (*cur =3D=3D ',') { - cur++; - virSkipSpaces(&cur); - neg =3D false; - } else if (*cur =3D=3D 0) { - break; - } else { - goto error; - } - } - - return bitmap; + if (virBitmapParseInternal(str, tmp, false) < 0) + return NULL; - error: - virReportError(VIR_ERR_INVALID_ARG, - _("Failed to parse bitmap '%s'"), NULLSTR(str)); - virBitmapFree(bitmap); - return NULL; + return g_steal_pointer(&tmp); } --=20 2.31.1