Skip to content
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

Building without _DEFAULT_SOURCE or _XOPEN_SOURCE >= 500 and without -Werror=implicit-function-declaration ends up in a segfault #33

Closed
melroy89 opened this issue Jan 21, 2022 · 12 comments
Labels

Comments

@melroy89
Copy link

When using the following C standard setting in CMake, the build give warnings. But worse my executable will now segfault:

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_C_EXTENSIONS NO)

Build output:

[build] ../lib/whereami/whereami.c: In function ‘wai_getExecutablePath’:
[build] ../lib/whereami/whereami.c:197:16: warning: implicit declaration of function ‘realpath’ [-Wimplicit-function-declaration]
[build]   197 |     resolved = realpath(WAI_PROC_SELF_EXE, buffer);
[build]       |                ^~~~~~~~
[build] ../lib/whereami/whereami.c:197:14: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
[build]   197 |     resolved = realpath(WAI_PROC_SELF_EXE, buffer);
[build]       |              ^
[build] ../lib/whereami/whereami.c: In function ‘wai_getModulePath’:
[build] ../lib/whereami/whereami.c:276:20: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
[build]   276 |           resolved = realpath(path, buffer);
[build]       |                    ^

Run-time crash / segfault at line 201 in whereami.c:

    length = (int)strlen(resolved);
@gpakosz
Copy link
Owner

gpakosz commented Jan 22, 2022

Hello @Danger89 👋

The warning happening at line 197, I assume you're under Linux.

As per man 3 realpath, realpath requires _XOPEN_SOURCE >= 500.

Please make sure you define e.g. -D_XOPEN_SOURCE=500 when building with CMake.

@melroy89
Copy link
Author

Hi oke let me test that. But I think this won't solve the second warning, which most likely is the root cause of the segfault: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast.

Yes, I'm under Linux. I use your library for my project that runs under Linux as well as een cross build towards Windows (mingw).

@gpakosz
Copy link
Owner

gpakosz commented Jan 22, 2022

But I think this won't solve the second warning

There won't be a warning at all if you use -D_XOPEN_SOURCE=500.

Without -D_XOPEN_SOURCE=500, the declaration of realpath() will be skipped when including stdlib.h.
From there, for historical reasons, the compiler assumes realpth() has the following definition

int realpath()

or

int realpath(int arg1, int arg2, int arg3, ...)

And this is what causes the warning and the segfault.

You should really compile with -Werror which will prevent your compiler from generating code that segfaults.

@melroy89
Copy link
Author

melroy89 commented Jan 22, 2022

Thanks for your advice. I added those -Werror flags to the CMAKE_CXX_FLAGS, but you're right. I need to do the same for C code that I use in my project.

Anyway, for the CMake users out there, this is how you add the definition using cmake to a specific target:

target_compile_definitions(${TARGET_NAME} PUBLIC _XOPEN_SOURCE=500)

EDIT: This did fixed my problem btw! 🥳

@melroy89
Copy link
Author

Maybe you want to update the README.md with a small note regarding the _XOPEN_SOURCE=500 define?

@gpakosz
Copy link
Owner

gpakosz commented Jan 22, 2022

I'll think about how to document these requirements for all platforms supported

@gpakosz
Copy link
Owner

gpakosz commented Feb 13, 2022

Can you please try the gh-33-34 branch and confirm it builds without defining _XOPEN_SOURCE=500 from CMake?

@melroy89
Copy link
Author

melroy89 commented Feb 16, 2022

Maybe remove your "invalid" label to this issue.

Computer says no:

[build] /bin/x86_64-linux-gnu-gcc-9  -isystem ../lib/whereami -Wall -Werror -pedantic -Werror=incompatible-pointer-types -g  -Wall   -std=c99 -MD -MT lib/whereami/CMakeFiles/whereami.dir/whereami.c.o -MF lib/whereami/CMakeFiles/whereami.dir/whereami.c.o.d -o lib/whereami/CMakeFiles/whereami.dir/whereami.c.o   -c ../lib/whereami/whereami.c
[build] In file included from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
[build]                  from /usr/include/stdlib.h:25,
[build]                  from ../lib/whereami/whereami.c:25:
[build] /usr/include/features.h:187:3: error: #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE" [-Werror=cpp]
[build]   187 | # warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
[build]       |   ^~~~~~~
[build] cc1: all warnings being treated as errors
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

(Native Linux build)

@gpakosz EDIT: Changing your code to the following, solves the issue above

#undef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE

melroy89 added a commit to LibreWeb/LibreWeb-Browser that referenced this issue Feb 16, 2022
@gpakosz
Copy link
Owner

gpakosz commented Feb 16, 2022

The invalid label corresponds to the fact that you attributed your compilation error to something the C standard defines. Which is not the case.

I force pushed the gh-33-34 branch to replace _BSD_SOURCE by _DEFAULT_SOURCE.

I'll merge soon if it works for you.

@melroy89
Copy link
Author

melroy89 commented Feb 16, 2022

Thanks. READY WHEN YOU ARE.

@melroy89
Copy link
Author

LGTM. You can merge 👍🏽

@melroy89
Copy link
Author

The invalid label corresponds to the fact that you attributed your compilation error to something the C standard defines. Which is not the case.

I force pushed the gh-33-34 branch to replace _BSD_SOURCE by _DEFAULT_SOURCE.

I'll merge soon if it works for you.

it works 🚀

@gpakosz gpakosz changed the title Can't use standard C settings with the build Building without _DEFAULT_SOURCE or _XOPEN_SOURCE >= 500 and without -Werror=implicit-function-declaration ends up in a segfault Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants