mosra / corrade

C++11 multiplatform utility library
https://magnum.graphics/corrade/
Other
486 stars 107 forks source link

error: cannot initialize a variable of type 'const char *const' with an rvalue of type 'int' #164

Closed yurivict closed 1 year ago

yurivict commented 1 year ago
/usr/ports/devel/corrade/work/corrade-2020.06-1214-g3bf6057d/src/Corrade/Utility/Implementation/ErrorString.cpp:68:23: error: cannot initialize a variable of type 'const char *const' with an rvalue of type 'int'
    const char* const string = strerror_r(error, buffer, Containers::arraySize(buffer));
                      ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

rev. 2020.06-1214-g3bf6057d clang-14 FreeBSD 13.1

mosra commented 1 year ago

Hmm, that's strange, because I have a preprocessor branch that is attempting to select the right variant:

https://github.com/mosra/corrade/blob/3bf6057d4e345bc92265e853bfbdd8a38add1142/src/Corrade/Utility/Implementation/ErrorString.cpp#L63-L69

Man pages tell me that _POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE is the expression to check, but maybe _POSIX_C_SOURCE isn't defined by BSD implicitly? (I have to admit my knowledge is very sparse in this area. :sweat_smile:) To be clear, I didn't want to #define _POSIX_C_SOURCE myself to any value to prevent unexpected ABI issues, just wanted to detect which function is actually available.

So, given that I already have to check for Apple and Emscripten explicitly anyway, should I check for BSD this way as well? I.e., would the following work? Is BSD the right define that covers FreeBSD, OpenBSD and others?

diff --git a/src/Corrade/Utility/Implementation/ErrorString.cpp b/src/Corrade/Utility/Implementation/ErrorString.cpp
index 675a6a9e3..66743901f 100644
--- a/src/Corrade/Utility/Implementation/ErrorString.cpp
+++ b/src/Corrade/Utility/Implementation/ErrorString.cpp
@@ -60,7 +60,7 @@ void printErrnoErrorString(Debug& debug, const int error) {
        idea. The POSIX variant returns int(0) on success, while the GNU variant
        may return a pointer to a statically allocated string instead of filling
        the buffer. Sigh. */
-    #if ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE) || defined(CORRADE_TARGET_EMSCRIPTEN) || defined(CORRADE_TARGET_APPLE)
+    #if ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE) || defined(CORRADE_TARGET_EMSCRIPTEN) || defined(CORRADE_TARGET_APPLE) || defined(BSD)
     char string[256];
     CORRADE_INTERNAL_ASSERT_OUTPUT(strerror_r(error, string, Containers::arraySize(string)) == 0);
     #else

Thanks in advance for testing this patch!

yurivict commented 1 year ago

The patch doesn't work, but it works when defined (__FreeBSD__) is used.

I just verified - no generic BSD define is present in the C++ compiler on FreeBSD:

$ c++ -dM -E - < /usr/include/time.h | grep -i BSD
#define __BSD_VISIBLE 1
#define __FBSDID(s) __IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
#define __FreeBSD__ 13
#define __FreeBSD_cc_version 1300010
#define __VERSION__ "FreeBSD Clang 14.0.5 (https://github.com/llvm/llvm-project.git llvmorg-14.0.5-0-gc12386ae247c)"

Only the __FreeBSD__ is usable. Likewise, __OpenBSD__ and __NetBSD__ are defined on those systems.

mosra commented 1 year ago

Ah, that's a bit sad. I added all three in 8effd9c49dd2a1c0fdc9dc814cc2aefa0867421c.

Thanks for reporting this, and thanks for maintaining the Corrade/Magnum packages as well :)