From nobody Sun Feb 8 21:09:46 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=1686556557; cv=none; d=zohomail.com; s=zohoarc; b=QHvna9cyB++nUY+U9gqkpsxrsOCJMdWNBfeOrkZEtl91z7e4oCNwuJEluRinPXTChF8B/i2BXXFl9p0AdRzWYLgU9W94VD2/fIjlBGsG0vGyLUlQYQ6YIYFnq4Vj984qIFbJbyUkZF2mDTHZDtLWb9fNkjOG6WOXZU2uq8T8fwY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686556557; 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=Td9anoz6+cDuUTNHgQChjPKP1n+5WzBbXThkUKoER9E=; b=C+K5+GBLxVt9vFDFmnzLzFDlE0wGhaGNn593S2gnPBUXoLWDZ+6UaO68M5Whsh2X1ns8/Fcz+3TwqUlReAxMsPVvX0jock5ZXq9vGoyIG+Nt5Qwhw6tjXgDZO65VfuUkGg4J2F+4j4HoDtdtCyffcl/j4QtTgg5xU5949wh5vmc= 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 1686556557118162.746565875486; Mon, 12 Jun 2023 00:55:57 -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-671-soQeGjLrPAyBC-GPENgAVQ-1; Mon, 12 Jun 2023 03:55:51 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 605738582A4; Mon, 12 Jun 2023 07:55:49 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A5B4492CAC; Mon, 12 Jun 2023 07:55:49 +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 2CC7319451C4; Mon, 12 Jun 2023 07:55:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id BAFDE194658C for ; Mon, 12 Jun 2023 07:55:44 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id ABEFD10ED7; Mon, 12 Jun 2023 07:55:44 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A52FC11324 for ; Mon, 12 Jun 2023 07:55:44 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 88411185A78B for ; Mon, 12 Jun 2023 07:55:44 +0000 (UTC) Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-561-0SWDUGZROn2Fg-hnIQd-ew-1; Mon, 12 Jun 2023 03:55:43 -0400 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-977d0333523so493496566b.0 for ; Mon, 12 Jun 2023 00:55:42 -0700 (PDT) Received: from wheatley.localdomain (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id gz18-20020a170906f2d200b009659ad1072fsm4822871ejb.113.2023.06.12.00.55.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 00:55:41 -0700 (PDT) Received: from wheatley.. (wheatley.k8r.cz [127.0.0.1]) by wheatley.localdomain (Postfix) with ESMTP id 85CD711B85754 for ; Mon, 12 Jun 2023 09:55:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686556556; 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=Td9anoz6+cDuUTNHgQChjPKP1n+5WzBbXThkUKoER9E=; b=EeDj5qm82fYAESXDWAUwNfjpJRE5VgOvtq8HE2E25bzrAvpFyINRawgbgywHe0ie8el8RJ ZAwHVMbPuhV6a2pl4ivTaLWo4ggi/9ZfZCE8L1Khv777cBNgBlIynSZa3UnOTIyiQER88M N1WuaGw3xqblP5RdYXIVEs2fRd2RmRQ= X-MC-Unique: soQeGjLrPAyBC-GPENgAVQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: 0SWDUGZROn2Fg-hnIQd-ew-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686556542; x=1689148542; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Td9anoz6+cDuUTNHgQChjPKP1n+5WzBbXThkUKoER9E=; b=ZuyeOYy/EtX2/OVGGjwLafqZLyv9A1XFlLqSR3Bm6XRSlSpm7yTUmIFZkWVy1PgKWM O1E1Z0NmwrLluxzoVm5mn+nAr1O0rmhy/pIvBxs0jObK0J+fKJL8YtichsUJizyrnqyT UlYKxziagc2Lbn4JQLTInANq/RYbZf5UKLmgltW73Tblj4n8CX2lUbl1JEvrTsNd5pNn 6ZFxnZyc18wE9IRBtwx8vzVLUAQOpf1622JWWeZLMMchAIlCjqVuViIfGxPY/f4ryQiD UV36DaSVXhcDU4ItFU3mo6SJFLH+t4zDF0dMEPJI6HOr/L78EdH0U95OCCYWBjxtGJRr sPtQ== X-Gm-Message-State: AC+VfDw1GpVpum/y22OBapJHjbEGfI1/PXjWor9a8+1pUxvTPQwX5ojM oVtjd1nAtna8YJq22AoOQdcB3uIrqAVF/5zxpslt0zINwGfwDjSsQkEQ2Q8nfL0P/Dwz33Ozrkh QX437A8BvSNqoh/QQt71ndZTHV/W+GE1Mk94EA3ddaCDFQlVaPhrfF5VT5wdkpGvpyDNI8Icgk5 PRHngmwA== X-Received: by 2002:a17:907:3da7:b0:974:c32c:b484 with SMTP id he39-20020a1709073da700b00974c32cb484mr10796164ejc.72.1686556542062; Mon, 12 Jun 2023 00:55:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4jklDJqdU7EzJo59ZVDn4i2F7KckfubqA0gSQ6DxVl2XWiFtQRexnDDkKtbTnQ1S6sLl6kfQ== X-Received: by 2002:a17:907:3da7:b0:974:c32c:b484 with SMTP id he39-20020a1709073da700b00974c32cb484mr10796151ejc.72.1686556541675; Mon, 12 Jun 2023 00:55:41 -0700 (PDT) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH 3/4] util: Use g_ascii_strtoll Date: Mon, 12 Jun 2023 09:55:36 +0200 Message-ID: <3f1c61194a6f761b192b31c8fc843a759fe8c0bb.1686556413.git.mkletzan@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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.9 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: 1686556558703100004 Content-Type: text/plain; charset="utf-8"; x-default="true" This has two main advantages: - it parses the number with C locale explicitly - it behaves the same on Windows as on Linux and BSD both of which are wanted behaviours. Signed-off-by: Martin Kletzander --- src/util/virstring.c | 54 +++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/util/virstring.c b/src/util/virstring.c index 635685eed4a4..6b728ff04759 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -35,7 +35,7 @@ =20 VIR_LOG_INIT("util.string"); =20 -/* Like strtol, but produce an "int" result, and check more carefully. +/* Like strtol with C locale, but produce an "int" result, and check more = carefully. Return 0 upon success; return -1 to indicate failure. When END_PTR is NULL, the byte after the final valid digit must be NUL. Otherwise, it's like strtol and lets the caller check any suffix for @@ -44,12 +44,12 @@ VIR_LOG_INIT("util.string"); int virStrToLong_i(char const *s, char **end_ptr, int base, int *result) { - long int val; + long long val; char *p; int err; =20 errno =3D 0; - val =3D strtol(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoll(s, &p, base); err =3D (errno || (!end_ptr && *p) || p =3D=3D s || (int) val !=3D val= ); if (end_ptr) *end_ptr =3D p; @@ -65,22 +65,22 @@ virStrToLong_i(char const *s, char **end_ptr, int base,= int *result) int virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *res= ult) { - unsigned long int val; + unsigned long long val; char *p; bool err =3D false; =20 errno =3D 0; - val =3D strtoul(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoull(s, &p, base); =20 /* This one's tricky. We _want_ to allow "-1" as shorthand for * UINT_MAX regardless of whether long is 32-bit or 64-bit. But - * strtoul treats "-1" as ULONG_MAX, and going from ulong back - * to uint differs depending on the size of long. */ - if (sizeof(long) > sizeof(int) && memchr(s, '-', p - s)) { + * g_ascii_strtoull treats "-1" as ULLONG_MAX, and going from ullong b= ack + * to uint differs depending on the size of uint. */ + if (memchr(s, '-', p - s)) { if (-val > UINT_MAX) err =3D true; else - val &=3D 0xffffffff; + val &=3D UINT_MAX; } =20 err |=3D (errno || (!end_ptr && *p) || p =3D=3D s || (unsigned int) va= l !=3D val); @@ -97,12 +97,12 @@ virStrToLong_ui(char const *s, char **end_ptr, int base= , unsigned int *result) int virStrToLong_uip(char const *s, char **end_ptr, int base, unsigned int *re= sult) { - unsigned long int val; + unsigned long long val; char *p; bool err =3D false; =20 errno =3D 0; - val =3D strtoul(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoull(s, &p, base); err =3D (memchr(s, '-', p - s) || errno || (!end_ptr && *p) || p =3D=3D s || (unsigned int) val != =3D val); if (end_ptr) @@ -121,13 +121,25 @@ virStrToLong_uip(char const *s, char **end_ptr, int b= ase, unsigned int *result) int virStrToLong_ul(char const *s, char **end_ptr, int base, unsigned long *re= sult) { - unsigned long int val; + unsigned long long val; char *p; - int err; + bool err =3D false; =20 errno =3D 0; - val =3D strtoul(s, &p, base); /* exempt from syntax-check */ - err =3D (errno || (!end_ptr && *p) || p =3D=3D s); + val =3D g_ascii_strtoull(s, &p, base); + + /* This one's tricky. We _want_ to allow "-1" as shorthand for + * ULONG_MAX regardless of whether long is 32-bit or 64-bit. But + * g_ascii_strtoull treats "-1" as ULLONG_MAX, and going from ullong b= ack + * to ulong differs depending on the size of ulong. */ + if (memchr(s, '-', p - s)) { + if (-val > ULONG_MAX) + err =3D true; + else + val &=3D ULONG_MAX; + } + + err |=3D (errno || (!end_ptr && *p) || p =3D=3D s || (unsigned long) v= al !=3D val); if (end_ptr) *end_ptr =3D p; if (err) @@ -142,14 +154,14 @@ int virStrToLong_ulp(char const *s, char **end_ptr, int base, unsigned long *result) { - unsigned long int val; + unsigned long long val; char *p; int err; =20 errno =3D 0; - val =3D strtoul(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoull(s, &p, base); err =3D (memchr(s, '-', p - s) || - errno || (!end_ptr && *p) || p =3D=3D s); + errno || (!end_ptr && *p) || p =3D=3D s || (unsigned long) val = !=3D val); if (end_ptr) *end_ptr =3D p; if (err) @@ -167,7 +179,7 @@ virStrToLong_ll(char const *s, char **end_ptr, int base= , long long *result) int err; =20 errno =3D 0; - val =3D strtoll(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoll(s, &p, base); err =3D (errno || (!end_ptr && *p) || p =3D=3D s); if (end_ptr) *end_ptr =3D p; @@ -189,7 +201,7 @@ virStrToLong_ull(char const *s, char **end_ptr, int bas= e, int err; =20 errno =3D 0; - val =3D strtoull(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoull(s, &p, base); err =3D (errno || (!end_ptr && *p) || p =3D=3D s); if (end_ptr) *end_ptr =3D p; @@ -210,7 +222,7 @@ virStrToLong_ullp(char const *s, char **end_ptr, int ba= se, int err; =20 errno =3D 0; - val =3D strtoull(s, &p, base); /* exempt from syntax-check */ + val =3D g_ascii_strtoull(s, &p, base); err =3D (memchr(s, '-', p - s) || errno || (!end_ptr && *p) || p =3D=3D s); if (end_ptr) --=20 2.41.0