gachi-mandoo-shell / kimchi-mandoo-shell

a simple shell - minishell 42Seoul-@innovationacademy-kr (jaeskim, yjung)
0 stars 2 forks source link

[BUG] pipe leaks error! #77

Closed JaeSeoKim closed 3 years ago

JaeSeoKim commented 3 years ago

Expected Behavior

cat readme.md | ./minishell; leaks minishell

와 같은 명령어를 실행 할 경우 Inappropriate ioctl for device 가 출력이 되고 leaks에서 leaks가 발생하지 않아야 함.

Current Behavior

위 명령어 기준으로 아래와 같이 leaks이 발생함

Process:         minishell [28448]
Path:            /Volumes/VOLUME/*/minishell
Load Address:    0x1036be000
Identifier:      minishell
Version:         ???
Code Type:       X86-64
Parent Process:  zsh [27220]

Date/Time:       2021-05-19 01:00:19.083 +0900
Launch Time:     2021-05-19 00:59:50.509 +0900
OS Version:      Mac OS X 10.15.7 (19H1030)
Report Version:  7
Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
Analysis Tool Version:  Xcode 12.3 (12C33)

Physical footprint:         404K
Physical footprint (peak):  404K
----

leaks Report Version: 4.0
Process 28448: 263 nodes malloced for 23 KB
Process 28448: 6 leaks for 240 total leaked bytes.

    6 (240 bytes) << TOTAL >>

      2 (96 bytes) ROOT LEAK: 0x7fafe3604130 [16]
         1 (80 bytes) 0x7fafe3604220 [80]  length: 9  "readme.md"

      2 (96 bytes) ROOT LEAK: 0x7fafe40040a0 [16]  length: 2  " B"
         1 (80 bytes) 0x7fafe4004220 [80]  length: 9  "minishell"

      2 (48 bytes) ROOT LEAK: 0x7fafe35040c0 [16]
         1 (32 bytes) 0x7fafe3504190 [32]

Possible Solution

// src/exec/fork.c
int ft_cmd_set(t_cmd *cmds, t_check *g)
{
    pid_t   pid;
    int     status;

    status = 0;
    pid = fork();
    if (pid < 0)
        ft_error_print("fail fork", strerror(errno));
    if (pid == 0)
    {
        ft_pipe_connect(&status, g);
        if (status < 0)
            return (status);
        status = ft_redir_connect(g, 0);
        if (status < 0)
            return (status);
        status = ft_make_cmd(cmds->cmd, cmds->args);
        exit(status);
    }
    waitpid(pid, &status, WUNTRACED);
    g_sh.status += (status >> 8) & 0xff;
    ft_redir_close(g);
    ft_pipe_close(g);
    return (g_sh.status);
}

파일과 같이 ft_pipe_connect 함수 호출 후 status가 0 보다 작은 경우 ft_pipe_close 도 동일하게 호출하고 반환하도록 수정 해야 함.

Steps to Reproduce

  1. minishell 안에서 cat readme.md | ./minishell; leaks minishell 명령어 실행
jyw2671 commented 3 years ago
minishell>cat readme.md | ./minishell; leaks minishell
Inappropriate ioctl for deviceProcess:         minishell [83429]
Path:            /Volumes/VOLUME/*/minishell
Load Address:    0x10bef0000
Identifier:      minishell
Version:         ???
Code Type:       X86-64
Parent Process:  zsh [78112]

Date/Time:       2021-05-21 13:00:46.233 +0900
Launch Time:     2021-05-21 13:00:37.021 +0900
OS Version:      Mac OS X 10.15.7 (19H1030)
Report Version:  7
Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
Analysis Tool Version:  Xcode 12.3 (12C33)

Physical footprint:         352K
Physical footprint (peak):  352K
----

leaks Report Version: 4.0
Process 83429: 251 nodes malloced for 18 KB
Process 83429: 0 leaks for 0 total leaked bytes.

해결