Skip to content

Commit 490e242

Browse files
Merge pull request #186 from cjee21/fix
Fix warnings when building for Android and MinGW
2 parents 78a36f9 + e786ceb commit 490e242

4 files changed

Lines changed: 81 additions & 53 deletions

File tree

.github/workflows/ZenLib_Checks.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ jobs:
6363
- platform: Win32
6464
msystem: MINGW32
6565
- platform: x64
66-
msystem: MINGW64
66+
msystem: UCRT64
6767
fail-fast: false
6868
env:
6969
GENERATOR: "Unix Makefiles"
7070
CONFIGURATION: "Release"
71+
CXXFLAGS: -Werror
7172
steps:
7273
- name: Checkout ZenLib
7374
uses: actions/checkout@v5

Source/ZenLib/Format/Http/Http_Cookies.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,28 @@ void Cookies::Create_Lines(std::ostream& Out)
8686
if (Cookie->second.Expires!=(time_t)-1)
8787
{
8888
char Temp[200];
89-
#if defined(HAVE_GMTIME_R)
90-
struct tm Gmt_Temp;
91-
struct tm *Gmt=gmtime_r(&Cookie->second.Expires, &Gmt_Temp);
92-
#elif defined(_MSC_VER)
93-
struct tm Gmt_Temp;
94-
errno_t gmtime_s_Result=gmtime_s(&Gmt_Temp , &Cookie->second.Expires);
95-
struct tm* Gmt=gmtime_s_Result?NULL:&Gmt_Temp;
89+
#if defined(_WIN32)
90+
#if _CRT_USE_CONFORMING_ANNEX_K_TIME || (__STDC_WANT_LIB_EXT1__ && defined(__STDC_LIB_EXT1__))
91+
// C11 standard version on MSVC or other compilers
92+
tm Gmt_Temp;
93+
tm* Gmt = gmtime_s(&Cookie->second.Expires, &Gmt_Temp);
94+
#else
95+
// MSVC and MinGW-w64 argument order and return value differs from C11 standard
96+
tm Gmt_Temp;
97+
errno_t gmtime_s_Result = gmtime_s(&Gmt_Temp, &Cookie->second.Expires);
98+
tm* Gmt = gmtime_s_Result ? nullptr : &Gmt_Temp;
99+
#endif
100+
#elif defined(HAVE_GMTIME_R)
101+
// POSIX or C23
102+
tm Gmt_Temp;
103+
tm *Gmt = gmtime_r(&Cookie->second.Expires, &Gmt_Temp);
96104
#else
97-
#ifdef __GNUC__
98-
#warning "This version of ZenLib is not thread safe"
105+
// Fallback: not thread-safe, but prevents compile errors
106+
#ifdef __GNUC__
107+
#warning "This version of ZenLib is not thread safe"
108+
#endif
109+
tm *Gmt = gmtime(&Cookie->second.Expires);
99110
#endif
100-
struct tm *Gmt=gmtime(&Cookie->second.Expires);
101-
#endif
102-
103111
if (Gmt)
104112
if (strftime(Temp, 200, "%a, %d-%b-%Y %H:%M:%S GMT", Gmt))
105113
Out << "; expires=" << Temp;

Source/ZenLib/Utils.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ZenLib/Utils.h"
2020
#include <cmath>
2121
#include <complex>
22+
#include <limits>
2223
//---------------------------------------------------------------------------
2324

2425
namespace ZenLib
@@ -972,16 +973,16 @@ void int64u_int32u (int64u BigInt, int32u &High, int32u &Low)
972973
int32s float32_int32s (float32 F, bool Rounded)
973974
{
974975
//Out of boundaries
975-
if (F>=(int32s)0x7FFFFFFF)
976-
return (int32s)0x7FFFFFFF;
977-
if (F<=(int32s)0x80000000)
978-
return (int32s)0x80000000;
976+
if (F>=(float32)std::numeric_limits<int32s>::max())
977+
return std::numeric_limits<int32s>::max();
978+
if (F<=(float32)std::numeric_limits<int32s>::min())
979+
return std::numeric_limits<int32s>::min();
979980

980981
//Not rounded
981982
if (!Rounded)
982983
return (int32s)F;
983984
//Rounded
984-
int I1=(int)F;
985+
int32s I1=(int32s)F;
985986
if (F-I1>=0.5f)
986987
return I1+1;
987988
else
@@ -991,16 +992,16 @@ int32s float32_int32s (float32 F, bool Rounded)
991992
int64s float32_int64s (float32 F, bool Rounded)
992993
{
993994
//Out of boundaries
994-
if (F>=(int64s)0x7FFFFFFFFFFFFFFFLL)
995-
return (int64s)0x7FFFFFFFFFFFFFFFLL;
996-
if (F<=(int64s)0x8000000000000000LL)
997-
return (int64s)0x8000000000000000LL;
995+
if (F>=(float32)std::numeric_limits<int64s>::max())
996+
return std::numeric_limits<int64s>::max();
997+
if (F<=(float32)std::numeric_limits<int64s>::min())
998+
return std::numeric_limits<int64s>::min();
998999

9991000
//Not rounded
10001001
if (!Rounded)
10011002
return (int64s)F;
10021003
//Rounded
1003-
int I1=(int)F;
1004+
int64s I1=(int64s)F;
10041005
if (F-I1>=0.5f)
10051006
return I1+1;
10061007
else
@@ -1010,10 +1011,10 @@ int64s float32_int64s (float32 F, bool Rounded)
10101011
int32s float64_int32s (float64 F, bool Rounded)
10111012
{
10121013
//Out of boundaries
1013-
if (F>=(int32s)0x7FFFFFFF)
1014-
return (int32s)0x7FFFFFFF;
1015-
if (F<=(int32s)0x80000000)
1016-
return (int32s)0x80000000;
1014+
if (F>=(float64)std::numeric_limits<int32s>::max())
1015+
return std::numeric_limits<int32s>::max();
1016+
if (F<=(float64)std::numeric_limits<int32s>::min())
1017+
return std::numeric_limits<int32s>::min();
10171018

10181019
//Not rounded
10191020
if (!Rounded)
@@ -1029,10 +1030,10 @@ int32s float64_int32s (float64 F, bool Rounded)
10291030
int64s float64_int64s (float64 F, bool Rounded)
10301031
{
10311032
//Out of boundaries
1032-
if (F>=(int64s)0x7FFFFFFFFFFFFFFFLL)
1033-
return (int64s)0x7FFFFFFFFFFFFFFFLL;
1034-
if (F<=(int64s)0x8000000000000000LL)
1035-
return (int64s)0x8000000000000000LL;
1033+
if (F>=(float64)std::numeric_limits<int64s>::max())
1034+
return std::numeric_limits<int64s>::max();
1035+
if (F<=(float64)std::numeric_limits<int64s>::min())
1036+
return std::numeric_limits<int64s>::min();
10361037

10371038
//Not rounded
10381039
if (!Rounded)

Source/ZenLib/Ztring.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,18 +1306,27 @@ Ztring& Ztring::Date_From_Seconds_1970 (const int32s Value)
13061306
Ztring& Ztring::Date_From_Seconds_1970 (const int64s Value)
13071307
{
13081308
time_t Time=(time_t)Value;
1309-
#if defined(HAVE_GMTIME_R)
1310-
struct tm Gmt_Temp;
1311-
struct tm *Gmt=gmtime_r(&Time, &Gmt_Temp);
1312-
#elif defined(_MSC_VER)
1313-
struct tm Gmt_Temp;
1314-
errno_t gmtime_s_Result=gmtime_s(&Gmt_Temp , &Time);
1315-
struct tm* Gmt=gmtime_s_Result?NULL:&Gmt_Temp;
1309+
#if defined(_WIN32)
1310+
#if _CRT_USE_CONFORMING_ANNEX_K_TIME || (__STDC_WANT_LIB_EXT1__ && defined(__STDC_LIB_EXT1__))
1311+
// C11 standard version on MSVC or other compilers
1312+
tm Gmt_Temp;
1313+
tm* Gmt = gmtime_s(&Time, &Gmt_Temp);
1314+
#else
1315+
// MSVC and MinGW-w64 argument order and return value differs from C11 standard
1316+
tm Gmt_Temp;
1317+
errno_t gmtime_s_Result = gmtime_s(&Gmt_Temp, &Time);
1318+
tm* Gmt = gmtime_s_Result ? nullptr : &Gmt_Temp;
1319+
#endif
1320+
#elif defined(HAVE_GMTIME_R)
1321+
// POSIX or C23
1322+
tm Gmt_Temp;
1323+
tm *Gmt = gmtime_r(&Time, &Gmt_Temp);
13161324
#else
1317-
#ifdef __GNUC__
1318-
#warning "This version of ZenLib is not thread safe"
1319-
#endif
1320-
struct tm *Gmt=gmtime(&Time);
1325+
// Fallback: not thread-safe, but prevents compile errors
1326+
#ifdef __GNUC__
1327+
#warning "This version of ZenLib is not thread safe"
1328+
#endif
1329+
tm *Gmt = gmtime(&Time);
13211330
#endif
13221331
if (!Gmt)
13231332
{
@@ -1349,18 +1358,27 @@ Ztring& Ztring::Date_From_Seconds_1970 (const int64s Value)
13491358
Ztring& Ztring::Date_From_Seconds_1970_Local (const int32u Value)
13501359
{
13511360
time_t Time=(time_t)Value;
1352-
#if defined(HAVE_LOCALTIME_R)
1353-
struct tm Gmt_Temp;
1354-
struct tm *Gmt=localtime_r(&Time, &Gmt_Temp);
1355-
#elif defined(_MSC_VER)
1356-
struct tm Gmt_Temp;
1357-
errno_t localtime_s_Result=localtime_s(&Gmt_Temp , &Time);
1358-
struct tm* Gmt=localtime_s_Result?NULL:&Gmt_Temp;
1361+
#if defined(_WIN32)
1362+
#if _CRT_USE_CONFORMING_ANNEX_K_TIME || (__STDC_WANT_LIB_EXT1__ && defined(__STDC_LIB_EXT1__))
1363+
// C11 standard version on MSVC or other compilers
1364+
tm Gmt_Temp;
1365+
tm* Gmt = localtime_s(&Time, &Gmt_Temp);
1366+
#else
1367+
// MSVC and MinGW-w64 argument order and return value differs from C11 standard
1368+
tm Gmt_Temp;
1369+
errno_t localtime_s_Result = localtime_s(&Gmt_Temp, &Time);
1370+
tm* Gmt = localtime_s_Result ? nullptr : &Gmt_Temp;
1371+
#endif
1372+
#elif defined(HAVE_GMTIME_R)
1373+
// POSIX or C23
1374+
tm Gmt_Temp;
1375+
tm *Gmt = localtime_r(&Time, &Gmt_Temp);
13591376
#else
1360-
#ifdef __GNUC__
1361-
#warning "This version of ZenLib is not thread safe"
1362-
#endif
1363-
struct tm *Gmt=localtime(&Time);
1377+
// Fallback: not thread-safe, but prevents compile errors
1378+
#ifdef __GNUC__
1379+
#warning "This version of ZenLib is not thread safe"
1380+
#endif
1381+
tm *Gmt = localtime(&Time);
13641382
#endif
13651383
Ztring DateT;
13661384
Ztring Date;

0 commit comments

Comments
 (0)