0

The C code is very simple: from a file named file_bib1 i get every line of text for further analysis (not yet implemented). This is the code:

int main ()
{
FILE * fp;
char *a;
int check;
int end = 0;
long int size;
long int start = 0;
fp = fopen("file_bib1", "r");

while ( end != 1 )
{
    size = 0;

    if (ftell(fp) != 0)
        start = ftell(fp) + 1;

    fseek(fp, start, SEEK_SET); 
    while (getc(fp) != '\n')
        size = size + 1;

    fseek(fp, start, SEEK_SET);
    a = (char*) malloc(size * sizeof(char));

    check = fscanf(fp, "%[^\n]", a);
    if( check == 0)
    {
        fscanf(fp, "%[^\EOF]", a);
        end = 1;
    }
    printf("%s\n", a);
    free(a);
}

fclose(fp);

return 0;
}

This instead is the content of 'file_bib1' :

autore: Di Ciccio      , Antonio; titolo: Manuale di architettura pisana; editore: Palestro; anno: 1990; prestito: t Fri Feb 10 00:00:00 CET 2012; volume: 1345; scaffale: A.west.2;      
autore : Di Ciccio   , Antonio; titolo: Manuale di architettura pisana; editore: Palestro; anno: 1990; luogo_pubblicazione: Milano; prestito: 10-02-2012; collocazione: B.23.4;descrizione_fisica: 123 p., ill,  23 cm;
autore: Melis, Antonio Pietro Angelo; autore: Gatto Magnolfi, Gianni;  titolo:Manuale di architettura fiorentina; editore: EQR; anno: 1910; nota:  Copia del Vescovo; collocazione: Z.12.56; luogo_pubblicazione: Firenze; descrizione_fisica: 34 p., cov,  59 cm;
autore: Kernighan, Brian W.; autore: Ritchie, Dennis M.;  titolo: Il linguaggio C (seconda Edizione); editore: Jackson Libri; anno: 1989; nota: Edizione italiana; collocazione: Z.22.56; luogo_pubblicazione: Milano; descrizione_fisica: 359 p., softcov,  13 cm;
autore: Kernighan, Brian W.; autore: Pike, Rob;  titolo: Programmazione nella Pratica; editore: Addison-Wesley Longman Italia Editoriale; anno: 1999; nota: Edizione italiana; collocazione: Z.24.56; luogo_pubblicazione: Milano; descrizione_fisica: 294 p., softcov,  13 cm; prestito: 13-07-2012;
autore: Singh, Jaswinder Pal; autore: Culler, David E.;  autore: Gupta,Anoop; titolo: Parallel Computer Architecture: A Hardware/Software Approach; editore: Morgan Kaufmann; anno: 1999; nota:  Copia M. Fillo; collocazione: QA76 58; luogo_pubblicazione: San Francisco, CA; descrizione_fisica: 1021 p., cov, ill,  23 cm;
autore: Hennessy, John L.; autore: Patterson, David A.;  titolo: Computer Architecture, Fifth Edition: A Quantitative Approach   ; editore:  Morgan Kaufmann; anno: 2011; nota: ; collocazione: HHH.56; luogo_pubblicazione: San Francisco, CA; descrizione_fisica: 896 p., softcov, 7 1/2 X 9 1/4 inc;
autore: Luccio, Fabrizio; autore: Pagli, Linda;  autore: Steel, Graham;titolo:Mathematical and Algorithmic Foundations of the Internet ; editore: CRC Press, Taylor and Francis Group; anno: 2011; nota: Chapman & Hall/CRC Applied Algorithms and Data Structures series; collocazione: Z.DDf.56; luogo_pubblicazione: New York; descrizione_fisica: 434 p., softcovcov,  22 cm;
autore : Di Ciccio   , Antonio; titolo: Manuale di architettura pisana; editore: Palestro; anno: 1990; luogo_pubblicazione: Milano; prestito: 10-0-2012; collocazione: B.23.4;descrizione_fisica: 123 p., ill,  23 cm;
autore: Melis, Antonio Pietro Angelo; autore: Gatto Magnolfi, Gianni;  titolo:Manuale di architettura fiorentina; editore: EQR; anno: 1910; nota:  Copia del Vescovo; collocazione: Z;12.56; luogo_pubblicazione: Firenze; descrizione_fisica: 34 p., cov,  59 cm;
autore: Melis, Antonio Pietro Angelo; autore:, Gianni;  titolo:Manuale di architettura fiorentina; editore: EQR; anno: 1910; nota:  Copia del Vescovo; collocazione: Z.12.56; luogo_pubblicazione: Firenze; descrizione_fisica: 34 p., cov,  59 cm;
autore: Singh, Jaswinder Pal; autore: Culler, David E.;  autore: Gupta,Anoop; titolo: Parallel Computer Architecture: A Hardware/Software Approach; editore: Morgan Kaufmann; 1999; nota:  Copia M. Fillo; collocazione: QA76 58; luogo_pubblicazione: San Francisco, CA; descrizione_fisica: 1021 p., cov, ill,  23 cm; 
autore: Hennessy, John L.; autore: Patterson, David A.;  titolo: Computer Architecture, Fifth Edition: A Quantitative Approach   ; editore:  Morgan Kaufmann; anno: 2011; nota: ; collocazione: HHH.56; luogo_pubblicazione: San Francisco, CA; descrizione_fisica: 896 p.; softcov, 7 1/2 X 9 1/4 inc; 
autore: Luccio, Fabrizio; autore: Pagli, Linda;  autore: Steel, Graham;titolo:Mathematical and Algorithmic Foundations of the Internet ; editore: CRC Press, Taylor and Francis Group; anno: 2011; nota: Chapman & Hall/CRC Applied Algorithms and Data Structures series; collocazione: Z.DDf.56; luogo_pubblicazione: New York; descrizione_fisica: 434 p., softcovcov,  22 cm
autore: Melis, Antonio Pietro Angelo; autore: Gatto Magnolfi, Gianni;  titolo: Manuale di architettura fiorentina; editore: EQR; anno: 2010; nota:  Copia del Vescovo; prestito: f; volume: 5; Scaffale; B.12;

Each line ends with \n and, the last one, with EOF as usual. When I run the code, I see this error in linux terminal after running well till mid-file

*** glibc detected *** ./prova: double free or corruption (!prev): 0x00000000017e8250 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7f0049d92626]
./prova[0x400880]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f0049d3576d]
./prova[0x4006a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 18770                              /home/arrigo/Desktop/prova
00600000-00601000 r--p 00000000 08:01 18770                              /home/arrigo/Desktop/prova
00601000-00602000 rw-p 00001000 08:01 18770                              /home/arrigo/Desktop/prova
017e8000-01809000 rw-p 00000000 00:00 0                                  [heap]
7f0049afe000-7f0049b13000 r-xp 00000000 08:01 5697                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0049b13000-7f0049d12000 ---p 00015000 08:01 5697                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0049d12000-7f0049d13000 r--p 00014000 08:01 5697                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0049d13000-7f0049d14000 rw-p 00015000 08:01 5697                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0049d14000-7f0049ec7000 r-xp 00000000 08:01 5676                       /lib/x86_64-linux-gnu/libc-2.15.so
7f0049ec7000-7f004a0c6000 ---p 001b3000 08:01 5676                       /lib/x86_64-linux-gnu/libc-2.15.so
7f004a0c6000-7f004a0ca000 r--p 001b2000 08:01 5676                       /lib/x86_64-linux-gnu/libc-2.15.so
7f004a0ca000-7f004a0cc000 rw-p 001b6000 08:01 5676                       /lib/x86_64-linux-gnu/libc-2.15.so
7f004a0cc000-7f004a0d1000 rw-p 00000000 00:00 0 
7f004a0d1000-7f004a0f3000 r-xp 00000000 08:01 5656                       /lib/x86_64-linux-gnu/ld-2.15.so
7f004a2d2000-7f004a2d5000 rw-p 00000000 00:00 0 
7f004a2ee000-7f004a2f3000 rw-p 00000000 00:00 0 
7f004a2f3000-7f004a2f4000 r--p 00022000 08:01 5656                       /lib/x86_64-linux-gnu/ld-2.15.so
7f004a2f4000-7f004a2f6000 rw-p 00023000 08:01 5656                       /lib/x86_64-linux-gnu/ld-2.15.so
7fff9a14f000-7fff9a170000 rw-p 00000000 00:00 0                          [stack]
7fff9a1ff000-7fff9a200000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

Anyone can help me to "translate" this error?

-------------------EDIT----------

ERROR FOUND. With some printf in the code, I spotted the error in ' free(a) ' but i can't understand why it generates such an error...

---------EDIT------------

I found the real problem... the incriminated line, where the program fails, ends with a blanck space after ; terminator... in some way, free(a) goes mad

11
  • It means you corrupted the heap or called free on an already-freed pointer, and glibc detected and reported it for you. Jul 3, 2012 at 13:55
  • why it occurs in the mid of file analysis? O.o for every line of text before 'autore: Hennessy, John L.; autore: Patterson, David A.; titolo: Computer Architecture, Fifth Edition: A Quantitative Approach ; editore: Morgan Kaufmann; anno: 2011; nota: ; collocazione: HHH.56; luogo_pubblicazione: San Francisco, CA; descrizione_fisica: 896 p., softcov, 7 1/2 X 9 1/4 inc;' it works well O.o Jul 3, 2012 at 13:57
  • 1
    Because you're overwriting past the end of the buffer you allocated. Jul 3, 2012 at 13:58
  • 1
    fscanf() does not take a regular expression so those lines are not doing what you think. Jul 3, 2012 at 13:58
  • 1
    fscanf should not even be used here. Just use fgets. Those %[ specifiers are not regular expressions; they're part of scanf, but OP's use of them is incorrect. Jul 3, 2012 at 13:58

1 Answer 1

1

Your problem is that you have not allocated enough memory to store your line (as indicated in comments by R..). You can discover this by printing the strlen of what you read in, and the value of size. They are the same. This means that fscanf will overrun the input buffer when it writes the terminating NUL character, and this corrupted the heap. The error message you received was glibc complaining about its corrupted state.

The reason it doesn't happen on the very first input line is not very important, because writing beyond the allocated memory causes undefined behavior, and undefined behavior includes it not causing a problem. A program should fix undefined behavior not just because it is the right thing to do, but also because it probably will cause a problem eventually.

You could have avoided the problem by specifying a maximum length specifier in your scan string, but you would have had to build the scan string in a buffer to achieve that.

char fmt[64];
snprintf(fmt, sizeof(fmt), "%%%d[^\n]", size);
check = fscanf(fp, fmt, a);

But you would also have to add code to check if you actually read in the complete line. The fix you probably want to make is to increase the size of your allocation by one to account for the terminating NUL. However, fixing this, your program is left in an infinite loop. This is because your getc loop is not checking for EOF. You need to add that.

int c;
while ((c = getc(fp)) != '\n') {
    if (c == EOF) {
        end = 1;
        break;
    }
    size = size + 1;
}
if (!end) {
    //...
}

Your second scan string %[^\EOF] is probably not what you think it means. There is no real way to tell fscanf to scan to the end of file anyway. If the check fails, it is easier to just report the failure and exit. You can then figure out later why your input caused it to fail, and fix the input.

fscanf is tricky to use because of various dangers with dealing with free form input. Buffer overrun is one problem, but the function also can behave unexpectedly if the input does not have the format you expect. This is why programmers will recommend reading the input unconditionally into a buffer, and parse the buffer, rather than use fscanf to read and parse the input at the same time.

You can implement some simple code to use only fgets to get a line at the cost of some copying. You resize the buffer larger if the buffer does not end with a \n, and continue reading.

size_t size = 256;
char *line = malloc(size);
line[size-2] = '\0';
while (fgets(line, size, fp) != 0) {
    while (line[size-2] && line[size-2] != '\n') {
        char *bigger_line = realloc(line, 2*size);
        assert(bigger_line); // rudimentary error handling
        size *= 2;
        line = bigger_line;
        line[size-2] = '\0';
        if (fgets(line + size/2 - 1, size/2 + 1, fp) == 0) {
            // last line doesn't end with '\n'
            break;
        }
    }
    //... handle input line
}
//...
free(line);
3
  • @ArrigoPierotti: I was just warning you away from fscanf. I edited the question with the cause of your problem.
    – jxh
    Jul 3, 2012 at 15:19
  • it's not a problem of string lenght... other lines are leghtier or shortier with no order and no problem is detected... Jul 3, 2012 at 15:27
  • 1
    @ArrigoPierotti: Overrunning the buffer causes undefined behavior, and undefined means it might work, or it might not.
    – jxh
    Jul 3, 2012 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.