From nobody Mon Feb 9 18:20:08 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=1628773854; cv=none; d=zohomail.com; s=zohoarc; b=n7jIgDd3shNroyKd3zuERcnzIjOBOHERQJeOJslWQOs3uoG06v+4QCJGPqod4BnDRcsknguquF26jvLzJcDev9JGUAKHLS5qTjLQjv3iuO4fGKtfe+p8hyMPqSr4VrjY30kWFXfdsONepVmVczZFOH6Ap/VNl4iUr4ngP+kEkkg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628773854; 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=5VeH65NLE1H6QdY16tVYcd479jmRWB5Dx/ALTuRMeTo=; b=hU7fmDSwjUDCtMpyR3MWO/GH2aVkpDH76NncZLiDiblYpzaWSRlYr5IIbX4rRamRY8HTaFeDcJQwPRD5btKGOzo9VsSieSbFnhCePROV7NNlCzckhnKzIHXFJpX5ZRE0ZL7TJwREJemQc9Gabg2Fi6Q31V7F/bpChsgSgI1MPY4= 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 1628773854133251.96776453004884; Thu, 12 Aug 2021 06:10:54 -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-148-5RGB8yuYMxWhVe4JboEMow-1; Thu, 12 Aug 2021 09:10:51 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EA6738ED9FE; Thu, 12 Aug 2021 13:10:25 +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 98C2C6CA26; Thu, 12 Aug 2021 13:10:24 +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 4D2D54A700; Thu, 12 Aug 2021 13:10:24 +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 17CD1mRi029750 for ; Thu, 12 Aug 2021 09:01:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 785875D9DE; Thu, 12 Aug 2021 13:01:48 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF7BC5D9C6 for ; Thu, 12 Aug 2021 13:01:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628773853; 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=5VeH65NLE1H6QdY16tVYcd479jmRWB5Dx/ALTuRMeTo=; b=cwdo5iTLV7NzHImqxIBc1/yrv7I1OBR87qyucUZs5TbMsaJ4e3KtH6V3uTuX7U1wdvOPSV M0y39SNWvEKcYnycpr+SA6zpPmkB2vLtNFFFG9i6A+xG0OmFN2Yoc2icduNlYMB4PJGyrU pHA3swYLggiC3wRy2P/QiNBc5V2eYec= X-MC-Unique: 5RGB8yuYMxWhVe4JboEMow-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 6/6] vshStringToArray: Rewrite using 'g_strsplit' Date: Thu, 12 Aug 2021 15:01:36 +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.79 on 10.5.11.15 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: 1628773854923100001 Content-Type: text/plain; charset="utf-8" Use 'g_strsplit' to split the strings and then concatenate back when the escape sequence (',,') is used. Signed-off-by: Peter Krempa --- tools/vsh.c | 62 ++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index cdcf67684e..202a507f2e 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -122,48 +122,38 @@ int vshStringToArray(const char *str, char ***array) { - g_autofree char *str_copied =3D g_strdup(str); - char *str_tok =3D NULL; - char *tmp; - unsigned int nstr_tokens =3D 0; - char **arr =3D NULL; - size_t len =3D strlen(str_copied); - - /* tokenize the string from user and save its parts into an array */ - nstr_tokens =3D 1; - - /* count the delimiters, recognizing ,, as an escape for a - * literal comma */ - str_tok =3D str_copied; - while ((str_tok =3D strchr(str_tok, ','))) { - if (str_tok[1] =3D=3D ',') - str_tok++; - else - nstr_tokens++; - str_tok++; - } + g_auto(GStrv) tmp =3D NULL; + GStrv n; + size_t ntoks =3D 0; + bool concat =3D false; + + tmp =3D g_strsplit(str, ",", 0); + + *array =3D g_new0(char *, g_strv_length(tmp) + 1); + (*array)[ntoks++] =3D g_strdup(tmp[0]); - /* reserve the NULL element at the end */ - arr =3D g_new0(char *, nstr_tokens + 1); + /* undo splitting of comma escape (',,') by concatenating back on empt= y strings */ + for (n =3D tmp + 1; n[0]; n++) { + if (concat) { + g_autofree char *old =3D (*array)[ntoks - 1]; - /* tokenize the input string, while treating ,, as a literal comma */ - nstr_tokens =3D 0; - tmp =3D str_tok =3D str_copied; - while ((tmp =3D strchr(tmp, ','))) { - if (tmp[1] =3D=3D ',') { - memmove(&tmp[1], &tmp[2], len - (tmp - str_copied) - 2 + 1); - len--; - tmp++; + (*array)[ntoks - 1] =3D g_strconcat(old, ",", n[0], NULL); + concat =3D false; continue; } - *tmp++ =3D '\0'; - arr[nstr_tokens++] =3D g_strdup(str_tok); - str_tok =3D tmp; + + if (strlen(n[0]) =3D=3D 0) { + concat =3D true; + } else { + (*array)[ntoks++] =3D g_strdup(n[0]); + } } - arr[nstr_tokens++] =3D g_strdup(str_tok); - *array =3D arr; - return nstr_tokens; + /* corner case of ending with a single comma */ + if (concat) + (*array)[ntoks++] =3D g_strdup(""); + + return ntoks; } virErrorPtr last_error; --=20 2.31.1