0
    char *headerString = strstr(line, "...\">");
    printf("%d", feof(site));                                   //all is ok
    sscanf(headerString, "...\">%[^<]", tempQuestion.header);
    printf("%d", feof(site));                                   //crash

I am completely puzzled why it crashes. I mean, sscanf doesn't do anything with FILE *site, why would it possibly crash?

EDIT: tempQuestion.header is (char *). What else would you like to know? Everything befor this part worked good. I guess, that I am getting access violation error then program crashes. Using debugger I found out that after the sscanf is done site address changes.

user1242967
  • 1,220
  • 3
  • 18
  • 30

1 Answers1

1

If (effectively, I know this is illegal declaration but is for illustration):

char* tempQuestion.header;

Then before writing to header it must be pointing at valid memory, otherwise the behaviour is undefined and could be the reason for the behaviour witnessed at feof. To correct malloc sufficient memory for header before using in sscanf.

Equally, what if strstr returns NULL? This would be passed to sscanf which would dereference it causing undefined behaviour (again).

Summary:

  • ensure header is pointing at valid memory
  • ensure strstr does not return NULL
hmjd
  • 120,187
  • 20
  • 207
  • 252