- Document FreeBSD quirk.
- Fix FreeBSD build: Cast to
int
to allowstd::min
to work under FreeBSD.
Context: #9598 (comment)
int
to allow std::min
to work under FreeBSD.Context: #9598 (comment)
So this was originally applied in #2695:
When compiling on FreeBSD, the calculation here returns an unsigned int. This causes a compile-time error with std::min, which cannot compare signed with unsigned integers. This pull inserts an explicit cast, treating the calculated value as signed, keeping the compiler happy.
Then reverted in #9598 to:
Improve readability by removing redundant casts to same type (on all platforms)
Now we’re re-adding (int) again but only for FreeBSD.
I’ve just built master (3f398d7) successfully on a FreeBSD 11.1 VM, so I’m curious to know which version of FreeBSD this is broken on?
Regardless of the above, the current patch doesn’t compile:
0fvisibility=hidden -c -o libbitcoin_server_a-init.o `test -f 'init.cpp' || echo './'`init.cpp
1init.cpp:969:2: error: invalid preprocessing directive #fi
2 #fi
3 ^~
4init.cpp:964:0: error: unterminated #else
5 #if defined(__FreeBSD__) || defined(__DragonFly__)
6
7make[2]: *** [libbitcoin_server_a-init.o] Error 1
8Makefile:5817: recipe for target 'libbitcoin_server_a-init.o' failed
960@@ -961,7 +961,12 @@ bool AppInitParameterInteraction()
961 nMaxConnections = std::max(nUserMaxConnections, 0);
962
963 // Trim requested connection counts, to fit into system limitations
964+#if defined(__FreeBSD__) || defined(__DragonFly__)
965+ // See #2695: Explictly cast to int to allow std::min to work under FreeBSD
966+ nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS)), 0);
967+#else
968 nMaxConnections = std::max(std::min(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);
969+#fi
Closing this temporarily while waiting for input from @anton48.
If we can reproduce under FreeBSD 11.1 I’ll re-open.
Agree with @fanquake.
Having different code for each platform makes testing a nightmare. Btw. I believe you can specify the function template you want to call with std::min<type>(a,b)
nMaxConnections = std::max(std::min<int>(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);
? yes, it can be compiled without errors on FreeBSD (tested on 10 and 11 versions).