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);
}
}
}