Thread: Another memory problem

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    Another memory problem

    Ok so occasionally I get a problem and I get a segfault error, and when I check it was with valgrind I get complain how when I strtok in the assignTemps(string, circuit) function, that string is do invalid reads of 1 byte. I've been looking over the code but I can't find anything wrong. Can someone help me? Thanks.

    Code:
    void getTemperature(Circuit* circuit, char* flp, char* ptrace)
    {
        int osize = 0;
        int size = 0;
        char* string = 0;
        char* overflow = 0;
    
        int status;
        int fd[2];
        int pid;
        int chars_read;
        char buffer[BUFFER];
    
        pipe(fd);
    
        if((pid = fork()) < 0)
        {
             perror("fork");
             exit(-1);
        }
    
        else if(pid == 0)
        {//child
             close(fd[0]);
             dup2(fd[1], STDOUT_FILENO);
    
            execl(hotspotDir, hotspotDir, "-c", "hotspot.config", "-f", flp, "-p", ptrace, (char*)0); 
    	 perror("EXEC!!!!!!!!!!!");
             exit(-1);
        }
        else
        {//parent
             pid = wait(&status);
             close(fd[1]);
    
             chars_read = read(fd[0], buffer, 20);
    
             while((chars_read = read(fd[0], buffer, BUFFER-1)) > 0)
             {
                  buffer[chars_read] = '\0';
                  
                  size = (int)rindex(buffer, '\n') - (int)buffer;
                  if(overflow == 0)
                  {     
                     string = malloc(size+1);
                     string = strncpy(string, buffer, size);
                     size = chars_read - size;
                     if(size != 0)
                     {
                        overflow = malloc(size+1);
                        overflow = strncpy(overflow, rindex(buffer, '\n') + 1, size);
                     }
                     else
                        overflow = 0;
                        
                     osize = strlen(overflow);
                  }
                  else
                  {
                     osize = strlen(overflow);
                     overflow = realloc(overflow, osize + size+1);
                     overflow = strncat(overflow, buffer, size);
                     string = overflow;
                     overflow = malloc(chars_read - size+1);
                     overflow = strncpy(overflow, buffer + size + 1, chars_read - size);
                  }
                  assignTemps(string, circuit);
    
            }
        }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    rindex returns a pointer, which may be NULL

    So this makes no sense whatsoever
    > size = (int)rindex(buffer, '\n') - (int)buffer;


    > chars_read = read(fd[0], buffer, 20);
    Why do you junk the first 20 chars?
    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
    Dec 2007
    Posts
    15
    >rindex returns a pointer, which may be NULL

    >So this makes no sense whatsoever
    >> size = (int)rindex(buffer, '\n') - (int)buffer;

    buffer always has a newline character so i don't have to worry about that.
    That line pretty much tells me how big the line read in is.


    >> chars_read = read(fd[0], buffer, 20);
    >Why do you junk the first 20 chars?
    This is just header information that I don't care about.

    The weird thing is what that code does works most of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with atl memory cleaning
    By Mariam1989 in forum Windows Programming
    Replies: 1
    Last Post: 11-11-2008, 12:35 PM
  2. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM