Skip to content

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

Description

@kou

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions