From nobody Mon Feb 9 07:23:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1666621088; cv=none; d=zohomail.com; s=zohoarc; b=E8FddczwuwMxG1utVpA/BtJRrsKDV29qBWJJshq2WvyE571WgeBKL9vlCa7/C01qjA2Zvdf4HJG1jaRXeYLf+ZNKdOFbk70SXa7YWYuoDqneeunDkun3SfJoaQyh5sXQQrDvHC2qC0lW+FR1GiWN1u9am2Te+tpvyEF1PDz9wNw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666621088; 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=q/R0qOEeQz7FAUstTmL2hF9X+fVxtIDPSf1PAKwPiQw=; b=JBIK6puEEmV+itCmWBO8gdeuOeya9DDrwyRFpVpTFBcoyg/B6G31Y9G1lJOMc+DK7JGj3yrhsFWS+aeMA1c58F0DN3my0JYTKI0kzU7gJJ1t3WnRNNVtBoRu9KbmPlSmUcJNtZZrxIqnldsJpm1sP7JvUymeXSghldxjK69b+rU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 1666621088653526.5403024728299; Mon, 24 Oct 2022 07:18:08 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-610-JYIgCROrNNCsMZnlfv_MAQ-1; Mon, 24 Oct 2022 10:15:41 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6BA911012471; Mon, 24 Oct 2022 14:15:11 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1880F2166B34; Mon, 24 Oct 2022 14:15:11 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id E29A61946595; Mon, 24 Oct 2022 14:15:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 00DCC19465B2 for ; Mon, 24 Oct 2022 14:15:10 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id DA96640C83C5; Mon, 24 Oct 2022 14:15:09 +0000 (UTC) Received: from speedmetal.lan (ovpn-208-27.brq.redhat.com [10.40.208.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30A3740C2064 for ; Mon, 24 Oct 2022 14:15:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666621087; 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=q/R0qOEeQz7FAUstTmL2hF9X+fVxtIDPSf1PAKwPiQw=; b=UqoRQvUOAtvXAYAb+YgvS+CB6pWs89suNqt0vwnJKMSwbVnYTinBSUdECOI9WFXRwjJxIK xdMyCBA/Twd5tDM/EsJvozyRXsfa2OplQr4EORJvNSf8Pfs/ngdHMrqfgo7c+uCTLVSHpf kvfRQqipQRds+tZVT/8JMteyhHnR4g0= X-MC-Unique: JYIgCROrNNCsMZnlfv_MAQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 11/43] util: xml: Remove double->(u)ll conversion in virXPath(U)LongLong Date: Mon, 24 Oct 2022 16:14:16 +0200 Message-Id: <5828129fae29faccd28bb28de4ebfcefa10dac69.1666619795.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 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: 1666621089369100001 Content-Type: text/plain; charset="utf-8" The conversion from double is not precise enough at the extremes so it must not be used. Spell out that the callers are required to use a string() conversion in the XPath expression and remove the code path handling the direct conversion from numbers. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virxml.c | 60 ++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index aec475ccfd..35c0340779 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -303,13 +303,15 @@ virXPathULongHex(const char *xpath, * virXPathULongLong: * @xpath: the XPath string to evaluate * @ctxt: an XPath context - * @value: the returned long long value + * @value: the returned unsigned long long value * - * Convenience function to evaluate an XPath number + * Convenience function to evaluate an XPath number. The @xpath expression + * must ensure that the evaluated value is returned as a string (use the + * 'string()' conversion in the expression). * * Returns 0 in case of success in which case @value is set, * or -1 if the XPath evaluation failed or -2 if the - * value doesn't have a long format. + * value doesn't have a unsigned long long format. */ int virXPathULongLong(const char *xpath, @@ -317,28 +319,14 @@ virXPathULongLong(const char *xpath, unsigned long long *value) { g_autoptr(xmlXPathObject) obj =3D NULL; - int ret =3D 0; - if ((ctxt =3D=3D NULL) || (xpath =3D=3D NULL) || (value =3D=3D NULL)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Invalid parameter to virXPathULong()")); + if (!(obj =3D virXPathEvalString(xpath, ctxt))) return -1; - } - obj =3D xmlXPathEval(BAD_CAST xpath, ctxt); - if ((obj !=3D NULL) && (obj->type =3D=3D XPATH_STRING) && - (obj->stringval !=3D NULL) && (obj->stringval[0] !=3D 0)) { - if (virStrToLong_ull((char *) obj->stringval, NULL, 10, value) < 0) - ret =3D -2; - } else if ((obj !=3D NULL) && (obj->type =3D=3D XPATH_NUMBER) && - (!(isnan(obj->floatval)))) { - *value =3D (unsigned long long) obj->floatval; - if (*value !=3D obj->floatval) - ret =3D -2; - } else { - ret =3D -1; - } - return ret; + if (virStrToLong_ull((char *) obj->stringval, NULL, 10, value) < 0) + return -2; + + return 0; } @@ -348,7 +336,9 @@ virXPathULongLong(const char *xpath, * @ctxt: an XPath context * @value: the returned long long value * - * Convenience function to evaluate an XPath number + * Convenience function to evaluate an XPath number. The @xpath expression + * must ensure that the evaluated value is returned as a string (use the + * 'string()' conversion in the expression). * * Returns 0 in case of success in which case @value is set, * or -1 if the XPath evaluation failed or -2 if the @@ -360,28 +350,14 @@ virXPathLongLong(const char *xpath, long long *value) { g_autoptr(xmlXPathObject) obj =3D NULL; - int ret =3D 0; - if ((ctxt =3D=3D NULL) || (xpath =3D=3D NULL) || (value =3D=3D NULL)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Invalid parameter to virXPathLongLong()")); + if (!(obj =3D virXPathEvalString(xpath, ctxt))) return -1; - } - obj =3D xmlXPathEval(BAD_CAST xpath, ctxt); - if ((obj !=3D NULL) && (obj->type =3D=3D XPATH_STRING) && - (obj->stringval !=3D NULL) && (obj->stringval[0] !=3D 0)) { - if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value) < 0) - ret =3D -2; - } else if ((obj !=3D NULL) && (obj->type =3D=3D XPATH_NUMBER) && - (!(isnan(obj->floatval)))) { - *value =3D (long long) obj->floatval; - if (*value !=3D obj->floatval) - ret =3D -2; - } else { - ret =3D -1; - } - return ret; + if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value) < 0) + return -2; + + return 0; } --=20 2.37.3