From cccd82f8b9e2efbe6d92170ad0ef0e92081f2c00 Mon Sep 17 00:00:00 2001 From: brofield Date: Sun, 1 Dec 2024 12:36:37 +1000 Subject: [PATCH] Use mbstowcs_s on Windows to avoid warnings about insecure functions. --- LICENCE.txt | 2 +- SimpleIni.h | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/LICENCE.txt b/LICENCE.txt index 88aefbc..405d417 100644 --- a/LICENCE.txt +++ b/LICENCE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2006-2022 Brodie Thiesfield +Copyright (c) 2006-2024 Brodie Thiesfield Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/SimpleIni.h b/SimpleIni.h index 1f0c0ae..e922ba3 100644 --- a/SimpleIni.h +++ b/SimpleIni.h @@ -3063,14 +3063,18 @@ class SI_ConvertW { return a_uInputDataLen; } -#if defined(SI_NO_MBSTOWCS_NULL) || (!defined(_MSC_VER) && !defined(_linux)) + // get the required buffer size +#if defined(_MSC_VER) + size_t uBufSiz; + errno_t e = mbstowcs_s(&uBufSiz, NULL, 0, a_pInputData, a_uInputDataLen); + return (e == 0) ? uBufSiz : (size_t) -1; +#elif !defined(SI_NO_MBSTOWCS_NULL) + return mbstowcs(NULL, a_pInputData, a_uInputDataLen); +#else // fall back processing for platforms that don't support a NULL dest to mbstowcs // worst case scenario is 1:1, this will be a sufficient buffer size (void)a_pInputData; return a_uInputDataLen; -#else - // get the actual required buffer size - return mbstowcs(NULL, a_pInputData, a_uInputDataLen); #endif } @@ -3119,9 +3123,18 @@ class SI_ConvertW { } // convert to wchar_t +#if defined(_MSC_VER) + size_t uBufSiz; + errno_t e = mbstowcs_s(&uBufSiz, + a_pOutputData, a_uOutputDataSize, + a_pInputData, a_uInputDataLen); + (void)uBufSiz; + return (e == 0); +#else size_t retval = mbstowcs(a_pOutputData, a_pInputData, a_uOutputDataSize); return retval != (size_t)(-1); +#endif } /** Calculate the number of char required by the storage format of this