Search code examples
carraysloopscomparisonc-strings

I keep getting segmentation fault: 11 when trying to compare two arrays in C


I am trying to compare two arrays that hold data and if one array matches data in another array I want it to print to the screen which data it found a match on. I keep getting a segmentation fault: 11 and it also prints match 1000 which is wrong. Normally I'm reading in a csv file that holds a lot more data but for the sake of testing and simplifying it I've just created smaller arrays of similar data. Any help would be great.

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

#define BUFFER_SIZE 100



int main(int argc, char *argv[]){

    //Setting up variables
    int i,j,count;

    /*Setting up file pointer and opening file
    //FILE *fp;
    //fp = fopen("csvTest.csv","r");
    //if(!fp){
        //printf("File did not open");
    }*/

    //Making a string buffer and getting the data from the CSV
    //and placing it into the buffer
    char buff[BUFFER_SIZE] = "1000,cap_net_raw,cap_sys_admin,cap_setpcap";
    //fgets(buff, 100, fp);
    //fclose(fp);

    //Finding out how many caps there are based off commas
    for(i=0;buff[i] != '\0';i++){
        count += (buff[i] == ',');
    }
    printf("Number of caps: %d\n", count);

    //Using strtok (string token) to split the buffer string at the comma
    //Setting up an array to hold the data after the split
        char *tokptr = strtok(buff,",");
        char *csvArray[count+1];
        i = 0;
        while(tokptr != NULL){
              csvArray[i++] = tokptr;
              tokptr = strtok(NULL, ",");
        }

    //Printing the data from the array 
    printf("EUID & CAPS from CSV file: \n");
        for(j=0; j < i; j++){
            printf("%s\n", csvArray[j]);
        }

    //Getting all current caps and storing them into an array
    //cap_t caps = cap_get_proc();
    //char *capList[CAP_LAST_CAP] = {};
    char *capList[7] = {"cap_chown","cap_setfcap","cap_net_raw","cap_sys_admin",
                 "cap_setpcap","cap_net_admin","cap_sys_overide"};

    /*for(i = 0; i < CAP_LAST_CAP; i++){
        //capList[i] = cap_to_name(i);
    }*/

    //Setting the caps by comparing the csv array and checking to see if
    //there is a match in the caplist, if there is then it will set the cap
    for(i = 0; i < count; i++){
        for(j = 0; j < 37; j++){
            if(strcmp(csvArray[i],capList[j]) == 0){
                printf("Set cap to %s\n", csvArray[i]);
            }
        }
    }

   return 0;
}

Solution

  • I found two problems:

    count is not initialized. When you are counting commas, you start from an unpredictable value. This is undefined behavior.

    The loop over capList goes out of bounds. The j loop goes up to 37, but the array is only of size 7. This is also undefined behavior.

    Fixing those two problems, your program appears to work (at least, it doesn't crash -- whether it has the behavior you intend, I can't be sure).