0

I was debugging some C code that was giving me a segmentation fault. I was trying to localize the issue by using printf()s.

However, even though I scoped the issue between two printf()s, I couldn't deduce the problem, because what was between these two printf()s was just a simple pointer declaration.

Finally, I used gdb to actually debug and see what's happening. I found that the Segmentation fault was as a result of an infinite loop somewhere down the line way past the second printf().

I always thought that using **printf()**s to localize seg-faults was an easy way out for simple programs. But seems not.

Can someone explain the behavior? Is it as a result of some sort of compiler optimization? Or has it something to do with the structure of my code itself?

The code I was testing is as below.
The Seg-Fault happens between the ----BEGIN----- and ----END----- printf calls.
The actual issue is in the last while loop which becomes an infinite loop.

I hope this is a legit question and not asked before.

void Replace(char *name,char *newname,char record[]) {
    printf("CAME\n");
    char *tempRecord = record;

    printf("This too\n");
    char *newRecord = newname;

    printf("----BEGIN-----\n");
    char *q;
    printf("----END-----\n");
    int i = 0;

    printf("HERE 1");
    while(*(tempRecord+i) != '\0') {
        if(*(tempRecord+i) == ','){
            q = tempRecord+i;
            break;
        }
        i++;
    }

    int j = 0;
    printf("HERE 2");
    while(j >= 0) {
        if(*(newname + j) == '\0') {    
            break;
        }
        j++;
    }
    int a =0;

    printf("HERE 3");
    while (*(q+a) != '\n') {
        *(newname + j + a) = *(q+a);
        a++;
    }

    printf("HERE 4");
    int b = 0;
    while (*newname != '\n'){
        *(record + b) = *(newname + b);
        b++;
    }

    printf("%s",record);
}

EDIT: Adding more source code

void main(void) {
    char arrayName[100];
    char arrayNewname[100];
    char *name = arrayName;
    char *newname = arrayNewname;
    char record[1000];
    printf("Please enter a name in the phonebook: ");
    scanf("%s",name);
    printf("Please enter the replacement name: ");
    scanf("%s",newname); 

    FindRecord("PhoneBook.csv",name,record);
    // records in csv are formatted as: "Bobby, 26, 5145480546"
    // The above method copies the matching record in csv into record[]
    printf("%s\n",record);

    Replace(name, newname,record);
}

Thank You
Shabir

Shabirmean
  • 2,341
  • 4
  • 21
  • 34
  • 1
    You'll need to post how you call this function and the input it crashes on. There are a few lines here that might or might not be a problem depending on the input. – MFisherKDX Nov 04 '17 at 05:56
  • Your do know that this doesn't copy any data, right? `char *newRecord = newname;` It only copies the address of the data (the pointer). – MFisherKDX Nov 04 '17 at 06:00
  • @MFisherKDX Yes, I get that it's an address copy. Let me see if I can post the entire code here. – Shabirmean Nov 04 '17 at 06:03
  • @MFisherKDXAdded more code. Let me know whether I need to add more. – Shabirmean Nov 04 '17 at 06:09
  • When you cause undefined behavior, it can result in errors (if any) at any time in the program, not necessarily at the point of the buggy code. It just depends on when the program tries to use the memory that was corrupted by your error. – Barmar Nov 04 '17 at 06:11
  • It looks like your loops are simply replicating what the functions `strchr()` and `strlen()` do. – Barmar Nov 04 '17 at 06:16
  • You may also want to look into `strtok`. – Barmar Nov 04 '17 at 06:16
  • 1
    You have no newlines in a lot of your later prints, making it very hard for you to tell whether or not they've executed. Also, you don't state *precisely* what output you saw, only state your conclusion that the fault happened between the two `printf` calls. It's very easy to draw the wrong conclusion. – David Schwartz Nov 04 '17 at 06:16
  • @Barmar - It's an assignment for 1st-year students who are not supposed to use the ****. I came across this whilst helping a student. – Shabirmean Nov 04 '17 at 06:18
  • Are you also not supposed to use subscripts? `*(q+a)` is easier to understand if you write it as `q[a]`. – Barmar Nov 04 '17 at 06:20
  • @Barmar - Yes, no array notation. – Shabirmean Nov 04 '17 at 06:21
  • I would suggest adding a `printf` statement (with newline) inside every loop for each character iterated. If you are outside your bounds, you should be able to tell. Also, the last `printf` statement obviously assumes the string is nul terminated. It is impossible to tell if that is happening here or not. – MFisherKDX Nov 04 '17 at 06:23
  • Thanks, everyone. Accepted below answer since it actually solves the questions I had. – Shabirmean Nov 04 '17 at 06:28

2 Answers2

1

Here is a link to the question regarding using printf() for debugging segmentation faults.

Execution of printf() and Segmentation Fault

Use fflush(stdout); after the lines you want to print out. This ensures that the line always gets printed. Having a newline in the printf() doesn't always guarantee that the line gets printed.

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. If you're just referencing another SO question, flag this as a duplicate instead of posting an answer. – Barmar Nov 04 '17 at 06:12
  • This basically answers most of the dilemmas I had. If there is no other explanation for what I'm getting, this seems legit to me. – Shabirmean Nov 04 '17 at 06:20
  • I 've had similar problems tracking segmentation faults and using a debugger like gdb is the best way to find segmentation faults as far as I know. – Sreedev Shibu Nov 04 '17 at 06:22
0

The question is not crystal clear, but I give an answer to explain why the segmentation fault pop up at this line. Do you defined a stack address and size somewhere? It sounds you have issue because the stack datas are stored at an un accessible memory (most probably 0 ) Adding printf is a intrusive method, especially on embedded systems, so you are using more resources (heap) and thus modify code behavior. For the fact the segmentation fault is seen prior the real root cause,Do you use compilation options Flag -0x? what is your target (x86, arm,..)?

claudio06
  • 44
  • 3