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

MSVC: can't use malloc() in string.c with WIN32_LEAN_AND_MEAN #6277

Closed
kou opened this issue May 29, 2024 · 3 comments
Closed

MSVC: can't use malloc() in string.c with WIN32_LEAN_AND_MEAN #6277

kou opened this issue May 29, 2024 · 3 comments

Comments

@kou
Copy link
Contributor

kou commented May 29, 2024

https://github.com/kou/mruby/actions/runs/9278121120/job/25528601313#step:4:75

string.c
D:\a\mruby\mruby\src\string.c(859): error C4013: 'malloc' undefined; assuming extern returning int
D:\a\mruby\mruby\src\string.c(859): warning C4312: 'type cast': conversion from 'int' to 'wchar_t *' of greater size
D:\a\mruby\mruby\src\string.c(866): warning C4312: 'type cast': conversion from 'int' to 'char *' of greater size
D:\a\mruby\mruby\src\string.c(868): error C4013: 'free' undefined; assuming extern returning int
D:\a\mruby\mruby\src\string.c(889): warning C4312: 'type cast': conversion from 'int' to 'wchar_t *' of greater size
D:\a\mruby\mruby\src\string.c(895): warning C4312: 'type cast': conversion from 'int' to 'char *' of greater size

If WIN32_LEAN_AND_MEAN is defined, windows.h doesn't include additional headers.
See also: https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers#faster-builds-with-smaller-header-files

It seems that stdlib.h and malloc.h are also excluded with WIN32_LEAN_AND_MEAN. They are required to use malloc().
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/malloc?view=msvc-170#requirements

Routine Required header
malloc <stdlib.h> and <malloc.h>

We can choose either of the following solutions:

  1. Include <stdlib.h> and <malloc.h> in src/string.c
  2. Use mrb_malloc()/mrb_free() instead of raw malloc()/free() for mrb_locale_from_utf8()/mrb_utf8_from_locale()

1. is easy because we can just add 2 lines. But 2. may be better to use our allocation functions in our code base. But 2. breaks backward compatibility.

kou added a commit to kou/mruby that referenced this issue May 29, 2024
…m_utf8(); fix mruby#6277

We need to include stdlib.h and malloc.h to use malloc()/free() but
they aren't included in src/string.c with WIN32_LEAN_AND_MEAN. It
generates build time warnings.

We can solve this by including stdlib.h and malloc.h explicitly or use
mrb_malloc()/mrb_free() instead of malloc()/free(). This change uses
the latter to use our allocation functions.

But this breaks backward compatibility.
@kou
Copy link
Contributor Author

kou commented May 29, 2024

#6278 uses the 2. approach.

@kou
Copy link
Contributor Author

kou commented May 29, 2024

#6279 uses the 1. approach.

@matz matz closed this as completed in c258ead May 29, 2024
matz added a commit that referenced this issue May 29, 2024
Include headers for malloc() explicitly; fix #6277
@matz
Copy link
Member

matz commented May 29, 2024

I chose #6279

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

No branches or pull requests

2 participants