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

Added explicit casts to malloc usages in order to fix compilation on C++ #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GlitchedPolygons
Copy link

When compiling this Argon2 lib in a C++ project, the compilation fails due to illegal conversions from void* (what malloc(size_t) returns) to uint8_t* - this is perfectly valid in C (and don't get me wrong, I love C just as much as everyone else) but it's a non-ignorable error in C++.

Since you put

#if defined(__cplusplus)
extern "C" {
#endif

inside argon2.h I thought I'd open this pull request, because it makes no sense to hint C++ friendliness with that extern "C" if compilation then fails in C++ due to implicit pointer type conversions.

My idea here would be to

  • Either accept this pull request and merge full C++ compatibility into Argon2 lib with minimal changes
  • Or reject this pull request but then also remove the #ifdef _cplusplus block at the top of the header file to avoid confusion for C++ devs interested in using this library

Kindest regards, and have a happy and successful new year! :D

@Hello71
Copy link

Hello71 commented Jan 28, 2022

although I am not argon2 author, extern "C" is fully consistent and in fact mandatory. argon2 is a C library that can be used from C++. it is not a C++ library. almost all C++ implementations except the notoriously deficient Microsoft implementation can be configured to compile certain files in C compliant mode, and even the Microsoft implementation now has official C compliant mode. extern "C" means that the referenced functions are written in C or more precisely use the C ABI, but can be seamlessly invoked from C++.

@josephlr
Copy link
Contributor

josephlr commented Jan 30, 2022

@Hello71 is correct. The #if defined(__cplusplus) stuff means that the headers for this library are C++ compatible. The fact that the implementation files for this library end in .c indicate that the library itself should be built with a C compiler not a C++ compiler.

I think this PR can be closed unless we think that adding the explicit casts is good C style. C++ compatibility for the .c files should be a non-goal in my opinion.

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

Successfully merging this pull request may close these issues.

3 participants