Thread: glibc detected

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    22

    glibc detected

    What will cause this kind of problem? I know it might be helpful to bring up my code. But it's pretty much a mess.

    *** glibc detected *** ./led: malloc(): memory corruption: 0xb77e67bf ***
    ======= Backtrace: =========
    /lib/i686/cmov/libc.so.6(+0x6b281)[0xb770e281]
    /lib/i686/cmov/libc.so.6(+0x6e085)[0xb7711085]
    /lib/i686/cmov/libc.so.6(__libc_malloc+0x5c)[0xb7712c8c]
    ./led[0x8048ab5]
    ./led[0x8048a20]
    ./led[0x80486ff]
    ./led[0x80486d5]
    /lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb76b9c76]
    ./led[0x8048581]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 00:16 87787899 /tmp_amd/adams/import/1/wsux397/cs1921/assignment/led
    08049000-0804a000 rwxp 00000000 00:16 87787899 /tmp_amd/adams/import/1/wsux397/cs1921/assignment/led
    0804a000-0806b000 rwxp 00000000 00:00 0 [heap]
    b7500000-b7521000 rwxp 00000000 00:00 0
    b7521000-b7600000 ---p 00000000 00:00 0
    b764f000-b766c000 r-xp 00000000 08:01 229412 /lib/libgcc_s.so.1
    b766c000-b766d000 rwxp 0001c000 08:01 229412 /lib/libgcc_s.so.1
    b768b000-b768c000 rwxp 00000000 00:00 0
    b768c000-b769f000 r-xp 00000000 08:01 353222 /lib/i686/cmov/libnsl-2.11.2.so
    b769f000-b76a0000 r-xp 00012000 08:01 353222 /lib/i686/cmov/libnsl-2.11.2.so
    b76a0000-b76a1000 rwxp 00013000 08:01 353222 /lib/i686/cmov/libnsl-2.11.2.so
    b76a1000-b76a3000 rwxp 00000000 00:00 0
    b76a3000-b77e3000 r-xp 00000000 08:01 352450 /lib/i686/cmov/libc-2.11.2.so
    b77e3000-b77e5000 r-xp 0013f000 08:01 352450 /lib/i686/cmov/libc-2.11.2.so
    b77e5000-b77e6000 rwxp 00141000 08:01 352450 /lib/i686/cmov/libc-2.11.2.so
    b77e6000-b77e9000 rwxp 00000000 00:00 0
    b7805000-b7807000 rwxp 00000000 00:00 0
    b7807000-b7808000 r-xp 00000000 08:01 3410173 /usr/local/lib/innetgr.so
    b7808000-b7809000 rwxp 00000000 08:01 3410173 /usr/local/lib/innetgr.so
    b7809000-b780c000 rwxp 00000000 00:00 0
    b780c000-b7827000 r-xp 00000000 08:01 229477 /lib/ld-2.11.2.so
    b7827000-b7828000 r-xp 0001a000 08:01 229477 /lib/ld-2.11.2.so
    b7828000-b7829000 rwxp 0001b000 08:01 229477 /lib/ld-2.11.2.so
    bff15000-bff2a000 rw-p 00000000 00:00 0 [stack]
    ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
    Aborted (core dumped)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    ./led[0x8048ab5]
    ./led[0x8048a20]
    ./led[0x80486ff]
    ./led[0x80486d5]
    Well first, you compile with the -g flag, so you get meaningful symbols in your code, rather than just raw addresses.
    That at least would give you somewhere to look.

    The basic problem is that you've trashed the memory pool. There are plenty of ways to do this.

    > I know it might be helpful to bring up my code. But it's pretty much a mess.
    Messy code brings it's own problems.
    - Harder to write,
    - Harder to debug,
    - Easier to add bugs to it
    - Impossible to get anyone else to give a damn about
    Careful cleanup will often reveal a bunch of stupid things you can fix along the way.

    You can also try using valgrind as well.
    this post is an example showing a simple overrun and memory leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    22
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define NUMBERLINES 1000  // max number of lines that can be read
    #define LINELENGTH  1000  // max length of a line
    #define FNAMELENGTH 100   // max length of a file name
    #define CMDLENGTH   102   // max length of a command (e.g.'f' + ' ' + filename)
    
    
    typedef struct Node {
         char   line[LINELENGTH];
         struct Node *next;
    } NodeT;
    
    
    void ScanInput(FILE *, char*);
    void PrintCommand(void);
    void PrintSpace(void);
    void PrintAll(NodeT *);
    NodeT *ReadFile(FILE *);
    void Unknown(void);
    void PrintCur(NodeT *);
    NodeT *makeNode(char *);
    //NodeT *GotoB(NodeT* );
    //void Append(FILE *, char*);
    
    
    
    int main(int argc, char **argv) {
     
       FILE *fp;
       fp = NULL;
      
       if(argc > 2){
          printf("Usage: ./led [filename]\n");
          exit(1);
       }
    
       if(argv[1] != NULL){   
          if((fp = fopen(argv[1],"r")) != NULL) {
            
          }
       
          else {
             fp = fopen(argv[1],"w");
             printf("Creating file %s\n", argv[1]);
          }     
       }
    
       while(1) ScanInput(fp, argv[1]);
    
       return 0;
    
    }
    
    
    void ScanInput(FILE *s, char *argv){
       char input[CMDLENGTH];   
       char filename[FNAMELENGTH];
       NodeT *head = NULL;
       NodeT *cur = NULL;
       
       strcpy(filename,argv);
          
       if( s!= NULL){
          head = ReadFile(s);
          cur = head;
       }
    
       printf("? ");   
          
       scanf("%s",input);   
       if(strcmp(input,"h") == 0){
          PrintCommand();
       }
       if(strcmp(input,"q") == 0){
          exit(1);
       }
       if(strcmp(input,"p") == 0){
         // cur = GotoB(head);
          PrintAll(head);
          
       }
       if(strcmp(input,"x") == 0){
          printf("Forced exit, changes not saved\n");
          exit(1);
       }
       if(strcmp(input,".") == 0){
          PrintCur(cur);
       }
       /*if(strcmp(input,"-") == 0){
          DecP();
       }
       if((strcmp(input,"+") == 0) || (strcmp(input,"\n") == 0)){
          IncP();
       }      
       
    
    
    
       if(strcmp(input,"a") == 0){
          Append(s, filename);
       }
       if(strcmp(input,"f") == 0){
          if(scanf("%s",filename) != 0) {
             Unknown();
          }
          if((fp1 = fopen(filename,"r")) != NULL){
             fclose(fp1);
             printf("File %s already exist, saving will overwrite", filename);
          }
          else {
             printf("Creating file :%s\n", filename);
             fp1 = fopen(filename,"w");
             
          }
       }*/
    }
    
    
    
    /*NodeT *GotoB(NodeT* head){
       NodeT *tmp;
       tmp = head;
       if(tmp == NULL){
          return NULL;
       }
       while(tmp->next != NULL){
          tmp = tmp -> next;
       }
       
       return tmp;
    }*/
    
    void PrintCur(NodeT *cur){
       if(cur == NULL) return ;
       printf("%s",cur->line);
    }
    
    
    
    
    void PrintCommand(){
       
       printf("Commands are (in upper or lower case):\n");
       PrintSpace();
       printf("q:      quit\n");
       PrintSpace();
       printf("s:      save\n");
       PrintSpace();
       printf("x:      force exit\n");
       PrintSpace();
       printf("f <filename>: the file is called <filename>\n");
       PrintSpace();
       printf("h:      print this help message\n");
       PrintSpace();
       printf("d:      delete current line\n");
       PrintSpace();
       printf("a:      append after current line, terminated by '.'\n");
       PrintSpace();
       printf("i:      insert before current line, terminated by '.'\n");
       PrintSpace();
       printf("p:      print all lines\n");
       PrintSpace();
       printf(".:      print current line\n");
       PrintSpace();
       printf("+:      increment line and print\n");   
       PrintSpace();
       printf("<return>:     same as '+'\n");
       PrintSpace();
       printf("-:      decrement line and print\n");
       PrintSpace();
       printf("number: make 'number' the current line\n");
    
    }
    
    void PrintSpace(){
       printf("        ");
    }
    void Unknown(){
       printf("Unknown command: ignoring\n");
    }
            
    void PrintAll (NodeT *s) {
       NodeT *cur;
       int line = 1;
       cur = s;
       while(cur != NULL) {
          printf("LINE %d %s", line, cur->line);
          cur = cur->next;
          line++;
       }
    
    }
    
    NodeT *ReadFile(FILE *fp){
       int i = 1;   
       char line [LINELENGTH ];    
       char *get;
       
       NodeT *head;
       NodeT *cur;
       NodeT *new;
    
       if(fp == NULL){
          printf("Empty File\n");   
          return NULL;
       }
       
       if((get = fgets(line, LINELENGTH , fp)) != NULL) {
          head = makeNode(line);
          cur = head;
       }
       
       while (fgets(line, LINELENGTH , fp) != NULL) {
          new = makeNode(line);
          cur->next = new;
          cur = new;
          i++;
       }
       if((i == 1) && (get == NULL)) {
          printf("Empty file");   
          return NULL;
       }
    
       fclose(fp);
       return head;
    }
    
    
    /*void Append(FILE *fp, char *filename){
       int i = 1;
       char *input = NULL;
       NodeT *temp;   
       
       
       
       fp = fopen(filename,"w+");
       
       while(strcmp(input,".") != 0) {
          scanf("%s",input); 
          
    */
    
    
    NodeT *makeNode(char *line) {
       NodeT *new;
       new = malloc(sizeof(NodeT));
       if (new == NULL) {
         fprintf(stderr, "Out of memory\n");
         exit(1); // program should terminate
       }
       strcpy(new->line,line);
       new->next = NULL;
       return new;
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    22
    this is my code & its design for reading a file and edit. and we can type in various command like "p" print out the data in file(which is the one cause the problem).

    also,does -g mean using gdb to detect errors?
    since im new to it, i can hardly understand the error messages.#0 0xffffe424 in __kernel_vsyscall ()
    #1 0xb7773751 in *__GI_raise (sig=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    #2 0xb7776b82 in *__GI_abort () at abort.c:92
    #3 0xb77aa18d in __libc_message (do_abort=2,
    fmt=0xb786e738 "*** glibc detected *** %s: %s: 0x%s ***\n")
    at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
    #4 0xb77b4281 in malloc_printerr (action=<value optimized out>,
    str=0x6 <Address 0x6 out of bounds>, ptr=0xb788c7bf) at malloc.c:6267
    #5 0xb77b7085 in _int_malloc (av=<value optimized out>,
    bytes=<value optimized out>) at malloc.c:4396
    #6 0xb77b8c8c in *__GI___libc_malloc (bytes=1004) at malloc.c:3661
    #7 0x08048a8c in makeNode (line=0xbfc32b9c "(�\004\b") at led.c:249
    #8 0x080489e7 in ReadFile (fp=0x804a008) at led.c:212
    #9 0x08048714 in ScanInput (s=0x804a008, argv=0xbfc337ba "123") at led.c:67
    #10 0x080486d5 in main (argc=2, argv=0xbfc33174) at led.c:51

    thanks for helping

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Ok, this is what I can read from your stack trace

    #6 0xb77b8c8c in *__GI___libc_malloc (bytes=1004) at malloc.c:3661
    malloc was called with a size of 1004, which matches nicely the size of your struct, being 1000 chars and a pointer.

    #7 0x08048a8c in makeNode (line=0xbfc32b9c "(�\004\b") at led.c:249
    led.c:249 is the filename and line number in the source code, so you can easily locate the line of code that called malloc.
    makeNode is the function name
    line is your parameter to makeNode - but the strange thing is, does that look like a valid string in your input file?

    #8 0x080489e7 in ReadFile (fp=0x804a008) at led.c:212
    Called from ScanInput()

    #9 0x08048714 in ScanInput (s=0x804a008, argv=0xbfc337ba "123") at led.c:67
    Called from main()

    #10 0x080486d5 in main (argc=2, argv=0xbfc33174) at led.c:51


    I ran your code, tried the p command, and got this
    LINE 49
    Empty file?

    It seems to me that you're trying to read the file each time you call ScanInput()
    This doesn't make any sense.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    22
    thanks a lot. my problem seems to be reading the file each time i call ScanInput;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GLIBC DETECTED error
    By programhelp in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2010, 10:01 PM
  2. glibc detected: what can I do?
    By violatro in forum C Programming
    Replies: 11
    Last Post: 06-11-2010, 06:39 AM
  3. glibc detected - error
    By mr_m4x in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 10:29 AM
  4. *** glibc detected *** a.out: realloc()
    By msshapira in forum C Programming
    Replies: 9
    Last Post: 01-27-2009, 09:49 AM
  5. glibc detected.
    By eXeCuTeR in forum C Programming
    Replies: 19
    Last Post: 07-09-2008, 01:38 PM