From 17ad4a12dd40799655b71a93de1209a6a0f24204 Mon Sep 17 00:00:00 2001 From: Chris Liddell Date: Fri, 25 Oct 2019 13:52:44 +0100 Subject: Coverity issues: Assignment of overlapping union members Strictly speaking assigning one element of union to another, overlapping element of a different size is undefined behavior, hence assign to intermediate variables before setting the other element in the union. Coverity #: 350159, 350173, 350205, 350176, 350215, 350192 --- base/gsparam.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'base/gsparam.c') diff --git a/base/gsparam.c b/base/gsparam.c index b235d8601..80cb0b2aa 100644 --- a/base/gsparam.c +++ b/base/gsparam.c @@ -171,15 +171,25 @@ param_coerce_typed(gs_param_typed_value * pvalue, gs_param_type req_type, * right now we can't. However, a 0-length heterogenous array * will satisfy a request for any specific type. */ + /* Strictly speaking assigning one element of union + * to another, overlapping element of a different size is + * undefined behavior, hence assign to intermediate variables + */ switch (pvalue->type /* actual type */ ) { case gs_param_type_int: switch (req_type) { case gs_param_type_long: - pvalue->value.l = pvalue->value.i; + { + long l = (long)pvalue->value.i; + pvalue->value.l = l; goto ok; + } case gs_param_type_float: - pvalue->value.f = (float)pvalue->value.l; + { + float fl = (float)pvalue->value.l; + pvalue->value.f = fl; goto ok; + } default: break; } @@ -187,15 +197,22 @@ param_coerce_typed(gs_param_typed_value * pvalue, gs_param_type req_type, case gs_param_type_long: switch (req_type) { case gs_param_type_int: + { + int int1; #if ARCH_SIZEOF_INT < ARCH_SIZEOF_LONG if (pvalue->value.l != (int)pvalue->value.l) return_error(gs_error_rangecheck); #endif - pvalue->value.i = (int)pvalue->value.l; + int1 = (int)pvalue->value.l; + pvalue->value.i = int1; goto ok; + } case gs_param_type_float: - pvalue->value.f = (float)pvalue->value.l; + { + float fl = (float)pvalue->value.l; + pvalue->value.f = fl; goto ok; + } default: break; } -- cgit v1.2.1