From nobody Mon Sep 16 19:18:21 2024 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=1695106869; cv=none; d=zohomail.com; s=zohoarc; b=ETVJ5PHeNbBIG/S5+TS+HWnQpQelwYWVtu0cv5LY5HT2lERyZv9NkXzdSpwDFbw3qCHgSdOAoQt3hk9eMozATieGYyHgow277lJScML3tOpYDbLlyKiCWgQIq2eCTNfoBIZDQgR4AZgbrIgTwiINmNAVnVRL5EeE3HufPc/TL90= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695106869; 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=uG2NBxbeDKNuphiKY5YckeDtIlgqHBWiO5LLr8pCgUg=; b=V9caK+4riqY7viaARAbFeB5nXJ3FePPKyJa85mIkH194DDVnqq6NwC9kgYl3ozIWfvyXAIfp3wQLXKQoVmQld12qfXzzj/c3OeHWkNHsiFO49g790BJv++eNOP0XfCXXjnV9x5Lo227V34yWtGEvr0eQWpzlt0JfGtbASG0RQ9U= 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 1695106869614173.21303191702032; Tue, 19 Sep 2023 00:01:09 -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-335-ECY_VVCdP-C_jrnia_66SA-1; Tue, 19 Sep 2023 03:00:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 4F694811E8F; Tue, 19 Sep 2023 07:00:09 +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 3BABE1C554; Tue, 19 Sep 2023 07:00:09 +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 30F25194658D; Tue, 19 Sep 2023 07:00:09 +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 CDB9619466FC for ; Tue, 19 Sep 2023 07:00:05 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AD82A40C2070; Tue, 19 Sep 2023 07:00:05 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.2.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55F1C40C2064 for ; Tue, 19 Sep 2023 07:00:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695106868; 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=uG2NBxbeDKNuphiKY5YckeDtIlgqHBWiO5LLr8pCgUg=; b=WoOUS0bISzfCj0sjNnn4pme4RuhcmDbFnT1BqB76B+qWLovI+lvQsPJ3NXQuZiaiqq7HoB IcBTK3oEdfW614GXDO+4oRnHrDaOAT/9mI7JRysWal5EhFEwXdyZ2ojITEDwbkZ+EUOUGb rxyrFXmFwdyAMLEbWyinmuAr8olmgJE= X-MC-Unique: ECY_VVCdP-C_jrnia_66SA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 4/4] virerror: Make virReportEnumRangeError() check for type mismatch Date: Tue, 19 Sep 2023 09:00:00 +0200 Message-ID: <1b90f5c98fb386e76eda9608e2cc151ed684d7c4.1695105934.git.mprivozn@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.5 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: 1695106871625100001 Content-Type: text/plain; charset="utf-8"; x-default="true" As can be seen from previous commits, it's fairly easy to pass a different type to virReportEnumRangeError() than the actual variable is of. So far, we have a sizeof() hack to check if some nonsensical types are not passed, e.g. it catches cases where a function name is passed instead of an enum. Extend the hack to check whether proper enum was passed. Signed-off-by: Michal Privoznik --- src/util/virerror.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/util/virerror.h b/src/util/virerror.h index ee85247433..d4b2679a09 100644 --- a/src/util/virerror.h +++ b/src/util/virerror.h @@ -161,16 +161,18 @@ void virReportSystemErrorFull(int domcode, #define virReportRestrictedError(...) \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_OPERATION_DENIED, \ __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) -/* The sizeof(...) comparison here is a hack to catch typos - * in the name of the enum by triggering a compile error, as well - * as detecting if you passed a typename that refers to a function - * or struct type, instead of an enum. It should get optimized away - * since sizeof() is known at compile time */ +/* The ternary operator here is a hack to catch typos in the name of + * the enum and mismatching enum by triggering a compile error, as + * well as detecting if you passed a typename that refers to a + * function or struct type, instead of an enum. It should get + * optimized away since the value is constant and thus is known at + * compile time. */ #define virReportEnumRangeError(typname, value) \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR, \ __FILE__, __FUNCTION__, __LINE__, \ "Unexpected enum value %d for %s", \ - value, sizeof((typname)1) !=3D 0 ? #typname : #ty= pname); + value, \ + (__typeof__(value))1 =3D=3D (typname)1 && sizeof(= (typname)1) !=3D 0 ? #typname : #typname) =20 #define virReportError(code, ...) \ virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \ --=20 2.41.0