5858#include "nifs.h"
5959#include "posix_nifs.h"
6060
61+ #if HAVE_EXECVE
62+ extern char * * environ ;
63+ #endif
64+
6165term posix_errno_to_term (int err , GlobalContext * glb )
6266{
6367#if HAVE_OPEN && HAVE_CLOSE || defined(HAVE_CLOCK_SETTIME ) || defined(HAVE_SETTIMEOFDAY )
@@ -511,6 +515,9 @@ static term nif_atomvm_posix_select_stop(Context *ctx, int argc, term argv[])
511515#if HAVE_EXECVE
512516static void free_string_list (char * * list )
513517{
518+ if (IS_NULL_PTR (list )) {
519+ return ;
520+ }
514521 char * * ptr = list ;
515522 while (* ptr ) {
516523 char * str = * ptr ;
@@ -564,19 +571,26 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[])
564571 free (path );
565572 RAISE_ERROR (BADARG_ATOM );
566573 }
567- char * * envp = parse_string_list (argv [2 ]);
568- if (IS_NULL_PTR (envp )) {
569- free (path );
570- free_string_list (args );
571- RAISE_ERROR (BADARG_ATOM );
574+ char * * envp ;
575+ char * * envp_array = NULL ;
576+ if (argv [2 ] == UNDEFINED_ATOM ) {
577+ envp = environ ;
578+ } else {
579+ envp_array = parse_string_list (argv [2 ]);
580+ if (IS_NULL_PTR (envp_array )) {
581+ free (path );
582+ free_string_list (args );
583+ RAISE_ERROR (BADARG_ATOM );
584+ }
585+ envp = envp_array ;
572586 }
573587
574588 int pstdout [2 ];
575589 int r = pipe (pstdout );
576590 if (r < 0 ) {
577591 free (path );
578592 free_string_list (args );
579- free_string_list (envp );
593+ free_string_list (envp_array );
580594 return errno_to_error_tuple_maybe_gc (ctx );
581595 }
582596 pid_t pid ;
@@ -609,7 +623,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[])
609623 if (UNLIKELY (r != 0 )) {
610624 free (path );
611625 free_string_list (args );
612- free_string_list (envp );
626+ free_string_list (envp_array );
613627 close (pstdout [0 ]);
614628 close (pstdout [1 ]);
615629 return error_tuple_maybe_gc (r , ctx );
@@ -620,7 +634,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[])
620634 int err = errno ;
621635 free (path );
622636 free_string_list (args );
623- free_string_list (envp );
637+ free_string_list (envp_array );
624638 close (pstdout [0 ]);
625639 close (pstdout [1 ]);
626640 return error_tuple_maybe_gc (err , ctx );
@@ -646,7 +660,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[])
646660 close (pstdout [1 ]); // close write-end of the pipe
647661 free (path );
648662 free_string_list (args );
649- free_string_list (envp );
663+ free_string_list (envp_array );
650664
651665 if (UNLIKELY (memory_ensure_free_opt (ctx , TUPLE_SIZE (3 ) + TERM_BOXED_RESOURCE_SIZE , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
652666 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
0 commit comments