Yoshiki-Iwasa / minishell

0 stars 0 forks source link

Conditional jump or move depends on uninitialised value(s) #80

Closed hiroin closed 4 years ago

hiroin commented 4 years ago

[事案] valgrind --leak-check=full --show-leak-kinds=all ./minishell でminishell起動後、exitすると以下のメッセージがでる。

minishell$ exit
exit
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10E137: pattern_pipe_not_exit (pattern_pipe_not_exist.c:28)
==4951==    by 0x10BFAD: shell_execute (shell_execution.c:27)
==4951==    by 0x10C0DD: exec_each_command (shell_execution.c:69)
==4951==    by 0x10DC27: launch_shell (launch_shell.c:67)
==4951==    by 0x10DCC3: commnad_loop (launch_shell.c:87)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10E140: pattern_pipe_not_exit (pattern_pipe_not_exist.c:30)
==4951==    by 0x10BFAD: shell_execute (shell_execution.c:27)
==4951==    by 0x10C0DD: exec_each_command (shell_execution.c:69)
==4951==    by 0x10DC27: launch_shell (launch_shell.c:67)
==4951==    by 0x10DCC3: commnad_loop (launch_shell.c:87)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10C0E5: exec_each_command (shell_execution.c:70)
==4951==    by 0x10DC27: launch_shell (launch_shell.c:67)
==4951==    by 0x10DCC3: commnad_loop (launch_shell.c:87)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10C0EE: exec_each_command (shell_execution.c:70)
==4951==    by 0x10DC27: launch_shell (launch_shell.c:67)
==4951==    by 0x10DCC3: commnad_loop (launch_shell.c:87)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10DCCC: commnad_loop (launch_shell.c:82)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10DCD6: commnad_loop (launch_shell.c:82)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Conditional jump or move depends on uninitialised value(s)
==4951==    at 0x10DCE0: commnad_loop (launch_shell.c:89)
==4951==    by 0x10B1D2: shell_start (main.c:50)
==4951==    by 0x10B2E6: main (main.c:77)
==4951== 
==4951== Syscall param exit_group(status) contains uninitialised byte(s)
==4951==    at 0x4F20E66: _Exit (_exit.c:31)
==4951==    by 0x4E7F1C1: __run_exit_handlers (exit.c:132)
==4951==    by 0x4E7F1E9: exit (exit.c:139)
==4951==    by 0x4E5DB9D: (below main) (libc-start.c:344)
==4951== 
==4951== 
==4951== HEAP SUMMARY:
==4951==     in use at exit: 0 bytes in 0 blocks
==4951==   total heap usage: 113 allocs, 113 frees, 13,679 bytes allocated
==4951== 
==4951== All heap blocks were freed -- no leaks are possible
==4951== 
==4951== For counts of detected and suppressed errors, rerun with: -v
==4951== Use --track-origins=yes to see where uninitialised values come from
hiroin commented 4 years ago

ft_atoi.cを以下のように修正してみてください。

static int      answer(char *num, long k, long m)
{
    int ans;
    int i;

    i = 0;
    ans = 0;
    //while (k >= 0)
    while (k > 0)
    {
        //ans = ans + ((num[i] - 48) * ft_iterative_power(10, k - 1));
        ans = ans * 10 + (num[i] - 48);
        k--;
        i++;
    }
    if (m == -1)
        return (ans * -1);
    else
        return (ans);
}

Conditional jump or move depends on uninitialised value(s)の意味がいまいちわからないので、なんでこのエラーが上記の修正でなくなるのかわからないので、もっとソースを簡単にして、teratailで聞くか、wakeさん、rnittaさんに解析に出したいですね(^^:

自分の考えを書きます。 main関数でreturnする値は「確定(値が入っている)」したものでないとNGのようです。  → Syscall param exit_group(status) contains uninitialised byte(s) 「確定」とは、例えばrv = 1000だったら、確定。rv = no_pipe(args, vals);だったら、この時点では未確定で、no_pipeの中にとびます。 これをずっとおっていくと…、ft_atoi.cの、 ft_iterative_power関数にたどり着きます。 ft_iterative_powerは再帰関数なので、いつまでも未確定になっちゃいます(もちろん、関数の中に終了条件が書いてあるので、実際は終了するのですが、パーサーはとこまで汲み取ってくれないのだと思います。)。 以上から、このエラーは無視してよいと結論づけますが、レビューのときに説明がめんどいし、自分がチェックするときに邪魔なので、ft_atoi.cを改造してしまうのがいいかなと思います。

Yoshiki-Iwasa commented 4 years ago

修正しました!