From 032e3404cef69a40f2ac078923769890678f6530 Mon Sep 17 00:00:00 2001 From: Jason Valenzuela Date: Mon, 9 Oct 2023 14:12:46 -0400 Subject: [PATCH] Change data types for DSCP configuration parameters. Addresses MinGW -Wformat warnings because the "hhu" scanf specifier doesn't seem to be supported in MinGW(x86_64-8.1.0-posix-seh-rt_v6-rev0). This approach was used instead of the __USE_MINGW_ANSI_STDIO macro because the latter is a private macro and is probably not intended for external use. --- source/src/ports/nvdata/nvqos.c | 43 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/source/src/ports/nvdata/nvqos.c b/source/src/ports/nvdata/nvqos.c index d1d1e68d4..8a362619f 100644 --- a/source/src/ports/nvdata/nvqos.c +++ b/source/src/ports/nvdata/nvqos.c @@ -14,6 +14,7 @@ #include "nvqos.h" #include +#include #include #include @@ -32,11 +33,18 @@ int NvQosLoad(CipQosObject *p_qos) { int rd_cnt = 0; EipStatus eip_status = kEipStatusError; - CipUsint dscp_urgent = 0; - CipUsint dscp_scheduled = 0; - CipUsint dscp_high = 0; - CipUsint dscp_low = 0; - CipUsint dscp_explicit = 0; + /* + * Data type for these parameters explicitly differs from the specification(CipUsint), + * because the required scanf specifier to read that type, "hhu", is not supported + * in MinGW. The generic unsigned type is used instead, which is guaranteed to fit + * a CipUsint, followed by a check after scanf to ensure valid values before + * casting them to CipUsint upon assignment to the output structure. + */ + unsigned int dscp_urgent = 0; + unsigned int dscp_scheduled = 0; + unsigned int dscp_high = 0; + unsigned int dscp_low = 0; + unsigned int dscp_explicit = 0; FILE *p_file = ConfFileOpen(false, QOS_CFG_NAME); if (NULL != p_file) { @@ -48,7 +56,7 @@ int NvQosLoad(CipQosObject *p_qos) { /* Read input data */ rd_cnt = fscanf(p_file, - " %" SCNu8 ", %" SCNu8 ", %" SCNu8 ", %" SCNu8 ", %" SCNu8 "\n", + " %u, %u, %u, %u, %u\n", &dscp_urgent, &dscp_scheduled, &dscp_high, @@ -64,13 +72,26 @@ int NvQosLoad(CipQosObject *p_qos) { eip_status = ConfFileClose(&p_file); } if (kEipStatusOk == eip_status) { + + /* + * Ensure all values read from the configuration file are within the CipUsint range; + * see above comments for these dscp_* local variables for details. + */ + if ( (dscp_urgent > UCHAR_MAX) + || (dscp_scheduled > UCHAR_MAX) + || (dscp_high > UCHAR_MAX) + || (dscp_low > UCHAR_MAX) + || (dscp_explicit > UCHAR_MAX) ) { + rd_cnt = 0; + } + /* If all data were read copy them to the global QoS object. */ if (5 == rd_cnt) { - p_qos->dscp.urgent = dscp_urgent; - p_qos->dscp.scheduled = dscp_scheduled; - p_qos->dscp.high = dscp_high; - p_qos->dscp.low = dscp_low; - p_qos->dscp.explicit_msg = dscp_explicit; + p_qos->dscp.urgent = (CipUsint)dscp_urgent; + p_qos->dscp.scheduled = (CipUsint)dscp_scheduled; + p_qos->dscp.high = (CipUsint)dscp_high; + p_qos->dscp.low = (CipUsint)dscp_low; + p_qos->dscp.explicit_msg = (CipUsint)dscp_explicit; } else { eip_status = kEipStatusError; }