win-fix-uv_spawn-ENOMEM-on-empty-env.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Ben Noordhuis <[email protected]>
  3. Date: Fri, 9 Aug 2019 13:34:20 +0200
  4. Subject: win: fix uv_spawn() ENOMEM on empty env
  5. Commit ba780231 ("unix,win: handle zero-sized allocations uniformly")
  6. makes `uv__malloc()` return NULL when `size == 0`.
  7. That's exactly the size that is passed to it when uv_spawn() tries to
  8. spawn a process with an empty environment so handle that edge case.
  9. Fixes: https://github.com/nodejs/node/issues/29008
  10. PR-URL: https://github.com/libuv/libuv/pull/2408
  11. Reviewed-By: Anna Henningsen <[email protected]>
  12. Reviewed-By: Colin Ihrig <[email protected]>
  13. Reviewed-By: Jameson Nash <[email protected]>
  14. Reviewed-By: Bartosz Sosnowski <[email protected]>
  15. diff --git a/deps/uv/src/win/process.c b/deps/uv/src/win/process.c
  16. index fa1a76a2e6626e15bdd0681626fc82c9ca6907fe..e1010d1248a95a3927d6ed1a1affbb545c1d7201 100644
  17. --- a/deps/uv/src/win/process.c
  18. +++ b/deps/uv/src/win/process.c
  19. @@ -714,7 +714,7 @@ int make_program_env(char* env_block[], WCHAR** dst_ptr) {
  20. /* second pass: copy to UTF-16 environment block */
  21. dst_copy = (WCHAR*)uv__malloc(env_len * sizeof(WCHAR));
  22. - if (!dst_copy) {
  23. + if (dst_copy == NULL && env_len > 0) {
  24. return ERROR_OUTOFMEMORY;
  25. }
  26. env_copy = alloca(env_block_count * sizeof(WCHAR*));
  27. @@ -739,7 +739,7 @@ int make_program_env(char* env_block[], WCHAR** dst_ptr) {
  28. }
  29. }
  30. *ptr_copy = NULL;
  31. - assert(env_len == (size_t) (ptr - dst_copy));
  32. + assert(env_len == 0 || env_len == (size_t) (ptr - dst_copy));
  33. /* sort our (UTF-16) copy */
  34. qsort(env_copy, env_block_count-1, sizeof(wchar_t*), qsort_wcscmp);
  35. diff --git a/deps/uv/test/test-list.h b/deps/uv/test/test-list.h
  36. index 29e9cbf85df3f07dd3a017ffca1cea0b56e76b89..10ec86869008c2cd15848e71476bd18fb2af3da1 100644
  37. --- a/deps/uv/test/test-list.h
  38. +++ b/deps/uv/test/test-list.h
  39. @@ -263,6 +263,7 @@ TEST_DECLARE (spawn_fails)
  40. #ifndef _WIN32
  41. TEST_DECLARE (spawn_fails_check_for_waitpid_cleanup)
  42. #endif
  43. +TEST_DECLARE (spawn_empty_env)
  44. TEST_DECLARE (spawn_exit_code)
  45. TEST_DECLARE (spawn_stdout)
  46. TEST_DECLARE (spawn_stdin)
  47. @@ -819,6 +820,7 @@ TASK_LIST_START
  48. #ifndef _WIN32
  49. TEST_ENTRY (spawn_fails_check_for_waitpid_cleanup)
  50. #endif
  51. + TEST_ENTRY (spawn_empty_env)
  52. TEST_ENTRY (spawn_exit_code)
  53. TEST_ENTRY (spawn_stdout)
  54. TEST_ENTRY (spawn_stdin)
  55. diff --git a/deps/uv/test/test-spawn.c b/deps/uv/test/test-spawn.c
  56. index fea1165d89e08e9e56257cded55048b09c27d1d0..fec610bfdef97eff778c28158c2360ef02cb08e5 100644
  57. --- a/deps/uv/test/test-spawn.c
  58. +++ b/deps/uv/test/test-spawn.c
  59. @@ -232,6 +232,34 @@ TEST_IMPL(spawn_fails_check_for_waitpid_cleanup) {
  60. #endif
  61. +TEST_IMPL(spawn_empty_env) {
  62. + char* env[1];
  63. +
  64. + /* The autotools dynamic library build requires the presence of
  65. + * DYLD_LIBARY_PATH (macOS) or LD_LIBRARY_PATH (other Unices)
  66. + * in the environment, but of course that doesn't work with
  67. + * the empty environment that we're testing here.
  68. + */
  69. + if (NULL != getenv("DYLD_LIBARY_PATH") ||
  70. + NULL != getenv("LD_LIBRARY_PATH")) {
  71. + RETURN_SKIP("doesn't work with DYLD_LIBRARY_PATH/LD_LIBRARY_PATH");
  72. + }
  73. +
  74. + init_process_options("spawn_helper1", exit_cb);
  75. + options.env = env;
  76. + env[0] = NULL;
  77. +
  78. + ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
  79. + ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  80. +
  81. + ASSERT(exit_cb_called == 1);
  82. + ASSERT(close_cb_called == 1);
  83. +
  84. + MAKE_VALGRIND_HAPPY();
  85. + return 0;
  86. +}
  87. +
  88. +
  89. TEST_IMPL(spawn_exit_code) {
  90. int r;