From nobody Mon Feb 9 03:34:16 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 207.211.31.81 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=1573660104; cv=none; d=zoho.com; s=zohoarc; b=mEMnncYBR9IkJLSuji0WS2tbziCcSMrOJtoKfJufwYDgnp1sGlbA6w/IOTg1UYEQUq+zbY1lG3ajtp807PypJ24fzBKPRCq3a6/QYlmVGJc+JGZA7MIQf710v10ZwouRJZQY0gWfSwdWGqjyaUP0NGsjx5od4QFqb6pu4z4M5bY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573660104; 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=OrfAksxCjY13W/SCn3MwOcDTU/f7h1HhOpPyCjdz2AY=; b=PYCx1DuSFy6nzpLzRUI9FZlzQdAJfbbU0yfUo4zPHgrMCYQE5iShzWz+5tOOSZV9IMd2K593mvZat7kFSqLCpx+RbUDVNga9dFQAtpNvF58KkNE4KEbu37sdxbjSMBSwqZ44TBmKEJ17m3oIYEZ2aTxpqKSndRnil6GzZn4Mc24= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1573660104922896.4543626245074; Wed, 13 Nov 2019 07:48:24 -0800 (PST) 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-387-J2zZ0oeuOR6IvTgFIlmuSw-1; Wed, 13 Nov 2019 10:48:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 482ABDBE5; Wed, 13 Nov 2019 15:48:13 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1CCB590A8; Wed, 13 Nov 2019 15:48:13 +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 CFAAB4E567; Wed, 13 Nov 2019 15:48:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xADFmB9g017672 for ; Wed, 13 Nov 2019 10:48:11 -0500 Received: by smtp.corp.redhat.com (Postfix) id 748F9104B4C9; Wed, 13 Nov 2019 15:48:11 +0000 (UTC) Received: from lpt.redhat.com (ovpn-200-61.brq.redhat.com [10.40.200.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF074101F967 for ; Wed, 13 Nov 2019 15:48:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573660103; 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=OrfAksxCjY13W/SCn3MwOcDTU/f7h1HhOpPyCjdz2AY=; b=ezElVqEO+U6LPKbGfaHweRTkbYTLQPasKT22Bg8FAu/yat4wqgESS7tWGqG2nGCvL65qNO X4x/Vhu4oAfF9zWVYOw7y7jsIKVcnSSfSEY3ROa5qdb+4fV7RE0Mh/ZdM9t8ugRxGr82pp Mtx1jPKwj0I1rzYxaBQFgo5+SKZPqXY= From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Wed, 13 Nov 2019 16:48:49 +0100 Message-Id: <2bbb2370a5d702f6adf817e9cf72327d7d355bcf.1573660076.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 08/12] util: use GRegex in virCommandRunRegex 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.23 X-MC-Unique: J2zZ0oeuOR6IvTgFIlmuSw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This saves us from allocating vars upfront, since GLib deals with that for us. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/util/vircommand.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 49432ddfcb..1ab3dbc819 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -22,7 +22,6 @@ #include =20 #include -#include #include #include #include @@ -3077,11 +3076,10 @@ virCommandRunRegex(virCommandPtr cmd, const char *prefix, int *exitstatus) { - int err; - regex_t *reg; - g_autofree regmatch_t *vars =3D NULL; + GRegex **reg =3D NULL; + g_autoptr(GMatchInfo) info =3D NULL; size_t i, j, k; - int totgroups =3D 0, ngroup =3D 0, maxvars =3D 0; + int totgroups =3D 0, ngroup =3D 0; char **groups; g_autofree char *outbuf =3D NULL; VIR_AUTOSTRINGLIST lines =3D NULL; @@ -3092,29 +3090,23 @@ virCommandRunRegex(virCommandPtr cmd, return -1; =20 for (i =3D 0; i < nregex; i++) { - err =3D regcomp(®[i], regex[i], REG_EXTENDED); - if (err !=3D 0) { - char error[100]; - regerror(err, ®[i], error, sizeof(error)); + g_autoptr(GError) err =3D NULL; + reg[i] =3D g_regex_new(regex[i], G_REGEX_OPTIMIZE, 0, &err); + if (!reg[i]) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to compile regex %s"), error); + _("Failed to compile regex %s"), err->message); for (j =3D 0; j < i; j++) - regfree(®[j]); + g_regex_unref(reg[j]); VIR_FREE(reg); return -1; } =20 totgroups +=3D nvars[i]; - if (nvars[i] > maxvars) - maxvars =3D nvars[i]; - } =20 /* Storage for matched variables */ if (VIR_ALLOC_N(groups, totgroups) < 0) goto cleanup; - if (VIR_ALLOC_N(vars, maxvars+1) < 0) - goto cleanup; =20 virCommandSetOutputBuffer(cmd, &outbuf); if (virCommandRun(cmd, exitstatus) < 0) @@ -3140,15 +3132,12 @@ virCommandRunRegex(virCommandPtr cmd, =20 ngroup =3D 0; for (i =3D 0; i < nregex; i++) { - if (regexec(®[i], p, nvars[i]+1, vars, 0) !=3D 0) + if (!(g_regex_match(reg[i], p, 0, &info))) break; =20 - /* NB vars[0] is the full pattern, so we offset j by 1 */ - for (j =3D 1; j <=3D nvars[i]; j++) { - if (VIR_STRNDUP(groups[ngroup++], p + vars[j].rm_so, - vars[j].rm_eo - vars[j].rm_so) < 0) - goto cleanup; - } + /* NB match #0 is the full pattern, so we offset j by 1 */ + for (j =3D 1; j <=3D nvars[i]; j++) + groups[ngroup++] =3D g_match_info_fetch(info, j); =20 } /* We've matched on the last regex, so callback time */ @@ -3170,7 +3159,7 @@ virCommandRunRegex(virCommandPtr cmd, } =20 for (i =3D 0; i < nregex; i++) - regfree(®[i]); + g_regex_unref(reg[i]); =20 VIR_FREE(reg); return ret; --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list