williamroocha / minishell

0 stars 0 forks source link

Builtins bug fixing & repeated calls of different type of command bug fixing (Builtin vs executable bug fixing) #10

Closed JOAOVIEG closed 8 months ago

JOAOVIEG commented 8 months ago

At the builtins and executables there is a bug: the sequence of tested commands are:

minishell>cd
minishell>pwd
/home/jpcvieg
minishell>cd (space is ok here)
minishell>cd Desktop/
minishell>pwd
/home/jpcvieg/Desktop
minishell>cd 
minishell>ls
 OK
minishell>cd
==7855== Invalid read of size 8
==7855==    at 0x10ACE5: get_home_var (cd.c:43)
==7855==    by 0x10AC11: cd (cd.c:25)
==7855==    by 0x10B93D: exec_builtin (ft_execute.c:50)
==7855==    by 0x10B7F1: ft_simple_cmds (ft_execute.c:22)
==7855==    by 0x10C1AE: ft_execute (ft_execute.c:288)
==7855==    by 0x1097D2: main (minishell.c:65)
==7855==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==7855== 
==7855== 
==7855== Process terminating with default action of signal 11 (SIGSEGV)
==7855==  Access not within mapped region at address 0x8
==7855==    at 0x10ACE5: get_home_var (cd.c:43)
==7855==    by 0x10AC11: cd (cd.c:25)
==7855==    by 0x10B93D: exec_builtin (ft_execute.c:50)
==7855==    by 0x10B7F1: ft_simple_cmds (ft_execute.c:22)
==7855==    by 0x10C1AE: ft_execute (ft_execute.c:288)
==7855==    by 0x1097D2: main (minishell.c:65)
==7855==  If you believe this happened as a result of a stack
==7855==  overflow in your program's main thread (unlikely but
==7855==  possible), you can try to increase the size of the
==7855==  main thread stack using the --main-stacksize= flag.
==7855==  The main thread stack size used in this run was 8388608.
==7855== 
==7855== HEAP SUMMARY:
==7855==     in use at exit: 214,821 bytes in 281 blocks
==7855==   total heap usage: 969 allocs, 688 frees, 319,894 bytes allocated
==7855== 
==7855== LEAK SUMMARY:
==7855==    definitely lost: 0 bytes in 0 blocks
==7855==    indirectly lost: 0 bytes in 0 blocks
==7855==      possibly lost: 0 bytes in 0 blocks
==7855==    still reachable: 214,821 bytes in 281 blocks
==7855==         suppressed: 0 bytes in 0 blocks
==7855== Reachable blocks (those to which a pointer was found) are not shown.
==7855== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==7855== 
==7855== For lists of detected and suppressed errors, rerun with: -s
==7855== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: v] Segmentation fault (core dumped)

After calling ls, cd (and space after, aka PATH = " ") outputs seg fault.

More examples:

==8549== Command: ./minishell
==8549== 
minishell>ls
includes  Makefile  minishell  obj  readline.supp  src
minishell>cd
==8549== Invalid read of size 8
==8549==    at 0x10ACE5: get_home_var (cd.c:43)
==8549==    by 0x10AC11: cd (cd.c:25)
==8549==    by 0x10B93D: exec_builtin (ft_execute.c:50)
==8549==    by 0x10B7F1: ft_simple_cmds (ft_execute.c:22)
==8549==    by 0x10C1AE: ft_execute (ft_execute.c:288)
==8549==    by 0x1097D2: main (minishell.c:65)
==8549==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==8549== 
==8549== 
==8549== Process terminating with default action of signal 11 (SIGSEGV)
==8549==  Access not within mapped region at address 0x8
==8549==    at 0x10ACE5: get_home_var (cd.c:43)
==8549==    by 0x10AC11: cd (cd.c:25)
==8549==    by 0x10B93D: exec_builtin (ft_execute.c:50)
==8549==    by 0x10B7F1: ft_simple_cmds (ft_execute.c:22)
==8549==    by 0x10C1AE: ft_execute (ft_execute.c:288)
==8549==    by 0x1097D2: main (minishell.c:65)
==8549==  If you believe this happened as a result of a stack
==8549==  overflow in your program's main thread (unlikely but
==8549==  possible), you can try to increase the size of the
==8549==  main thread stack using the --main-stacksize= flag.
==8549==  The main thread stack size used in this run was 8388608.
==8549== 
==8549== HEAP SUMMARY:
==8549==     in use at exit: 214,593 bytes in 263 blocks
==8549==   total heap usage: 809 allocs, 546 frees, 243,757 bytes allocated
==8549== 
==8549== LEAK SUMMARY:
==8549==    definitely lost: 0 bytes in 0 blocks
==8549==    indirectly lost: 0 bytes in 0 blocks
==8549==      possibly lost: 0 bytes in 0 blocks
==8549==    still reachable: 214,593 bytes in 263 blocks
==8549==         suppressed: 0 bytes in 0 blocks
==8549== Reachable blocks (those to which a pointer was found) are not shown.
==8549== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==8549== 
==8549== For lists of detected and suppressed errors, rerun with: -s
==8549== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: v] Segmentation fault (core dumped)

It does not matter if cd is followed by PATH. It sends seg fault after callig executable ls.

The pattern of the bug seems to be after an executable is called: be it the builtins, be it another executable the program gives seg fault or does not work as intended:

==10560== Command: ./minishell
==10560== 
minishell>wc -l Makefile 
58 Makefile
minishell>echo Hello
Hello
minishell>env
minishell>cat Makefile 
ERROR env at path: Success
==10560== 
==10560== HEAP SUMMARY:
==10560==     in use at exit: 208,562 bytes in 244 blocks
==10560==   total heap usage: 903 allocs, 659 frees, 308,588 bytes allocated
==10560== 
==10560== LEAK SUMMARY:
==10560==    definitely lost: 0 bytes in 0 blocks
==10560==    indirectly lost: 0 bytes in 0 blocks
==10560==      possibly lost: 0 bytes in 0 blocks
==10560==    still reachable: 208,562 bytes in 244 blocks
==10560==         suppressed: 0 bytes in 0 blocks
==10560== Reachable blocks (those to which a pointer was found) are not shown.
==10560== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==10560== 
==10560== For lists of detected and suppressed errors, rerun with: -s
==10560== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: v] Error 1

And:

minishell>env
ENV IS NOT CORRECT TOO
minishell>ls
includes  Makefile  minishell  obj  readline.supp  src
minishell>env
minishell>wc -l Makefile 
ERROR env at path: Success
==11514== 
==11514== HEAP SUMMARY:
==11514==     in use at exit: 208,554 bytes in 245 blocks
==11514==   total heap usage: 873 allocs, 628 frees, 275,048 bytes allocated
==11514== 
==11514== LEAK SUMMARY:
==11514==    definitely lost: 0 bytes in 0 blocks
==11514==    indirectly lost: 0 bytes in 0 blocks
==11514==      possibly lost: 0 bytes in 0 blocks
==11514==    still reachable: 208,554 bytes in 245 blocks
==11514==         suppressed: 0 bytes in 0 blocks
==11514== Reachable blocks (those to which a pointer was found) are not shown.
==11514== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==11514== 
==11514== For lists of detected and suppressed errors, rerun with: -s
==11514== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: v] Error 1

In short, env is not properly copied: builtins functionalities need to be checked and corrected; executables and builtins are not working together: to see if these problems are related minding attached errors.

JOAOVIEG commented 8 months ago

On the env builtin command: the output is only different when we run the valgrind command. It is not an issue with the implementation.