简体   繁体   English

分段错误-自定义外壳

[英]Segmentation Fault - Custom Shell

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

#define BUF 1024        //I assume that the maximum number of arguments is 1024

main()
{
    char c;
    char *temp;
    char *arg[BUF];                 //the commands
    int i=1,j,k,iter=0;

    while(1)
    {
            i=1;
            iter=0;
            printf("CS21> ");
            temp = malloc(sizeof(char));
            while((c=fgetc(stdin))!='\n')
            {
                    temp = realloc(temp, i*sizeof(char));

                    temp[i-1]=c;
                    i++;
            }

            j=0;
            while(j<strlen(temp))
            {
                    if(temp[j]==' ')
                    {
                            j++;
                            continue;
                    }

                    if(temp[j]!=' ')  //Line 38: Same check performed as Line 42
                    {
                                    k=j;
                                    arg[iter] = malloc(sizeof(char));
                                    while(temp[k]!=' ')    //Line 42: Segmentation Fault here
                                    {
                                            arg[iter] = realloc(arg[iter],(k-j+1)*sizeof(char));
                                            arg[iter][k-j]=temp[k];
                                            k++;
                                    }
                                    iter++;
                                    k++;
                                    j=k;
                                    continue;
                    }
            }
    }
}

Hi, The above is a sample of code from my custom shell's code. 嗨,上面是我的自定义外壳程序代码中的代码示例。 I haven't completed the code yet, just in case you're wondering about the program going on till infinity. 我还没有完成代码,以防万一您想知道程序一直运行到无穷远。 Now, I am getting a segmentation fault at a line (its been commented), but I don't understand why. 现在,我在一行中遇到了分割错误(已被注释),但是我不明白为什么。 I perform the same check as Line 42 at Line 38, but it didn't give a segmentation fault there. 我在38行执行与42行相同的检查,但是在那里没有出现分段错误。 Can anyone help me out? 谁能帮我吗?

The purpose of some of the mentioned variables is as follows: "temp" is a pointer to a memory location that holds the entire command given to the shell. 某些提到的变量的用途如下:“ temp”是指向存储位置的指针,该位置保存了给外壳程序的整个命令。 "args" is an array of pointers, each pointer pointing to a memory location that contains the individual arguments in the command. “ args”是一个指针数组,每个指针指向一个包含命令中各个参数的内存位置。

For example, "temp" will hold the string - gcc hello.c -o hello, if that has been passed to my shell. 例如,“ temp”将保存字符串-gcc hello.c -o hello(如果已将其传递给我的shell)。 And args[0] will point to "gcc", args[1] will point to "hello.c" and so on. args [0]指向“ gcc”,args [1]指向“ hello.c”,依此类推。

That is the purpose of this sample of code. 这就是该代码示例的目的。 It will store all the arguments in "args" after eliminating the white spaces from "temp". 从“ temp”中删除空格后,它将所有参数存储在“ args”中。 The while(1) loop will exit when the person calls the exit command from the shell. 当人员从shell调用exit命令时,while(1)循环将退出。 But that part of it will be done separately. 但是,这部分将单独完成。 Now can anyone help me with this sample of code? 现在有人可以帮助我处理此代码示例吗?

Thanks! 谢谢!

You have loop in while(temp[k]!=' ') which doesn't finish, when there is no space in the string (case of last argument). 当字符串中没有空格(最后一个参数的情况)时,您将进入while(temp [k]!='')循环,但不会结束。 You need to stop the loop if k > strlen(temp). 如果k> strlen(temp),则需要停止循环。

Just my comment: Who the hell is teaching to read by bytes and realloc after each character? 只是我的评论:谁在教谁按字节读取并在每个字符后重新分配? This is awkward... 真尴尬

I think the following line: 我认为以下内容:

arg[iter] = malloc(sizeof(char));

can damage value of k if item is greater than BUF. 如果item大于BUF,可能会损坏k值。 Also it can damage value of temp if iter is negative. 如果iter为负,也会损坏temp值。 This is because k and temp are stored on stack somewhat near arg and writing arg elements beyond its size may actually overwrite variables stored nearby. 这是因为ktemp存储在堆栈中靠近arg并且写入超出其大小的arg元素实际上可能会覆盖附近存储的变量。

Try printing k and temp before and after the line mentioned above and see whether the values of them are damaged. 尝试在上述行的前后打印ktemp ,看它们的值是否损坏。

In the line 在行中

while((c=fgetc(stdin))!='\n')
{
        temp = realloc(temp, i*sizeof(char));

        temp[i-1]=c;
        i++;
}

you don't end the temp string with a null terminating character 您不要以空终止符结束临时字符串

and then later you go out of bounds of that array 然后,您超出了该数组的范围

 while(temp[k]!=' ')    //Line 42: Segmentation Fault here




Change the line 换线

 temp[i-1]=c; to  temp[i-1]='\n';

and

while(temp[k]!=' ') to while(temp[k]!='\0')

You need to allocate additional one char space for temp , for the special '\\0' char which indicates end of string. 您需要为temp分配一个额外的char空间,以用于特殊的'\\0' 0'char,它指示字符串的结尾。

while((c=fgetc(stdin))!='\n')
{
    temp = realloc(temp, i*sizeof(char) + 1);  //1 more char space for '\0'
    temp[i-1]=c;
    i++;
 }
 temp[i] = '\0';  //Indicates the end of String

Not that the arg string has to end with '\\0' too. 并非arg字符串也必须以'\\0'结尾。

This will prevent the segmentation fault you are getting, but you might want to check another cases where your program might fall.. 这将防止您遇到分段错误 ,但是您可能需要检查程序可能会失败的其他情况。

For more information, see this . 有关更多信息,请参见this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM