Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yurivict opened this issue Jan 15, 2023 · 3 comments

Comments

@yurivict
Copy link
Contributor

/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 mosra added this to the 2022.0a milestone Jan 16, 2023
@mosra
Copy link
Owner

mosra commented Jan 16, 2023

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

#if ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE) || defined(CORRADE_TARGET_EMSCRIPTEN) || defined(CORRADE_TARGET_APPLE)
char string[256];
CORRADE_INTERNAL_ASSERT_OUTPUT(strerror_r(error, string, Containers::arraySize(string)) == 0);
#else
char buffer[256];
const char* const string = strerror_r(error, buffer, Containers::arraySize(buffer));
#endif

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. 😅) 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
Copy link
Contributor Author

yurivict commented Jan 16, 2023

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
Copy link
Owner

mosra commented Jan 16, 2023

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

2 participants