1

I am getting a segmentation fault when I want to run this program. It is supposed to by a self-written grep function but case-insensitive. The same code for case-sensitive works just fine so it might have something to do with tolower()?. Please help. Used VS code in Ubuntu on a Windows PC.

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


int main (int argc, char *argv[]) {
    if (argc > 3) {
        printf("Too many arguments. Only use 2: your search string and the file name.");
        return -1;
    }

    //FILE * fp;
    FILE * fp = fopen(argv[2], "r");
    if(fp == NULL) {
           perror("Unable to open file!");
           return -1;
       }
    char * buffer = NULL;
    size_t bufsize = 0;
    char * lowerBuffer = malloc(sizeof(strlen(buffer)));

    for (int i = 0; buffer[i] != '\0'; i++) {
        lowerBuffer[i] = tolower(buffer[i]);
    }
    
    printf("tolower: %s", lowerBuffer);

    while (getline(&buffer, &bufsize, fp) != -1) {
        if (strstr(buffer, argv[1]))
        {
            printf("%s", buffer);
        }
                
        //printf("%zd", strlen(buffer));
    }
    
    fclose(fp);
    free(buffer);

    //getline();

    return 0;
}

Compiled with gcc mygrepin.c, run with ./a.out test test.txt. This is what my test.txt looks like: hallo das ist ein Test.
test
test
test
test
test

I am expecting this output for this command ./a.out test test.txt:
hallo das ist ein Test.
test
test
test
test
test

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
learning
  • 13
  • 3

1 Answers1

0

You try to use

malloc(sizeof(strlen(buffer)))

on an object you just defined as Null

char * buffer = NULL;

Malloc is used to allocate space. What you want to do is to allocate space for the size of a string with the length of your variable buffer. Buffer is null.

Edit: As a comment pointed out not no space is allocated. It is either 4 or 8 depending on your machine. It works on the type. A pointer should return the value of 4 or 8 depending on your architecture of the system. Thanks to @mch for pointing that out. Even though this is true, the problem still remains and was caused by the buffer never being set to a value besides NULL.

SickerDude43
  • 168
  • 8
  • Please elaborate which part is problematic there. – Sourav Ghosh Feb 16 '23 at 13:37
  • Is that better? @SouravGhosh The logic is pretty faulty here. I thought he figured it out by re-reading those two lines. – SickerDude43 Feb 16 '23 at 13:42
  • Yes I did figure out what the problem was by these two lines, so thank you! I just have t find a solution now :) btw female here! – learning Feb 16 '23 at 13:48
  • No problem and good luck to you for future projects – SickerDude43 Feb 16 '23 at 14:01
  • 2
    `What you want to do is to allocate space for the size of a string with the length of your variable buffer. Buffer is null. So no space is allocated.` is not true. `sizeof` evaluates to a compile time constant and works on the type. `strlen(buffer)` would (would, because it is not called in this context) return a `size_t`, so it is just `sizeof(size_t)`, which is `4` or `8` on typical machines. – mch Feb 16 '23 at 14:42
  • @mch Thank you. I tested this and you are right. Edited my answer. – SickerDude43 Feb 16 '23 at 15:09