biralavor / 42_minishell

minishell, a 42 school team project to rebuild a tiny bash, using C language. Build with @Thais-Malheiros
MIT License
3 stars 0 forks source link

fix sigfault when heredoc with cat and ctrl d #553

Closed biralavor closed 3 weeks ago

biralavor commented 3 weeks ago

the command was:

<< eof cat

Result: Seg Fault

<<< Born Again (mini) SHell >>>$ << eof cat
(mini)heredoc<< sd
(mini)heredoc<< f
(mini)heredoc<< dsf
(mini)heredoc<< ds
(mini)heredoc<< 
 here-document at line 5 delimited by end-of-file (wanted `eof')
==297304== Invalid read of size 4
==297304==    at 0x111B81: convert_envs_to_array (51.command_runner_utils.c:25)
==297304==    by 0x111963: fork_and_execve (50.command_runner.c:20)
==297304==    by 0x111B51: command_runner (50.command_runner.c:71)
==297304==    by 0x10E666: preprocessing_for_command_runner (29.manage_single_cmd.c:38)
==297304==    by 0x10E617: manage_single_command (29.manage_single_cmd.c:27)
==297304==    by 0x10D960: tree_execution (23.tree_execution.c:31)
==297304==    by 0x10E2EA: finalize_redirect (27.manage_redirects.c:114)
==297304==    by 0x10DFA8: manage_redirect (27.manage_redirects.c:39)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304==  Address 0x10 is not stack'd, malloc'd or (recently) free'd
==297304== 
==297304== 
==297304== Process terminating with default action of signal 11 (SIGSEGV)
==297304==  Access not within mapped region at address 0x10
==297304==    at 0x111B81: convert_envs_to_array (51.command_runner_utils.c:25)
==297304==    by 0x111963: fork_and_execve (50.command_runner.c:20)
==297304==    by 0x111B51: command_runner (50.command_runner.c:71)
==297304==    by 0x10E666: preprocessing_for_command_runner (29.manage_single_cmd.c:38)
==297304==    by 0x10E617: manage_single_command (29.manage_single_cmd.c:27)
==297304==    by 0x10D960: tree_execution (23.tree_execution.c:31)
==297304==    by 0x10E2EA: finalize_redirect (27.manage_redirects.c:114)
==297304==    by 0x10DFA8: manage_redirect (27.manage_redirects.c:39)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304==  If you believe this happened as a result of a stack
==297304==  overflow in your program's main thread (unlikely but
==297304==  possible), you can try to increase the size of the
==297304==  main thread stack using the --main-stacksize= flag.
==297304==  The main thread stack size used in this run was 8388608.
==297304== 
==297304== FILE DESCRIPTORS: 7 open (3 std) at exit.
==297304== Open file descriptor 4: /dev/pts/8
==297304==    at 0x49CCFEB: dup (syscall-template.S:120)
==297304==    by 0x10DF15: manage_redirect (27.manage_redirects.c:29)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== Open file descriptor 3: /dev/pts/8
==297304==    at 0x49CCFEB: dup (syscall-template.S:120)
==297304==    by 0x10DF08: manage_redirect (27.manage_redirects.c:28)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== Open file descriptor 22: /home/biralavor/.vscode-server/data/logs/20241010T061121/remoteagent.log
==297304==    <inherited from parent>
==297304== 
==297304== Open file descriptor 19: /home/biralavor/.vscode-server/data/logs/20241010T061121/ptyhost.log
==297304==    <inherited from parent>
==297304== 
==297304== 
==297304== HEAP SUMMARY:
==297304==     in use at exit: 215,479 bytes in 259 blocks
==297304==   total heap usage: 718 allocs, 459 frees, 247,241 bytes allocated
==297304== 
==297304== 1 bytes in 1 blocks are still reachable in loss record 1 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112858: ft_strdup (ft_strdup.c:30)
==297304==    by 0x111CA6: testing_all_paths_with_cmd (51.command_runner_utils.c:54)
==297304==    by 0x111AE8: lookup_cmd_path (50.command_runner.c:55)
==297304==    by 0x111B33: command_runner (50.command_runner.c:68)
==297304==    by 0x10E666: preprocessing_for_command_runner (29.manage_single_cmd.c:38)
==297304==    by 0x10E617: manage_single_command (29.manage_single_cmd.c:27)
==297304==    by 0x10D960: tree_execution (23.tree_execution.c:31)
==297304==    by 0x10E2EA: finalize_redirect (27.manage_redirects.c:114)
==297304==    by 0x10DFA8: manage_redirect (27.manage_redirects.c:39)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304== 
==297304== 4 bytes in 1 blocks are still reachable in loss record 2 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112BDD: ft_substr (ft_substr.c:30)
==297304==    by 0x10BAC3: state_90 (11.state61_to_state90.c:95)
==297304==    by 0x10B2BA: get_state (08.get_state.c:36)
==297304==    by 0x10ADF7: create_token_list (07.token_list_functions.c:22)
==297304==    by 0x10A99C: loop_routine (05.loop_routine.c:54)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 4 bytes in 1 blocks are still reachable in loss record 3 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112858: ft_strdup (ft_strdup.c:30)
==297304==    by 0x10E748: convert_tokens_to_array (29.manage_single_cmd.c:64)
==297304==    by 0x10E640: preprocessing_for_command_runner (29.manage_single_cmd.c:35)
==297304==    by 0x10E617: manage_single_command (29.manage_single_cmd.c:27)
==297304==    by 0x10D960: tree_execution (23.tree_execution.c:31)
==297304==    by 0x10E2EA: finalize_redirect (27.manage_redirects.c:114)
==297304==    by 0x10DFA8: manage_redirect (27.manage_redirects.c:39)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 15 bytes in 1 blocks are still reachable in loss record 16 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112858: ft_strdup (ft_strdup.c:30)
==297304==    by 0x10CD4A: path_file (20.manage_heredoc.c:68)
==297304==    by 0x10CBAD: manage_heredoc (20.manage_heredoc.c:31)
==297304==    by 0x10A9EA: loop_routine (05.loop_routine.c:59)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 16 bytes in 1 blocks are still reachable in loss record 19 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112212: ft_calloc (ft_calloc.c:34)
==297304==    by 0x10E70F: convert_tokens_to_array (29.manage_single_cmd.c:60)
==297304==    by 0x10E640: preprocessing_for_command_runner (29.manage_single_cmd.c:35)
==297304==    by 0x10E617: manage_single_command (29.manage_single_cmd.c:27)
==297304==    by 0x10D960: tree_execution (23.tree_execution.c:31)
==297304==    by 0x10E2EA: finalize_redirect (27.manage_redirects.c:114)
==297304==    by 0x10DFA8: manage_redirect (27.manage_redirects.c:39)
==297304==    by 0x10D919: tree_execution (23.tree_execution.c:26)
==297304==    by 0x10AA64: loop_routine (05.loop_routine.c:69)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 32 bytes in 1 blocks are still reachable in loss record 30 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112212: ft_calloc (ft_calloc.c:34)
==297304==    by 0x10D611: build_tree_recursive (21.building_tree.c:88)
==297304==    by 0x10D6A8: initiate_tree (21.building_tree.c:101)
==297304==    by 0x10AA3C: loop_routine (05.loop_routine.c:67)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 32 bytes in 1 blocks are still reachable in loss record 31 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112212: ft_calloc (ft_calloc.c:34)
==297304==    by 0x10D611: build_tree_recursive (21.building_tree.c:88)
==297304==    by 0x10D3E0: get_splited (21.building_tree.c:31)
==297304==    by 0x10D672: build_tree_recursive (21.building_tree.c:93)
==297304==    by 0x10D6A8: initiate_tree (21.building_tree.c:101)
==297304==    by 0x10AA3C: loop_routine (05.loop_routine.c:67)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 32 bytes in 1 blocks are still reachable in loss record 32 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112212: ft_calloc (ft_calloc.c:34)
==297304==    by 0x10D611: build_tree_recursive (21.building_tree.c:88)
==297304==    by 0x10D3F4: get_splited (21.building_tree.c:32)
==297304==    by 0x10D672: build_tree_recursive (21.building_tree.c:93)
==297304==    by 0x10D6A8: initiate_tree (21.building_tree.c:101)
==297304==    by 0x10AA3C: loop_routine (05.loop_routine.c:67)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== 80 bytes in 2 blocks are still reachable in loss record 40 of 97
==297304==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==297304==    by 0x112212: ft_calloc (ft_calloc.c:34)
==297304==    by 0x10B9AF: state_90 (11.state61_to_state90.c:85)
==297304==    by 0x10B2BA: get_state (08.get_state.c:36)
==297304==    by 0x10ADF7: create_token_list (07.token_list_functions.c:22)
==297304==    by 0x10A99C: loop_routine (05.loop_routine.c:54)
==297304==    by 0x109EB0: main (01.main.c:33)
==297304== 
==297304== LEAK SUMMARY:
==297304==    definitely lost: 0 bytes in 0 blocks
==297304==    indirectly lost: 0 bytes in 0 blocks
==297304==      possibly lost: 0 bytes in 0 blocks
==297304==    still reachable: 216 bytes in 10 blocks
==297304==         suppressed: 215,263 bytes in 249 blocks
==297304== 
==297304== For lists of detected and suppressed errors, rerun with: -s
==297304== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
make: *** [Makefile:281: val] Segmentation fault (core dumped)