From 3b1020ac805e3414f309346bf4f798df0ce34dad Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 20 Feb 2018 15:56:52 +0000 Subject: [PATCH] util: add a virReportEnumRangeError for bad value reporting MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit To ensure we have standardized error messages when reporting problems with enum values being out of a range, add virReportEnumRangeError(). virReportEnumRangeError(virDomainState, 34); results in a message "internal error: Unexpected enum value 34 for virDomainState" Signed-off-by: Daniel P. Berrangé --- src/util/virerror.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/util/virerror.h b/src/util/virerror.h index cf434f45fc..f0df8f24de 100644 --- a/src/util/virerror.h +++ b/src/util/virerror.h @@ -164,7 +164,16 @@ 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 */ +# define virReportEnumRangeError(typname, value) \ + virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR, \ + __FILE__, __FUNCTION__, __LINE__, \ + "Unexpected enum value %d for %s", \ + value, sizeof((typename)1) != 0 ? #typname : #typname); void virReportOOMErrorFull(int domcode, const char *filename, -- 2.39.5