簡體   English   中英

使用 strtok() 時 C 編程中的分段錯誤

[英]Segmentation Fault in C programming when using strtok()

我正在編寫一個簡單的 shell 並且命令 (ls & whoami;) 有效,但是當我僅鍵入命令 ls 時,代碼由於第 51 行的分段錯誤而中斷。我想知道如何 go 關於這個我使用了 Valgrind 和大量的 printf 語句,但似乎沒有任何效果。 讓我知道你的想法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>

#define MAX_LINE 80
#define MAX_CHAR 100

int main()
{
    int should_run = 1;
    char line[MAX_CHAR];
    char *args[MAX_LINE/2 +1];
    int ampersandFlag = 0;


    /*---------------------------------------------------*/
    /*                Start of The Program               */

    while(should_run){
        printf("osh>");
        fgets(line,MAX_CHAR,stdin);

        /* get rid of newline char */
        int k = 0;
        while(line[k] != '\n'){
            k++;
        }
        line[k] = '\0';

        /* get rid of semicolon char */
        int l = 0;
        while(line[l] != ';'){
            l++;
        }
        line[l] = '\0';


        int i = 0;
        args[i] = strtok(line, " ");

        if(args[i] == NULL){
            return 1;
        }

        while(args[i] != NULL){
            printf("%s\n", args[i]);
            i++;
            args[i] = strtok(NULL, " ");
        }

        /* Handles the exit command.  */
        int a = 0;
        if(strcmp(args[a], "exit") == 0){
            exit(0);
        }
        if(strcmp(args[1], "&") == 0){
            args[1] = NULL;
            ampersandFlag = 1;
        }

        /*---------------------------------------------------*/
        /*                     Start Forking                 */

        int returnForkValue = fork();
        int status;

        if(returnForkValue < 0){
            perror("Fork Failed");
        }

        else if(returnForkValue == 0){
            printf("This is the child\n");
            if(ampersandFlag == 1){
                int stat;
                int retFork = fork();

                if(retFork == 0){
                    printf("This is second child");
                    execvp(args[2],args);
                }
                else{
                    printf("This is the second parent");
                    waitpid(retFork, &stat, 0);
                }
            }
            execvp(args[0],args);
            printf("This won't be printed if execvp is successul\n");
        }

        else{
            printf("This is the parent\n");
            waitpid(returnForkValue, &status, 0);
        }
    }
    return 0;
}

如果您只有一個標記,則只有args[0]將指向一個有效的字符串。 下一個args[1]將是NULL指針。 args的所有其他元素都將未初始化。

在這種情況下,行

        if(strcmp(args[1], "&") == 0){

將嘗試使用NULL指針作為字符串。

循環之后

while(args[i] != NULL){

變量i將包含令牌的數量。

您應該只使用 index values < i來訪問args

執行第二個命令的代碼是錯誤的。 代替

                    execvp(args[2],args);

你必須使用

                    execvp(args[2],&args[2]);

但這僅在令牌數至少為 3 時才有效。

順便說一句:您的代碼有點不一致。 要檢查"exit" ,您使用變量a作為args的索引,對於其他比較,您使用硬編碼數字。

當我使用valgrind運行程序並輸入ls時,我收到一條錯誤消息,顯示NULL指針訪問。

==30002== Invalid read of size 1
==30002==    at 0x483EED4: strcmp (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==30002==    by 0x10943A: main (test.c:58)
==30002==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

我收到一條額外的錯誤消息

==30002== Conditional jump or move depends on uninitialised value(s)
==30002==    at 0x10933C: main (test.c:34)

發生這種情況是因為您的循環

while(line[l] != ';'){

不檢查終止NUL ( '\0' ) 字符,如果它不包含分號,它將讀取字符串的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM